``` list.map { if (it.a == key) { ... } el...
# arrow
p
Copy code
list.map {
  if (it.a == key) {
    ...
  } else {
    it
  }
}
g
🤦‍♂️ Thanks for pointing out the obvious; I completely forgot about kotlin...
But how would you handle if say, the modification failed with an Optional or an Either, wouldn't it just create a mixed type list that you'd have to re-iterate over to check?
j
there is
filterMap
for option, and you can in general always use
traverse
.
traverse
is equal to
map
followed by
sequence
and
sequence
can turn a
List<Option<A>>
or
List<Either<L, R>>
into
Option<List<A>>
and
Either<L, List<R>>
. (it short circuits as soon as it finds a None or a Left)
code with traverse:
Copy code
list.traverse(Option.applicative()) {
  if (it.a == key)
    ... (some computation that ends with Option)
  else it.some()
}
should produce the same thing
🔝 1
assuming you want to fail the whole computation with Left or None when modification fails
g
Yes that's what I was after, thanks for your help