map

map items in collection to values returned from given action


Parameters

map collection :integer :string :literal :dictionary :object :inline :block :range
    params :null :literal :block
    action :block :bytecode

Attributes

with :literal use given index
parallel :integer run each item in its own cooperative fiber; integer caps the number of in-flight fibers

Returns

  • :block
  • or  :nothing

Examples

copy
print map 1..5 [x][ 2*x ] ; 2 4 6 8 10

copy
arr: 1..5 map 'arr 'x -> 2*x print arr ; 2 4 6 8 10

copy
map 1..6 [x y][ print ["mapping" x "and" y "->" x+y] x+y ] ; mapping 1 and 2 -> 3 ; mapping 3 and 4 -> 7 ; mapping 5 and 6 -> 11 ; => [3 7 11]

copy
map.with:'i ["one" "two" "three" "four"] 'x [ (even? i)? -> upper x -> x ] ; => ["ONE" "two" "THREE" "four"]

copy
; .parallel, fan out one cooperative fiber per item. ; Bare flag = unbounded; integer = sliding-window cap. results: map.parallel 1..5 'x [ pause 100 x*2 ] ; 5 fibers run concurrently; ~100ms total instead of ~500ms ; ⚠ each fiber gets a SHALLOW COPY of parent symbols; ; writes don't leak back. Return values; don't mutate ; outer state inside the body.

Related