Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use map with function with more than one parameter

Tags:

clojure

I have the following method:

(defn area [x y] (* x y))

How do I iterate through a list with respect to the parameters number. Something like

(map area [2 5 6 6])

so it will make calculations like (area 2 5) and (area 6 6), maybe vector is not the proper type to use.

like image 374
Vestimir Markov Avatar asked Mar 27 '26 11:03

Vestimir Markov


1 Answers

You can use partition as some have suggested here but you might want to consider arranging the data differently. For example you could use a vector of vectors:

[[2 5] [6 6]]

Then you can change your area function to:

(defn area [[x y]] (* x y))

Now you can call that with one of your pairs: (area [6 6]) and mapping over your vector is easy:

(map area [[2 5] [6 6]])

If for some reason you need area to take two parameters instead of a vector you can do something like this:

(map #(apply area %) [[2 5] [6 6]])

To me that's still simpler than using partition.

like image 118
sethev Avatar answered Apr 02 '26 04:04

sethev