Skip to content

Commit

Permalink
Added 'map' and 'or' operators
Browse files Browse the repository at this point in the history
  • Loading branch information
Denis Trotsenko committed Oct 31, 2021
1 parent a8ed0c5 commit ae624a5
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
1 change: 0 additions & 1 deletion async/src/main/kotlin/io/github/anvell/async/Async.kt
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ data class Success<out T>(val value: T) : Async<T>() {
fun peek() = value
}


/**
* Represents failure when loading the `value` and stores corresponding [error].
*/
Expand Down
10 changes: 10 additions & 0 deletions async/src/main/kotlin/io/github/anvell/async/Map.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package io.github.anvell.async

inline fun <R, T> Async<T>.map(
transform: (value: T) -> R
): Async<R> = when (this) {
is Uninitialized -> Uninitialized
is Loading -> Loading
is Success -> Success(transform(value))
is Fail -> Fail(error)
}
10 changes: 10 additions & 0 deletions async/src/main/kotlin/io/github/anvell/async/Or.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package io.github.anvell.async

inline fun <T> Async<T>.or(
transform: (error: Throwable) -> Async<T>
): Async<T> = when (this) {
is Uninitialized -> Uninitialized
is Loading -> Loading
is Success -> Success(value)
is Fail -> transform(error)
}

0 comments on commit ae624a5

Please sign in to comment.