Skip to content
TurtleKitty edited this page May 12, 2019 · 2 revisions

map

Generic mapping function. It is defined for multiple lists, multiple vectors, and single tables and sets.

(let (xs '(2 3 5))
   (proc sq (x) (* x x))
      (map sq xs))

; (4 9 25)

(let ()
   (def xs '(1 2 3))
   (def ys '(4 5 6))
   (def zs '(7 8 9))
   (proc bam! (x y z)
      (* (+ x y) z))
   (map bam! xs ys zs))

; (35 56 81)

(let (v (vector 2 3 5))
   (proc foo (x) (+ x 10))
      (map foo v))

; #(vector 12 13 15)

(let (v (vector 2 3 5))
   (proc foo (p) (+ p.head p.tail 10))
   (map foo v index: true))

; #(vector 12 14 17)

(let ()
   (def v1 (vector 1 2 3))
   (def v2 (vector 2 3 5 7 11))
   (def v3 (vector 3 4 5 7 9 11))
   (proc boom (x1 x2 x3)
      (+ x1 x2 x3))
      (map boom v1 v2 v3))

; #(vector 6 9 13)

(let (t (: foo 2 bar 3 baz 5))
   (def f
      (lambda (kv)
         %($kv.head . $(math.pow kv.tail 3))))
   (map f t))

;  #(table foo 8 bar 27 baz 125))

(let (s (set 2 3 5))
   (def f (lambda (v) (+ 10 v)))
   (map f s))

; #(set 12 13 15)
Clone this wiki locally