#What's the best way to take an iterator of results, and get a result with an iterator and an error?

1 messages · Page 1 of 1 (latest)

lethal chasm
#

I'm writing a lexer currently, and it can possibly error in a few areas, so I'm returning an iterator of results with the token and a lex error: Iterator(Result(Token, LexErr))

What would be the best way to convert that to a type of Result(Iterator(Token), LexErr)? I was trying to figure out how to use something like iterator.fold_until but I think I might be missing something.

fiery nest
#

result.all from the stdlib changes from List(Result(a, b)) into Result(List(a), b)

cyan rampart
#

I don't think you can without iterating over the whole thing and iterators are usually used when you want to avoid doing that. you probably want to convert the iterator to a list (iterator.to_list) and then use the function naomi mentioned to invert the types.

#

more concretely, you can't return an Ok until you evaluate every element in the iterator to verify that it's not an Error.