Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clojure: map map

I would like to use the map function on map. But I can't get it to work.

A toy example:

(map map [+ - *] [1 2 3] [4 5 6] [7 8 9])

I expect a result like (12 15 18) but all I get is an error.

Thanks.

like image 217
user1992634 Avatar asked Dec 11 '22 04:12

user1992634


1 Answers

As an alternative to already existing answers, you could replace the outer map with a list comprehension, which is more readable than a nested map IMHO:

user=> (defn fun [ops & args]
  #_=>     (for [op ops]
  #_=>         (apply map op args)))
#'user/fun

user=> (fun [+ - *] [1 2 3] [4 5 6] [7 8 9])
((12 15 18) (-10 -11 -12) (28 80 162))
like image 60
sloth Avatar answered Jan 23 '23 06:01

sloth