I would like to convert a List[Box[T]] into a Box[List[T]].
I know that I could use foldRight, but I can’t find an elegant way into doing so.
EDIT I would like to keep the properties of Box, that is to say, if there is any failure, return a Box with this failure.
It’s really easier to do this with a tail-recursive function than with a fold:
This works as expected:
One can also do this with
Iterator.iterate, but it’s more awkward and slower, so I would avoid that approach in this case.(See also my answer in the question 4e6 linked to.)