Iter.filter
func Iter.filter[T](s: Seq[T], f: func(T): bool): Seq[T]Iter.filter returns a sequence of the elements of s that f accepts.
Details
- Lazy:
Iter.filterruns nothing when called.fruns only whenfororIter.toArrayruns the result, once per element the source yields. - The kept elements come out in the order of the source.
- When the consumer stops early, as
Iter.takeor abreakdoes, the source stops too, andfis not called on the elements after. - Each run of the result runs the source again and calls
fagain.
Basic examples
Keep the multiples of 3 below 10:
Iter.filter(Iter.range(0, 10), { x % 3 == 0 }) | Iter.toArray() // [0, 3, 6, 9]Keep the multiples of 7 below 30 with a pipe:
Iter.range(0, 30) | Iter.filter({ x % 7 == 0 }) | Iter.toArray() // [0, 7, 14, 21, 28]Loop over the elements a predicate accepts:
for n in Iter.range(1, 20) | Iter.filter({ x % 5 == 0 }) {
println(n)
}5
10
15Scope
Filter the elements of an array as a sequence:
Iter.fromArray(["ant", "beetle", "fly"]) | Iter.filter({ x.length() > 3 }) | Iter.toArray() // ["beetle"]An empty sequence gives an empty sequence, and so does a predicate that accepts nothing:
Iter.range(5, 5) | Iter.filter({ x > 0 }) | Iter.toArray() // []
Iter.range(0, 5) | Iter.filter({ false }) | Iter.toArray() // []Test a record through its fields:
type Task = { title: string, done: bool }
let tasks = [Task{ title: "write", done: true }, Task{ title: "test", done: false }]
Iter.fromArray(tasks) | Iter.filter({ !x.done }) | Iter.toArray() // [Task{ title: "test", done: false }]Pass a named function or a function on a type instead of a lambda:
func isOdd(n: int): bool { n % 2 == 1 }
Iter.range(0, 8) | Iter.filter(isOdd) | Iter.toArray() // [1, 3, 5, 7]
Iter.fromArray(["a", "", "b"]) | Iter.filter(String.isEmpty) | Iter.toArray() // [""]Call it through . on a sequence:
Iter.range(0, 10).filter({ x > 7 }).toArray() // [8, 9]Filter a sequence you wrote yourself, here an endless one, and stop it with Iter.take:
func naturals(): Seq[int] {
func(yield: func(int): bool) {
var n = 1
while true {
if !yield(n) { return }
n += 1
}
}
}
naturals() | Iter.filter({ x % 3 == 0 }) | Iter.take(4) | Iter.toArray() // [3, 6, 9, 12]Applications
Find the first five primes without choosing an upper bound:
func naturals(): Seq[int] {
func(yield: func(int): bool) {
var n = 1
while true {
if !yield(n) { return }
n += 1
}
}
}
func isPrime(n: int): bool {
if n < 2 { return false }
var d = 2
while d * d <= n {
if n % d == 0 { return false }
d += 1
}
true
}
println(naturals() | Iter.filter(isPrime) | Iter.take(5) | Iter.toArray())[2, 3, 5, 7, 11]Select and then transform, without building an array in between:
type User = { name: string, active: bool }
let users = [
User{ name: "Ann", active: true },
User{ name: "Bob", active: false },
User{ name: "Cid", active: true },
]
Iter.fromArray(users).filter({ x.active }).map({ x.name }).toArray() // ["Ann", "Cid"]Stop at the first match with break; the predicate sees nothing after it:
let checked: Array[int] = []
for n in Iter.range(10, 100) | Iter.filter({ checked.push(x); x % 13 == 0 }) {
println(n)
break
}
println(checked)13
[10, 11, 12, 13]Properties and relations
On an array, filtering the sequence gives what Array.filter gives:
let xs = [5, 8, 1, 9, 4]
let f: func(int): bool = { x > 4 }
assert((Iter.fromArray(xs) | Iter.filter(f) | Iter.toArray()) == Array.filter(xs, f))The predicate does not run until the result runs, and then only as far as the consumer asks:
let calls: Array[int] = []
let evens = Iter.range(0, 10) | Iter.filter({ calls.push(x); x % 2 == 0 })
calls // []
evens | Iter.take(2) | Iter.toArray() // [0, 2]
calls // [0, 1, 2]filter and map run element by element, interleaved, not one pass after the other:
let log: Array[string] = []
let kept = Iter.range(1, 5) | Iter.filter({ log.push("f${x}"); x % 2 == 0 })
kept | Iter.map({ log.push("m${x}"); x * 10 }) | Iter.toArray() // [20, 40]
log // ["f1", "f2", "m2", "f3", "f4", "m4"]Possible issues
Running the result twice runs the predicate twice. Collect it once with Iter.toArray when f is costly or has effects:
var n = 0
let evens = Iter.range(0, 4).filter({ n += 1; x % 2 == 0 })
evens.toArray() // [0, 2]
evens.toArray() // [0, 2]
n // 8On an array, .filter is Array.filter, which runs f at once and gives an array. On a sequence it is Iter.filter, which runs nothing until the result runs:
[1, 2, 3].filter({ x > 1 }) // [2, 3]
Iter.fromArray([1, 2, 3]).filter({ x > 1 }).toArray() // [2, 3]Iter.filter takes a sequence, not an array. Wrap an array with Iter.fromArray, or call Array.filter:
let big = Iter.filter([1, 2, 3], { x > 1 }) // error: cannot use Array[int] where Seq[T] is expectedThe predicate must return a bool. Ant has no truthy values, so an int is a compile error:
let nonZero = Iter.range(0, 3).filter({ x }) // error: cannot use int where bool is expectedThe result keeps the element type of the source. Filtering out null gives a Seq[int | null], not a Seq[int]:
let scores: Array[int | null] = [90, null, 75]
let ints: Array[int] = Iter.fromArray(scores).filter({ x != null }).toArray()
// error: cannot use Array[int | null] where Array[int] is expectedNarrow in a loop instead, where the test on the element narrows it to int:
let scores: Array[int | null] = [90, null, 75]
var total = 0
for s in Iter.fromArray(scores) {
if s != null { total += s }
}
println(total)165See also
Iter.map, Iter.take, Iter.range, Iter.fromArray, Iter.toArray, Array.filter, Iter
