Skip to content

Instantly share code, notes, and snippets.

View mzaimilzam's full-sized avatar

mzaimilzam mzaimilzam

  • Telkom Sigma
  • Serpong
View GitHub Profile
@mzaimilzam
mzaimilzam / ResultFlatMap.kt
Created January 16, 2024 08:27 — forked from gmk57/ResultFlatMap.kt
Implementation of Result.flatMap(), missing from Kotlin stdlib
inline fun <R, T> Result<T>.flatMap(transform: (T) -> Result<R>): Result<R> =
fold({ transform(it) }, { Result.failure(it) })
fun main() {
getUserInput()
.flatMap { input -> parseInput(input) }
.flatMap { numbers -> calculateMax(numbers) }
.onSuccess { maxNumber -> println("max: $maxNumber") }
.onFailure { throwable -> throwable.printStackTrace() }
}