aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoseph T. Lyons <JosephTLyons@gmail.com>2024-12-05 16:59:10 -0500
committerLouis Pilfold <louis@lpil.uk>2024-12-05 23:47:53 +0000
commit02d0004cd0d8507c59feabd9d23e0029ab76852f (patch)
tree15abdc1caaba2d0c3eb53dfc1b7a9c60837c7ae0
parent89861ef2bab99077c2b2a0223f752be3fe327a06 (diff)
downloadgleam_stdlib-02d0004cd0d8507c59feabd9d23e0029ab76852f.tar.gz
gleam_stdlib-02d0004cd0d8507c59feabd9d23e0029ab76852f.zip
Improve strict_zip runtime
-rw-r--r--src/gleam/list.gleam16
1 files changed, 13 insertions, 3 deletions
diff --git a/src/gleam/list.gleam b/src/gleam/list.gleam
index 6d29064..97ff912 100644
--- a/src/gleam/list.gleam
+++ b/src/gleam/list.gleam
@@ -1087,9 +1087,19 @@ pub fn strict_zip(
list: List(a),
with other: List(b),
) -> Result(List(#(a, b)), Nil) {
- case length(of: list) == length(of: other) {
- True -> Ok(zip(list, other))
- False -> Error(Nil)
+ strict_zip_loop(list, other, [])
+}
+
+fn strict_zip_loop(
+ one: List(a),
+ other: List(b),
+ acc: List(#(a, b)),
+) -> Result(List(#(a, b)), Nil) {
+ case one, other {
+ [], [] -> Ok(acc |> reverse)
+ [], _ | _, [] -> Error(Nil)
+ [first_one, ..rest_one], [first_other, ..rest_other] ->
+ strict_zip_loop(rest_one, rest_other, [#(first_one, first_other), ..acc])
}
}