Contents    Page-10    Prev    Next    Page+10    Index   

Map

The map function applies a given function to each element of a list, producing a new list of the results:

(map function list )


(map symbol? '(2 medium 7 large))
    ->  (false true false true)


(map (fn [x] (* 2 x)) '(2 3 7))
    ->  (4 6 14)

Note that this example used an anonymous function:

(fn [ args ] code )

This is useful when the function is small and not worth giving it a name as a separate function.

We could define length (not very efficiently) by mapping a list to a list of 1's, which we add using reduce and +:


(defn length [x]
  (reduce + (map (fn [z] 1) x)))

 (length '(2 medium 3 large))
    ->  (1 1 1 1)    ->  4