Arc Forumnew | comments | leaders | submitlogin
2 points by waterhouse 5133 days ago | link | parent

Function: (mapn f a b . xs) : "map n". Primary usage: (mapn f a b) = (map f (range a b)). Extra argument pairs will yield nested applications of 'mapn, with f applied to a kind of product of all ranges. The implementation and a demonstration will make this clearer:

  (def mapn (f a b . xs)
    (if no.xs
        (map f (range a b))
        (mapn [apply mapn (fn args (apply f _ args))
                     xs]
              a b)))

  arc> (mapn square 1 10)
  (1 4 9 16 25 36 49 64 81 100)
  arc> (mapn list 1 3 20 22)
  (((1 20) (1 21) (1 22)) ((2 20) (2 21) (2 22)) ((3 20) (3 21) (3 22)))
'mapn is very nice for testing out a function on a numerical range, especially repeatedly. Also, given the 'grid function, we can construct two-dimensional tables of a function extremely easily:

  arc> (grid:mapn * 1 7 1 7)
  1  2  3  4  5  6  7
  2  4  6  8 10 12 14
  3  6  9 12 15 18 21
  4  8 12 16 20 24 28
  5 10 15 20 25 30 35
  6 12 18 24 30 36 42
  7 14 21 28 35 42 49
  nil
  arc> (grid:mapn choose 0 7 0 7)
  1 0  0  0  0  0 0 0
  1 1  0  0  0  0 0 0
  1 2  1  0  0  0 0 0
  1 3  3  1  0  0 0 0
  1 4  6  4  1  0 0 0
  1 5 10 10  5  1 0 0
  1 6 15 20 15  6 1 0
  1 7 21 35 35 21 7 1
  nil