Skip to content

Instantly share code, notes, and snippets.

@surajsau
Created March 13, 2020 11:56
Show Gist options
  • Save surajsau/a7bad51241a8412d33f5f493aa2fc5f5 to your computer and use it in GitHub Desktop.
Save surajsau/a7bad51241a8412d33f5f493aa2fc5f5 to your computer and use it in GitHub Desktop.
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