Skip to content

Commit

Permalink
init array for immutable list
Browse files Browse the repository at this point in the history
  • Loading branch information
IR0NSIGHT committed Oct 25, 2023
1 parent b18ba0d commit 8ae3471
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
8 changes: 8 additions & 0 deletions libraries/js/immutable/list.effekt
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand Down
7 changes: 5 additions & 2 deletions libraries/js/mutable/array.effekt
Original file line number Diff line number Diff line change
Expand Up @@ -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))"

Expand Down

0 comments on commit 8ae3471

Please sign in to comment.