Change sequence() to return a list
This commit is contained in:
parent
96e7a51ccc
commit
2665f8ff98
|
@ -707,7 +707,7 @@ def unwrap_r(r: Result[A, Any]) -> A:
|
||||||
return val
|
return val
|
||||||
case Err(e):
|
case Err(e):
|
||||||
raise AssertionError(f'Tried to unwrap an error: ', e)
|
raise AssertionError(f'Tried to unwrap an error: ', e)
|
||||||
def sequence(s: Sequence[Result[A, B]]) -> Result[Iterator[A], B]:
|
def sequence(s: Sequence[Result[A, B]]) -> Result[Sequence[A], B]:
|
||||||
"""
|
"""
|
||||||
Convert a list of results into a result of a list.
|
Convert a list of results into a result of a list.
|
||||||
|
|
||||||
|
@ -716,14 +716,14 @@ def sequence(s: Sequence[Result[A, B]]) -> Result[Iterator[A], B]:
|
||||||
errors, proccessing of the sequence is immediately stopped, and the first error
|
errors, proccessing of the sequence is immediately stopped, and the first error
|
||||||
encountered is returned.
|
encountered is returned.
|
||||||
|
|
||||||
>>> map_res(list, sequence([Ok(1), Ok(2), Ok(3)]))
|
>>> sequence([Ok(1), Ok(2), Ok(3)])
|
||||||
Ok([1, 2, 3])
|
Ok([1, 2, 3])
|
||||||
|
|
||||||
>>> sequence([Ok(1), Err('Oops!'), Err('Aw man!')])
|
>>> sequence([Ok(1), Err('Oops!'), Err('Aw man!')])
|
||||||
Err('Oops!')
|
Err('Oops!')
|
||||||
"""
|
"""
|
||||||
if all(s):
|
if all(s):
|
||||||
return Ok(map(unwrap_r, s))
|
return Ok(list(map(unwrap_r, s)))
|
||||||
else:
|
else:
|
||||||
o = next(filter(not_, s))
|
o = next(filter(not_, s))
|
||||||
assert isinstance(o, Err)
|
assert isinstance(o, Err)
|
||||||
|
|
Loading…
Reference in New Issue