You use apply to transform a function that works with multiple arguments, which works with a single sequence of arguments. You can also insert arguments before a sequence. For example, map can work with several sequences. This example (from ClojureDocs ) uses map to transfer the matrix.
user=> (apply map vector [[:a :b] [:c :d]]) ([:a :c] [:b :d])
One argument inserted here is vector . Thus, apply expands to
user=> (map vector [:a :b] [:c :d])
Cute!
PS To return a vector of vectors instead of a sequence of vectors, wrap it all in vec :
user=> (vec (apply map vector [[:a :b] [:c :d]]))
While we are here, vec can be defined as (partial apply vector) , although it is not.
Regarding Lisp -1 and Lisp -2: 1 and 2, indicate the number of things that a name can denote in a given context. In Lisp -2, you can have two different things (function and variable) with the same name. So, wherever possible, you should decorate your program with something to indicate what you mean. Fortunately, Clojure (or Scheme ...) allows you to designate only one thing, so such decorations are not needed.
Thumbnail Dec 17 '13 at 15:50 2013-12-17 15:50
source share