Skip to content

Commit 02d0004

Browse files
JosephTLyonslpil
authored andcommitted
Improve strict_zip runtime
1 parent 89861ef commit 02d0004

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

src/gleam/list.gleam

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1087,9 +1087,19 @@ pub fn strict_zip(
10871087
list: List(a),
10881088
with other: List(b),
10891089
) -> Result(List(#(a, b)), Nil) {
1090-
case length(of: list) == length(of: other) {
1091-
True -> Ok(zip(list, other))
1092-
False -> Error(Nil)
1090+
strict_zip_loop(list, other, [])
1091+
}
1092+
1093+
fn strict_zip_loop(
1094+
one: List(a),
1095+
other: List(b),
1096+
acc: List(#(a, b)),
1097+
) -> Result(List(#(a, b)), Nil) {
1098+
case one, other {
1099+
[], [] -> Ok(acc |> reverse)
1100+
[], _ | _, [] -> Error(Nil)
1101+
[first_one, ..rest_one], [first_other, ..rest_other] ->
1102+
strict_zip_loop(rest_one, rest_other, [#(first_one, first_other), ..acc])
10931103
}
10941104
}
10951105

0 commit comments

Comments
 (0)