Question

new to clojure, so maybe I am going about this totally the wrong way and all, but is there a way to do this type of thing?

(map (cycle [+ -]) [1 1 1] [1 1 1])

I would want it to return, in this example; (2 0 2)

Thanks to the help below, the solution is:

(map (fn [a b c] (a b c)) (cycle [+ -]) [1 1 1] [1 2 3])

or

(map #(%1 %2 %3) (cycle [+ -]) [1 1 1] [1 2 3])

for short

Was it helpful?

Solution

Yes -

(map #(%1 %2 %3) (cycle [+ -]) [1 1 1] [1 1 1])
;;(2 0 2)

# is a short hand for an anonymous function and % is the number of the argument passed. So you are just running map against
(+ 1 1),(- 1 1),(+ 1 1)

OTHER TIPS

The first argument to map needs to be a function that will be applied to each element of the collections. What you have instead is a vector of functions. So what you need is a function that will apply each element of the vector to the corresponding elements in the other vectors. Like so:

(map #(%1 %2 %3) (cycle [+ -]) [1 1 1] [1 1 1])
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top