From 02d0004cd0d8507c59feabd9d23e0029ab76852f Mon Sep 17 00:00:00 2001 From: "Joseph T. Lyons" Date: Thu, 5 Dec 2024 16:59:10 -0500 Subject: Improve strict_zip runtime --- src/gleam/list.gleam | 16 +++++++++++++--- 1 file 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]) } } -- cgit v1.2.3