https://kotlinlang.org logo
#arrow
Title
# arrow
r

rcd27

08/19/2022, 6:16 PM
Having this setup, trying to aggregate all to one list only those who have
AddressContactInfo
. What is a better FP way to do that?
However, seems legit, but maybe there is an existing tool for such kind of tasks.
And yea, this is CastException 😄
l

Lukasz Kalnik

08/20/2022, 7:36 PM
You could just use `filter`:
Copy code
filter { it.contactInfo.isRight || it.contactInfo.isBoth }
Using
fold
in your example you have basically reimplemented
filter
😉
BTW wouldn't it be better domain modeling to just use nullable or
Option
types for address/email contact info in the
Contact
data class?
Ior
is right-biased, which means
AddressContactInfo
is privileged over
EmailContactInfo
. Or is it just an abstract example?
s

simon.vergauwen

08/22/2022, 8:16 AM
Hey @rcd27, Looks like you might be able use the
ior
binding for this.
Copy code
ior(semigroup) {
  listOf(
    contactWithAddres,
    contactWithMail
   ).map { it.contactInfo.bind() }
  }
}
Where
isLeft
short-circuits like
Either.Left
but
isBoth
accumulates the
E
and return` Right`
isRight
behaves just like
Either.Right
r

rcd27

08/29/2022, 6:38 PM
@Lukasz Kalnik the idea of picking
Ior
was that it represent either left, or right, or both. And I still don't know how to model that by hands, Nullability of those two values means that there is a 4th case: both missing, which is not valid for this case.
Yeah, thanks @simon.vergauwen and @Lukasz Kalnik, I think
Ior
is not the best solution for my example, as it is right-biased, which is something I don't need.
l

Lukasz Kalnik

08/30/2022, 6:43 AM
Ah, that's a very interesting case. So you need a guarantee that at least one of them is present. What about
NonEmptyList
?
2 Views