Skip to content

Commit 29bc8d3

Browse files
committed
align @immut/list.zip with @array.zip
1 parent d9c8b4c commit 29bc8d3

File tree

4 files changed

+17
-20
lines changed

4 files changed

+17
-20
lines changed

immut/list/list.mbt

+8-13
Original file line numberDiff line numberDiff line change
@@ -460,25 +460,20 @@ pub fn fold_righti[A, B](self : T[A], f : (Int, A, B) -> B, init~ : B) -> B {
460460
461461
///|
462462
/// Zip two lists.
463-
/// If the lists have different lengths, it will return None.
463+
/// If the lists have different lengths, it will return a list with shorter length.
464464
///
465465
/// # Example
466466
///
467467
/// ```
468468
/// let r = @list.zip(@list.of([1, 2, 3, 4, 5]), @list.of([6, 7, 8, 9, 10]))
469-
/// assert_eq!(r, Some(@list.from_array([(1, 6), (2, 7), (3, 8), (4, 9), (5, 10)])))
470-
/// ```
471-
pub fn zip[A, B](self : T[A], other : T[B]) -> T[(A, B)]? {
472-
let mut acc = Nil
473-
let res = loop self, other {
474-
Nil, Nil => break Some(acc)
475-
Cons(x, xs), Cons(y, ys) => {
476-
acc = Cons((x, y), acc)
477-
continue xs, ys
478-
}
479-
_, _ => break None
469+
/// assert_eq!(r, @list.from_array([(1, 6), (2, 7), (3, 8), (4, 9), (5, 10)]))
470+
/// ```
471+
pub fn zip[A, B](self : T[A], other : T[B]) -> T[(A, B)] {
472+
match (self, other) {
473+
(Nil, _) => Nil
474+
(_, Nil) => Nil
475+
(Cons(x, xs), Cons(y, ys)) => Cons((x, y), zip(xs, ys))
480476
}
481-
res.map(T::rev)
482477
}
483478
484479
///|

immut/list/list.mbti

+2-2
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ fn unsafe_nth[A](T[A], Int) -> A
159159

160160
fn unzip[A, B](T[(A, B)]) -> (T[A], T[B])
161161

162-
fn zip[A, B](T[A], T[B]) -> T[(A, B)]?
162+
fn zip[A, B](T[A], T[B]) -> T[(A, B)]
163163

164164
// Types and methods
165165
pub(all) enum T[A] {
@@ -249,7 +249,7 @@ impl T {
249249
unsafe_minimum[A : Compare](Self[A]) -> A
250250
unsafe_nth[A](Self[A], Int) -> A
251251
unzip[A, B](Self[(A, B)]) -> (Self[A], Self[B])
252-
zip[A, B](Self[A], Self[B]) -> Self[(A, B)]?
252+
zip[A, B](Self[A], Self[B]) -> Self[(A, B)]
253253
}
254254
impl[A] Add for T[A]
255255
impl[X] Default for T[X]

immut/list/list_test.mbt

+6-3
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ test "zip" {
215215
let rs = @list.of([6, 7, 8, 9, 10])
216216
inspect!(
217217
ls.zip(rs),
218-
content="Some(@list.of([(1, 6), (2, 7), (3, 8), (4, 9), (5, 10)]))",
218+
content="@list.of([(1, 6), (2, 7), (3, 8), (4, 9), (5, 10)])",
219219
)
220220
}
221221

@@ -607,13 +607,16 @@ test "List::zip with lists of equal length" {
607607
let list1 = @list.of([1, 2, 3])
608608
let list2 = @list.of(["a", "b", "c"])
609609
let zipped = list1.zip(list2)
610-
let expected = Some(@list.of([(1, "a"), (2, "b"), (3, "c")]))
610+
let expected = @list.of([(1, "a"), (2, "b"), (3, "c")])
611611
assert_eq!(zipped, expected)
612612
}
613613

614614
///|
615615
test "@list.zip with empty list" {
616-
inspect!(@list.of([1]).zip((@list.Nil : @list.T[Int])), content="None")
616+
inspect!(
617+
@list.of([1]).zip((@list.Nil : @list.T[Int])),
618+
content="@list.of([])",
619+
)
617620
}
618621

619622
///|

immut/list/moon.pkg.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
"moonbitlang/core/builtin",
44
"moonbitlang/core/array",
55
"moonbitlang/core/quickcheck",
6-
"moonbitlang/core/json",
7-
"moonbitlang/core/option"
6+
"moonbitlang/core/json"
87
],
98
"targets": {
109
"panic_test.mbt": ["not", "native"]

0 commit comments

Comments
 (0)