Skip to content

Commit

Permalink
tweak immut/list.T::to_array to not use O(n) length func
Browse files Browse the repository at this point in the history
  • Loading branch information
hackwaly authored and bobzhang committed Dec 12, 2024
1 parent f85055e commit 3caee0f
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions immut/list/list.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,15 @@ pub fn rev_map[A, B](self : T[A], f : (A) -> B) -> T[B] {
pub fn to_array[A](self : T[A]) -> Array[A] {
match self {
Nil => []
Cons(head, _) => {
let arr = Array::make(self.length(), head)
self.eachi(fn(i, x) { arr[i] = x })
Cons(x, xs) => {
let arr = [x]
loop xs {
Nil => ()
Cons(x, xs) => {
arr.push(x)
continue xs
}
}
arr
}
}
Expand Down

0 comments on commit 3caee0f

Please sign in to comment.