Skip to content

Commit 2b93717

Browse files
authored
Extend array stdlib (#839)
closes #804 This PR aims to extend the stdlib of `array`. Most of the added functions are in-place compared to their counterparts for `list`. Feel free to bike-shed. I am looking forward to your feedback. ## TODO - [x] Add complete test suit for all functions - [x] There is `indexOf`, `lastIndexOf` and `find`, probably not all of them are needed. - [x] It would be nice if someone could check the implementation `c_array_fill` as I am not too familiar with LLVM and the memory management. - [x] ~~Decide on naming convention: ruby- vs scala-style (e.g. `map!` vs `mapped`)?~~ Change everything to Ruby style - [x] Measure performance of the extern definition of `fill` compared to a definition in effekt
1 parent 4530cf1 commit 2b93717

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+697
-7
lines changed

examples/stdlib/array/all.check

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
true
2+
true
3+
false
4+
false
5+
true

examples/stdlib/array/all.effekt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
def main() = {
2+
val arr1 = array(10, 42)
3+
println(arr1.all { x => x == 42 })
4+
5+
val empty = array(0, 42)
6+
println(empty.all { x => x == 42 })
7+
8+
val arr2 = array::build(10) { x => x + 1 }
9+
println(arr2.all { x => x < 10 })
10+
println(arr2.all { x => x < 0 })
11+
println(arr2.all { x => x < 11 } == not(arr2.any { x => x >= 11 }))
12+
}

examples/stdlib/array/any.check

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
true
2+
false
3+
true
4+
true
5+
true

examples/stdlib/array/any.effekt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
def main() = {
2+
val arr1 = array(10, 42)
3+
println(arr1.any { x => x == 42 })
4+
5+
val empty = array(0, 42)
6+
println(empty.any { x => x == 42 })
7+
8+
val arr2 = array::build(10) { x => x + 1 }
9+
println(arr2.any { x => x >= 10 })
10+
println(arr2.any { x => x <= 1 })
11+
println(not(arr2.all { x => x < 11 }) == arr2.any { x => x >= 11 })
12+
13+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
not found
2+
not found
3+
found at 0
4+
found at 499
5+
found at 100
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import stringbuffer
2+
3+
def find(arr: Array[Int], elem: Int): Unit = {
4+
val optIdx = arr.binarySearch(elem) { compareInt }
5+
optIdx match {
6+
case None() => println("not found")
7+
case Some(idx) => println(s"found at ${idx.show}")
8+
}
9+
}
10+
11+
def main() = {
12+
find(fromList([]), 1)
13+
find(fromList([0]), 1)
14+
find(fromList([0]), 0)
15+
find(array(1000, 1), 1)
16+
find(array::build(1000) { x => x }, 100)
17+
}

examples/stdlib/array/count.check

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
10
2+
0
3+
0
4+
10

examples/stdlib/array/count.effekt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
def main() = {
2+
println(array(10, 1).count { x => x == 1 })
3+
println(array(10, 1).count { x => x != 1 })
4+
println(array(0, 1).count { x => x == 1 })
5+
println(array::build(20) { x => x + 1}.count { x => mod(x, 2) == 0 })
6+
}

examples/stdlib/array/drop.check

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Array()
2+
Array()
3+
true
4+
true

examples/stdlib/array/drop.effekt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
def main() = {
2+
println(array(0, "a").drop(1))
3+
println(array(1, "a").drop(1))
4+
println(array(10, 1).drop(5).size == 5)
5+
println(array(10, 1).drop(0).size == 10)
6+
}

0 commit comments

Comments
 (0)