본문으로 건너뛰기
토파즈문서
6.05.21

문서 검색

명세 6.0

방향키로 결과를 고르고 Enter를 눌러 여세요.

API 이름, 진단 번호, 규칙 또는 프로젝트 주제를 입력하세요.

    다운로드 →

    라이브러리 · 컬렉션

    Range.map

    Call a function once per element in iteration order and collect the results in an array.

    map(f: (int) -> U) -> Array<U>
    수신자
    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.

    결과

    The declared result type is Array<U>.

    효과

    실패

    Result and effects

    Each element of type int passes to the function once. The results keep the receiver's iteration order.

    Related entries

    Collections →

    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

    main.tpzObserve one pass per array method플레이그라운드 열기 →
    1. import std.io
    2. function scaled(value: int) -> int {
    3. io.writeLine("map {value}")
    4. value * 10
    5. }
    6. function kept(value: int) -> bool {
    7. io.writeLine("filter {value}")
    8. value != 2
    9. }
    10. let values = [1, 2, 3]
    11. let mapped = values.map(scaled)
    12. let selected = values.filter(kept)
    13. let total = values.reduce(100, (sum: int, value: int) => {
    14. io.writeLine("reduce {sum} {value}")
    15. sum - value
    16. })
    17. io.writeLine("{mapped[0]} {mapped[1]} {mapped[2]}")
    18. io.writeLine("{selected.length} {selected[0]} {selected[1]}")
    19. io.writeLine("{total}")
    기대 출력종료 코드 0
    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
    
    $ topaz run main.tpz · 대상 제품 5.21.3
    기대 결과

    기대 결과

    실행을 마칩니다. 종료 코드 0.

    기대 표준 출력
    1. map 1
    2. map 2
    3. map 3
    4. filter 1
    5. filter 2
    6. filter 3
    7. reduce 100 1
    8. reduce 99 2
    9. reduce 97 3
    10. 10 20 30
    11. 2 1 3
    12. 94

    Transform sets and ranges into arrays

    main.tpzTransform sets and ranges into arrays플레이그라운드 열기 →
    1. import std.io
    2. let marks = set { 3, 1, 2 }
    3. let doubled: Array<int> = marks.map((value: int) => value * 2)
    4. let large: Array<int> = marks.filter((value: int) => value > 1)
    5. let digits = marks.reduce("", (text: string, value: int) => text + "{value}")
    6. io.writeLine("{doubled[0]} {doubled[1]} {doubled[2]}")
    7. io.writeLine("{large.length} {large[0]} {large[1]}")
    8. io.writeLine(digits)
    9. let squares: Array<int> = (0..<4).map((value: int) => value * value)
    10. let even: Array<int> = (1..6).filter((value: int) => value % 2 == 0)
    11. let number = (1..4).reduce(0, (total: int, value: int) => total * 10 + value)
    12. io.writeLine("{squares.length} {squares[3]}")
    13. io.writeLine("{even.length} {even[0]} {even[2]}")
    14. io.writeLine("{number}")
    기대 출력종료 코드 0
    6 2 4
    2 3 2
    312
    4 9
    3 2 6
    1234
    
    $ topaz run main.tpz · 대상 제품 5.21.3
    기대 결과

    기대 결과

    실행을 마칩니다. 종료 코드 0.

    기대 표준 출력
    1. 6 2 4
    2. 2 3 2
    3. 312
    4. 4 9
    5. 3 2 6
    6. 1234

    Return empty arrays and the initial value for empty receivers

    main.tpzReturn empty arrays and the initial value for empty receivers플레이그라운드 열기 →
    1. import std.io
    2. function never(value: int) -> int {
    3. io.writeLine("called {value}")
    4. value
    5. }
    6. let none: Array<int> = []
    7. let nothing: Set<int> = set {}
    8. let mapped = none.map(never)
    9. let selected = nothing.filter((value: int) => never(value) > 0)
    10. let total = (5..<5).reduce(7, (sum: int, value: int) => sum + never(value))
    11. io.writeLine("{mapped.length} {selected.length} {total}")
    기대 출력종료 코드 0
    0 0 7
    
    $ topaz run main.tpz · 대상 제품 5.21.3
    기대 결과

    기대 결과

    실행을 마칩니다. 종료 코드 0.

    기대 표준 출력
    1. 0 0 7