Skip to content

Commit d3aae6b

Browse files
feat(std/array): Add array_filled (#783)
* feat(std): array_filled * Remove test for array_filled * Add default value to `array_filled` * Change description of `array_filled` * Change comments to triple slash * Apply suggestions from code review Change type tests for stronger assertion. Co-authored-by: Paweł Karaś <pkaras.it@gmail.com> --------- Co-authored-by: Paweł Karaś <pkaras.it@gmail.com>
1 parent 8320e77 commit d3aae6b

File tree

8 files changed

+94
-0
lines changed

8 files changed

+94
-0
lines changed

src/std/array.ab

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,15 @@ pub fun array_shift(ref array) {
8686
array = array[1..length]
8787
return element
8888
}
89+
90+
/// Returns an array of length `size` with each element set to `value`; if `size`
91+
/// is less than zero or not of type `Int`, an empty array is returned
92+
pub fun array_filled(size, value = 0) {
93+
let array = [value]
94+
array = array[0..0]
95+
if not (size is Int):
96+
return array
97+
for _ in 0..(size >= 0 then size else 0):
98+
array += [value]
99+
return array
100+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { array_filled } from "std/array"
2+
3+
// Output
4+
// [0 0 0]
5+
// [0 0 0]
6+
// [.5 .5 .5]
7+
// [a a a]
8+
// [0 0 0]
9+
// [ ]
10+
11+
main {
12+
echo "[{array_filled(3)}]"
13+
echo "[{array_filled(3, 0)}]"
14+
echo "[{array_filled(3, .5)}]"
15+
echo "[{array_filled(3, "a")}]"
16+
echo "[{array_filled(3, false)}]"
17+
echo "[{array_filled(3, null)}]"
18+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { array_filled } from "std/array"
2+
3+
// Output
4+
// Array length for size=-1: 0
5+
// Array length for size=0: 0
6+
// Array length for size=1: 1
7+
// Array length for size=2: 2
8+
// Array length for size=3: 3
9+
10+
fun test_size(size: Int): Null {
11+
let array = array_filled(size, 0)
12+
echo "Array length for size={size}: {len(array)}"
13+
}
14+
15+
main {
16+
for i in -1..=3 {
17+
test_size(i)
18+
}
19+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { array_filled } from "std/array"
2+
3+
main {
4+
let array = array_filled(0, true)
5+
if array == [Bool]:
6+
echo "Succeeded"
7+
else:
8+
echo "Failed"
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { array_filled } from "std/array"
2+
3+
main {
4+
let array = array_filled(0, 1)
5+
if array == [Int]:
6+
echo "Succeeded"
7+
else:
8+
echo "Failed"
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { array_filled } from "std/array"
2+
3+
main {
4+
let array = array_filled(0, null)
5+
if array == [Null]:
6+
echo "Succeeded"
7+
else:
8+
echo "Failed"
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { array_filled } from "std/array"
2+
3+
main {
4+
let array = array_filled(0, 1.0)
5+
if array == [Num]:
6+
echo "Succeeded"
7+
else:
8+
echo "Failed"
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { array_filled } from "std/array"
2+
3+
main {
4+
let array = array_filled(0, "")
5+
if array == [Text]:
6+
echo "Succeeded"
7+
else:
8+
echo "Failed"
9+
}

0 commit comments

Comments
 (0)