diff --git a/libraries/js/immutable/list.effekt b/libraries/js/immutable/list.effekt index 5caa4ae607..a04285ef86 100644 --- a/libraries/js/immutable/list.effekt +++ b/libraries/js/immutable/list.effekt @@ -20,6 +20,14 @@ def foreach[A](l: List[A]) { f: A => Unit } : Unit = { } } +def foreachIndex[T](list: List[T], i: Int){ f: (Int, T) => Unit}: Unit = { + list match { + case Nil() => (); + case Cons(head, tail) => f(i, head); foreachIndex(tail, i+1){f} + + } +} + def map[A, B](l: List[A]) { f: A => B } : List[B] = { var acc = Nil[B]() l.foreach { el => acc = Cons(f(el), acc) } diff --git a/libraries/js/mutable/array.effekt b/libraries/js/mutable/array.effekt index a8f895c346..1750ba3cff 100644 --- a/libraries/js/mutable/array.effekt +++ b/libraries/js/mutable/array.effekt @@ -24,10 +24,13 @@ def initArray[T](size: Int) { index: Int => T}: Array[T] = { def initArray[T](list: List[T]): Array[T] = { val listSize = list.size(); val arr = emptyArray(listSize); - var i = 0; - each(0,list.size()) + + foreachList(list, 0){(i, head) => put(arr, i, head)} + return arr; } + + extern pure def emptyArray[T](initialSize: Int): Array[T] = "(new Array(initialSize))"