Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: new api try_collect for builtin/iter #1260

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions builtin/builtin.mbti
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ impl Iter {
take_while[T](Self[T], (T) -> Bool) -> Self[T]
tap[T](Self[T], (T) -> Unit) -> Self[T] //deprecated
to_array[T](Self[T]) -> Array[T]
try_collect[T, E : Error](Self[Result[T, E]]) -> Array[T]!E
}
impl[T : Show] Show for Iter[T]

Expand Down
14 changes: 14 additions & 0 deletions builtin/iter.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -781,6 +781,20 @@ pub fn collect[T](self : Iter[T]) -> Array[T] {
result
}

///|
/// Collects the `Ok` elements of the iterator into an array,
/// short circuiting if a `Err` is encountered.
pub fn try_collect[T, E : Error](self : Iter[Result[T, E]]) -> Array[T]!E {
let result = []
for element in self {
match element {
Ok(x) => result.push(x)
Err(e) => raise e
}
}
result
}

///|
/// Iter itself is an iterator.
/// so that it works with array spread operator. e.g, `[..iter]`
Expand Down
32 changes: 32 additions & 0 deletions builtin/iter_test.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,38 @@ test "collect" {
assert_eq!(vec, ['1', '2', '3', '4', '5'])
}

test "try_collect Ok" {
let arr = ['1', '2', '3', '4', '5']
let iter : Iter[Result[Char, Error]] = Iter::new(
fn(yield_) {
for i = 0, len = arr.length(); i < len; i = i + 1 {
yield_(Ok(arr[i])) |> ignore
}
IterContinue
},
)
let vec = iter.try_collect?()
inspect!(vec, content="Ok(['1', '2', '3', '4', '5'])")
}

test "try_collect Err" {
let arr = ['1', '2', '3', '4', '5']
let iter : Iter[Result[Char, Error]] = Iter::new(
fn(yield_) {
for i = 0, len = arr.length(); i < len; i = i + 1 {
if i % 2 == 0 {
yield_(Ok(arr[i])) |> ignore
} else {
yield_(Err(Failure("odd"))) |> ignore
}
}
IterContinue
},
)
let vec = iter.try_collect?()
inspect!(vec, content="Err(moonbitlang/core/builtin.Failure.Failure)")
}

test "until" {
// edge cases
inspect!((0).until(10).take(100), content="[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]")
Expand Down
3 changes: 2 additions & 1 deletion builtin/moon.pkg.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"moonbitlang/core/array",
"moonbitlang/core/int64",
"moonbitlang/core/uint",
"moonbitlang/core/json"
"moonbitlang/core/json",
"moonbitlang/core/error"
],
"targets": {
"int64_js.mbt": ["js"],
Expand Down