Skip to content

Commit b7a1f17

Browse files
illusory0x0bobzhang
authored andcommitted
perf(list): use loop directly avoid rev
1 parent 7074415 commit b7a1f17

1 file changed

Lines changed: 14 additions & 8 deletions

File tree

list/list.mbt

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -635,15 +635,21 @@ pub fn is_empty[A](self : T[A]) -> Bool {
635635
/// assert_eq!(b, @list.from_array([2, 4, 6]))
636636
/// ```
637637
pub fn unzip[A, B](self : T[(A, B)]) -> (T[A], T[B]) {
638-
let mut xs = Empty
639-
let mut ys = Empty
640-
// implemented with loop to avoid stack overflow
641-
loop self.rev() {
642-
Empty => break (xs, ys)
638+
match self {
639+
Empty => (Empty, Empty)
643640
More((x, y), tail~) => {
644-
xs = More(x, tail=xs)
645-
ys = More(y, tail=ys)
646-
continue tail
641+
let xs = More(x, tail=Empty)
642+
let ys = More(y, tail=Empty)
643+
loop tail, xs, ys {
644+
Empty, _, _ => ()
645+
More((x, y), tail~), More(_) as xptr, More(_) as yptr => {
646+
xptr.tail = More(x, tail=Empty)
647+
yptr.tail = More(y, tail=Empty)
648+
continue tail, xptr.tail, yptr.tail
649+
}
650+
_, _, _ => abort("unreachable")
651+
}
652+
(xs, ys)
647653
}
648654
}
649655
}

0 commit comments

Comments
 (0)