filter(f: (int) - > bool) - > Array<int>- 모듈
- 컬렉션 →
- 수신자
- Range
- 최소 인자 수
- 1
- 최대 인자 수
- 1
Declaration and parameters
사용 조건
기본 포함 · 예
These types are provided by the language. No module import is required.
The declarations below describe the standard operations on built-in values.
수신자
Range
매개변수 제약
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.
관련 규칙 · Transform arrays, sets and ranges with callbacks →결과
The declared result type is Array<int>.
효과
실패
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.
이유
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
기대 결과
기대 결과
실행을 마칩니다. 종료 코드 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
기대 결과
기대 결과
실행을 마칩니다. 종료 코드 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
기대 결과
기대 결과
실행을 마칩니다. 종료 코드 0.
0 0 7