From 3c82d745ee7b59bab60bca498c31337b0413bdcd Mon Sep 17 00:00:00 2001 From: myfreess Date: Tue, 18 Feb 2025 15:55:58 +0800 Subject: [PATCH] align @immut/list.zip with @array.zip --- immut/list/list.mbt | 16 ++++++---------- immut/list/list.mbti | 2 +- immut/list/list_test.mbt | 9 ++++++--- 3 files changed, 13 insertions(+), 14 deletions(-) diff --git a/immut/list/list.mbt b/immut/list/list.mbt index 70a48d996..70213aa13 100644 --- a/immut/list/list.mbt +++ b/immut/list/list.mbt @@ -480,23 +480,19 @@ pub fn fold_righti[A, B](self : T[A], f : (Int, A, B) -> B, init~ : B) -> B { ///| /// Zip two lists. -/// If the lists have different lengths, it will return None. +/// If the lists have different lengths, it will return a list with shorter length. /// /// # Example /// /// ``` /// let r = @list.zip(@list.of([1, 2, 3, 4, 5]), @list.of([6, 7, 8, 9, 10])) -/// assert_eq!(r, Some(@list.from_array([(1, 6), (2, 7), (3, 8), (4, 9), (5, 10)]))) +/// assert_eq!(r, @list.from_array([(1, 6), (2, 7), (3, 8), (4, 9), (5, 10)])) /// ``` -pub fn zip[A, B](self : T[A], other : T[B]) -> T[(A, B)]? { +pub fn zip[A, B](self : T[A], other : T[B]) -> T[(A, B)] { match (self, other) { - (Nil, Nil) => Some(Nil) - (Cons(x, xs), Cons(y, ys)) => - match zip(xs, ys) { - None => None - Some(ls) => Some(Cons((x, y), ls)) - } - _ => None + (Nil, _) => Nil + (_, Nil) => Nil + (Cons(x, xs), Cons(y, ys)) => Cons((x, y), zip(xs, ys)) } } diff --git a/immut/list/list.mbti b/immut/list/list.mbti index 7ee2d0e11..e7d30bce3 100644 --- a/immut/list/list.mbti +++ b/immut/list/list.mbti @@ -95,7 +95,7 @@ impl T { unsafe_minimum[A : Compare](Self[A]) -> A unsafe_nth[A](Self[A], Int) -> A unzip[A, B](Self[(A, B)]) -> (Self[A], Self[B]) - zip[A, B](Self[A], Self[B]) -> Self[(A, B)]? + zip[A, B](Self[A], Self[B]) -> Self[(A, B)] } impl[X] Default for T[X] impl[A : Eq] Eq for T[A] diff --git a/immut/list/list_test.mbt b/immut/list/list_test.mbt index 525ce96ed..54e7e5628 100644 --- a/immut/list/list_test.mbt +++ b/immut/list/list_test.mbt @@ -215,7 +215,7 @@ test "zip" { let rs = @list.of([6, 7, 8, 9, 10]) inspect!( ls.zip(rs), - content="Some(@list.of([(1, 6), (2, 7), (3, 8), (4, 9), (5, 10)]))", + content="@list.of([(1, 6), (2, 7), (3, 8), (4, 9), (5, 10)])", ) } @@ -607,13 +607,16 @@ test "List::zip with lists of equal length" { let list1 = @list.of([1, 2, 3]) let list2 = @list.of(["a", "b", "c"]) let zipped = list1.zip(list2) - let expected = Some(@list.of([(1, "a"), (2, "b"), (3, "c")])) + let expected = @list.of([(1, "a"), (2, "b"), (3, "c")]) assert_eq!(zipped, expected) } ///| test "@list.zip with empty list" { - inspect!(@list.of([1]).zip((@list.Nil : @list.T[Int])), content="None") + inspect!( + @list.of([1]).zip((@list.Nil : @list.T[Int])), + content="@list.of([])", + ) } ///|