Hi guys,
We're starting to use arrow on my project but I'm realizing I'm seriously rusty with my eithers 😞
Trying to transform a result of type
Either<Error, List<A>>
into
Either<Error, List<B>>
So far I have :
call.map { it.map { B(a.value) } }
Seems to me like there should be something simpler but I can't for the life of me find it 😅
y
Youssef Shoaib [MOD]
04/23/2024, 1:53 PM
That's probably the easiest way, but you can also do
Copy code
either {
call.bind().map { B(it.value) }
}
j
Jérémy CROS
04/23/2024, 2:09 PM
oh ok 🤔 I've seen that syntax before but not used to it yet...
not sure why I thought there would be something else 🤔
anyway, thanks! 🙏 will submit to the team to see which one they find the easier to read 🙂
s
simon.vergauwen
04/23/2024, 2:22 PM
There is nothing specific for drilling into a nested List, using Optics it's should also possible... Not entirely sure on the syntax..
Copy code
Either.right<List<A>>().every.modify(original, f)
j
Jérémy CROS
04/23/2024, 2:39 PM
Thanks for the final alternative! 🙂
Submitted to a vote, 100% of the team went for the double map since it's completely explicit and easier to understand at first sight
👍 1
🔝 3
s
stojan
04/23/2024, 2:59 PM
the
either {}
block +
bind()
enables you to work with the right side of the
Either
and short-circuits on left.
Copy code
either {
call.bind() // the result of this is `List<A>`
}
IMO this scales better if you do multiple ops with the right value.
I'd argue it's also explicit, but maybe less familiar
s
simon.vergauwen
04/23/2024, 3:04 PM
@Jérémy CROS I also vote for the other solution 😂 Double map is the way to go IMO. Easy and explicit 👍 Just wanted to give an alternative ☺️