filter(f: (T) - > bool) - > Array<T>- Module
- Collections →
- Receiver
- Array<T>
- Minimum arity
- 1
- Maximum arity
- 1
Declaration and parameters
Availability
Included by default · yes
These types are provided by the language. No module import is required.
The declarations below describe the standard operations on built-in values.
Receiver
Array<T>
Parameter constraints
Array<T>, Set<T> and Range values provide map, filter and reduce. A range supplies int elements.
Each call makes one complete pass over the receiver in its iteration order. Arrays use element order. Sets use insertion order. Ranges use the order of their bounds and step.
map(f) calls f once per element and returns an array of the results in that order.
filter(f) calls f once per element and returns an array of the elements for which f returned true, in that order. Set and Range receivers also produce an array.
reduce(initial, f) starts with initial and calls f with the current accumulator and each element in order. It returns the last accumulator. An empty receiver returns initial without calling f.
Receiver and callback evaluation, chaining and callback propagation follow the callback and comprehension rule.
Related rule · Transform arrays, sets and ranges with callbacks →Result
The declared result type is Array<T>.
Effects
Failures
Result and effects
The predicate runs once per element. The result is an array for every receiver kind.
Related entries
Transform arrays, sets and ranges with callbacks
Transform arrays, sets and ranges with callbacks
Array<T>, Set<T> and Range values provide map, filter and reduce. A range supplies int elements.
Each call makes one complete pass over the receiver in its iteration order. Arrays use element order. Sets use insertion order. Ranges use the order of their bounds and step.
map(f) calls f once per element and returns an array of the results in that order.
filter(f) calls f once per element and returns an array of the elements for which f returned true, in that order. Set and Range receivers also produce an array.
reduce(initial, f) starts with initial and calls f with the current accumulator and each element in order. It returns the last accumulator. An empty receiver returns initial without calling f.
Receiver and callback evaluation, chaining and callback propagation follow the callback and comprehension rule.
Why
A for loop and a comprehension already accept arrays, sets and ranges in the same way. Callback methods accept the same receivers, so the choice between a callback chain and a comprehension follows intent and not the receiver type. map and filter return arrays for every receiver, so a chain has one result shape.
Observe one pass per array method
import std.iofunction scaled(value: int) -> int {io.writeLine("map {value}")value * 10}function kept(value: int) -> bool {io.writeLine("filter {value}")value != 2}let values = [1, 2, 3]let mapped = values.map(scaled)let selected = values.filter(kept)let total = values.reduce(100, (sum: int, value: int) => {io.writeLine("reduce {sum} {value}")sum - value})io.writeLine("{mapped[0]} {mapped[1]} {mapped[2]}")io.writeLine("{selected.length} {selected[0]} {selected[1]}")io.writeLine("{total}")
map 1
map 2
map 3
filter 1
filter 2
filter 3
reduce 100 1
reduce 99 2
reduce 97 3
10 20 30
2 1 3
94
Expected result
Expected result
Execution completes. Exit code 0.
map 1map 2map 3filter 1filter 2filter 3reduce 100 1reduce 99 2reduce 97 310 20 302 1 394
Transform sets and ranges into arrays
import std.iolet marks = set { 3, 1, 2 }let doubled: Array<int> = marks.map((value: int) => value * 2)let large: Array<int> = marks.filter((value: int) => value > 1)let digits = marks.reduce("", (text: string, value: int) => text + "{value}")io.writeLine("{doubled[0]} {doubled[1]} {doubled[2]}")io.writeLine("{large.length} {large[0]} {large[1]}")io.writeLine(digits)let squares: Array<int> = (0..<4).map((value: int) => value * value)let even: Array<int> = (1..6).filter((value: int) => value % 2 == 0)let number = (1..4).reduce(0, (total: int, value: int) => total * 10 + value)io.writeLine("{squares.length} {squares[3]}")io.writeLine("{even.length} {even[0]} {even[2]}")io.writeLine("{number}")
6 2 4
2 3 2
312
4 9
3 2 6
1234
Expected result
Expected result
Execution completes. Exit code 0.
6 2 42 3 23124 93 2 61234
Return empty arrays and the initial value for empty receivers
import std.iofunction never(value: int) -> int {io.writeLine("called {value}")value}let none: Array<int> = []let nothing: Set<int> = set {}let mapped = none.map(never)let selected = nothing.filter((value: int) => never(value) > 0)let total = (5..<5).reduce(7, (sum: int, value: int) => sum + never(value))io.writeLine("{mapped.length} {selected.length} {total}")
0 0 7
Expected result
Expected result
Execution completes. Exit code 0.
0 0 7