Having this setup, trying to aggregate all to one ...
# arrow
r
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
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
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
@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
Ah, that's a very interesting case. So you need a guarantee that at least one of them is present. What about
NonEmptyList
?