Daniel Berg
04/05/2021, 6:20 PMtraverseEither
on Nel. The behavior of traverseEither on Iterable was also different than what I expected. It walks the sequence from back to front and also f
is evaluated on all elements even if a Left value is found.Daniel Berg
04/05/2021, 6:21 PMfun <E, A, B> traverse(xs: List<A>, f: (A) -> Either<E, B>): Either<List<B>> {
val z: Either<E, <List<B>> = listOf<B>().right()
return xs.fold(z) { acc, a ->
acc.flatMap { ys -> f(a).map { ys + it } }
}
}
Daniel Berg
04/05/2021, 6:23 PMf
that we don't executed if a Left value is found.Jannis
04/05/2021, 6:23 PMDaniel Berg
04/05/2021, 6:29 PMJannis
04/05/2021, 6:39 PMList.plus
is insane, it throws away a lot of object arrays and just hurts the gc times. I first noticed this when benchmarking queues in arrow STM...
The current impl should be O(2*N)
as in reverse
followed by fold
which are both N
(I hope). Some people also ignore the constant factor but I think that depends on what you are analyzing and here they do matter.Daniel Berg
04/05/2021, 6:43 PM