lang: core: iter: Add filter iterator function

This was fun to write and adds a new core iterator function.
This commit is contained in:
James Shubin
2024-07-03 21:25:19 -04:00
parent e747e12002
commit aa03b5ce2f
3 changed files with 489 additions and 0 deletions

View File

@@ -0,0 +1,14 @@
import "iter"
import "math"
$fn1 = func($x) {
math.mod($x, 2) == 0 # is even?
}
$in1 = [8, -1, 0, 2, 4, 5, 13,]
$out1 = iter.filter($in1, $fn1)
$t1 = template("out1: {{ . }}", $out1)
test [$t1,] {}

View File

@@ -0,0 +1,14 @@
import "iter"
import "math"
$fn1 = func($x) {
math.mod(len($x), 2) == 0 # is length even ?
}
$in1 = ["xxxxxx", "a", "bb", "ccc", "dddd", "eeeee",]
$out1 = iter.filter($in1, $fn1)
$t1 = template("out1: {{ . }}", $out1)
test [$t1,] {}