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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With