Created
March 13, 2020 11:56
-
-
Save surajsau/a7bad51241a8412d33f5f493aa2fc5f5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sealed class Monad<out T> { | |
object Nothing: Monad<kotlin.Nothing>() | |
data class Something<out T>(val value: T): Monad<T>() | |
inline fun<S> flatMap(f: (T) -> Monad<S>): Monad<S> = when(this) { | |
is Nothing -> this | |
is Something -> f(this.value) // we aren't wrapping f(value) again | |
// since it already returns a wrapped value | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment