hello! I’m struggling a bit to figure out some log...
# arrow
s
hello! I’m struggling a bit to figure out some logic that uses two
Option<T>
properties
Copy code
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