Marc Plano-Lesay
01/26/2023, 9:57 PMfilterOrElse
to be a bit annoying - while implementing something similar with flatMap
is easy enough, it loses quite a lot in terms of readability. 🧵Marc Plano-Lesay
01/26/2023, 9:58 PM.filterOrElse {
predicate = { userAccountManager.hasSession(it) },
default = { SignInError.Internal },
}
And with `flatMap`:
.flatMap {
if (userAccountManager.hasSession(it)) {
Right(it)
} else {
Left(SignInError.Internal)
}
}
Marc Plano-Lesay
01/26/2023, 9:59 PMfilterOrElse
name - which is not the case at all anymore. Is there a more expressive way to replace filterOrElse
that I'm missing?Stylianos Gakis
01/27/2023, 12:48 AM.flatMap {
if (userAccountManager.hasSession(it)) {
it.right()
} else {
SignInError.Internal.left()
}
}
In fact, I’d personally rather see something like this instead
val x = either<SignInError, Session?> {
val session: Session = Session // you had this before the flatMap, might need to `bind()` here
ensure(userAccountManager.hasSession(session)) { SignInError.Internal }
session
}
Marc Plano-Lesay
01/27/2023, 12:54 AMeither
block + ensure
might be the better option indeed. Thanks, I hadn't considered that one!Stylianos Gakis
01/27/2023, 12:56 AMMarc Plano-Lesay
01/27/2023, 12:57 AM