starke
06/04/2020, 10:18 PMOption<T> properties
fun example() {
val maybeFoo: Option<DateTime>
val maybeBar: Option<DateTime>
// nested fold feels gross
val isFooAfterBar: Boolean = maybeFoo.fold(
ifEmpty = { false },
ifSome = { foo ->
maybeBar.fold(
ifEmpty = { true },
ifSome = { bar -> foo.isAfter(bar) }
)
}
)
// slightly better? same underlying logic though
val isFooAfterBarAlt: Boolean = maybeFoo.map { foo ->
maybeBar.map { bar -> foo.isAfter(bar) }.getOrElse { true }
}.getOrElse { false }
}
this code works, but I feel like there’s probably a better solution rather than nesting fold 🤔
the logic is basically:
if maybeFoo is none(), return false
if maybeFoo is some() and maybeBar is none(), return true
if maybeFoo is some() and maybeBar is some(), return foo > bar
any advice would be greatly appreciated! thanks