Skip to content

Commit

Permalink
Simplify predicate-based functions
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelbull committed Mar 8, 2024
1 parent 0b85cc2 commit f208f5e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -251,14 +251,9 @@ public inline fun <V, E> Result<V, E>.toErrorIf(predicate: (V) -> Boolean, trans
callsInPlace(transform, InvocationKind.AT_MOST_ONCE)
}

return when (this) {
is Ok -> if (predicate(value)) {
Err(transform(value))
} else {
this
}

is Err -> this
return when {
this is Ok && predicate(value) -> Err(transform(value))
else -> this
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,9 @@ public inline fun <V, E : Throwable> Result<V, E>.throwIf(predicate: (E) -> Bool
callsInPlace(predicate, InvocationKind.AT_MOST_ONCE)
}

return when (this) {
is Ok -> this
is Err -> if (predicate(error)) {
throw error
} else {
this
}
return when {
this is Err && predicate(error) -> throw error
else -> this
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,9 @@ public inline fun <V, E> Result<V, E>.recoverIf(predicate: (E) -> Boolean, trans
callsInPlace(transform, InvocationKind.AT_MOST_ONCE)
}

return when (this) {
is Ok -> this
is Err -> if (predicate(error)) {
Ok(transform(error))
} else {
this
}
return when {
this is Err && predicate(error) -> Ok(transform(error))
else -> this
}
}

Expand All @@ -63,13 +59,9 @@ public inline fun <V, E> Result<V, E>.recoverUnless(predicate: (E) -> Boolean, t
callsInPlace(transform, InvocationKind.AT_MOST_ONCE)
}

return when (this) {
is Ok -> this
is Err -> if (!predicate(error)) {
Ok(transform(error))
} else {
this
}
return when {
this is Err && !predicate(error) -> Ok(transform(error))
else -> this
}
}

Expand Down Expand Up @@ -101,13 +93,9 @@ public inline fun <V, E> Result<V, E>.andThenRecoverIf(
callsInPlace(transform, InvocationKind.AT_MOST_ONCE)
}

return when (this) {
is Ok -> this
is Err -> if (predicate(error)) {
transform(error)
} else {
this
}
return when {
this is Err && predicate(error) -> transform(error)
else -> this
}
}

Expand All @@ -124,12 +112,8 @@ public inline fun <V, E> Result<V, E>.andThenRecoverUnless(
callsInPlace(transform, InvocationKind.AT_MOST_ONCE)
}

return when (this) {
is Ok -> this
is Err -> if (!predicate(error)) {
transform(error)
} else {
this
}
return when {
this is Err && !predicate(error) -> transform(error)
else -> this
}
}

0 comments on commit f208f5e

Please sign in to comment.