Skip to content

Instantly share code, notes, and snippets.

View Atternatt's full-sized avatar

Marc Moreno Ferrer Atternatt

View GitHub Profile
@pakoito
pakoito / lwo.kt
Last active January 17, 2018 16:44
Lightweight Option
inline fun <A, B> A?.fold(crossinline fn: () -> B, crossinline f: (A) -> B): B =
if (this == null) fn() else f(this)
inline fun <A, B> A?.map(crossinline f: (A) -> B): B? = fold({ null }, f)
inline fun <A> A?.orElse(crossinline fn: () -> A): A = fold(fn, { it })