@@ -460,25 +460,20 @@ pub fn fold_righti[A, B](self : T[A], f : (Int, A, B) -> B, init~ : B) -> B {
460
460
461
461
///|
462
462
/// 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 .
464
464
///
465
465
/// # Example
466
466
///
467
467
/// ```
468
468
/// 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 ))
480
476
}
481
- res .map (T ::rev)
482
477
}
483
478
484
479
///|
0 commit comments