2
votes

I am learning clojure and I am having problem understanding this clojure code, so I have this partial function

 (def add-five (partial + 5))

When I run,

(add-five 2)# I get 7
(add-five 2 5) # I get 12

For first, I give one argument; For second, I give two arguments.

(map add-five [1 2 3 4 5])

this gives me

(6 7 8 9 10)

Here, I am assuming, add-five is being applied element of the list. However when I run,

(reduce add-five [1 2 3 4 5])

, I have no idea what is happening?

 (reduce add-five [0]) #gives me zero
 (reduce add-five [0 0]) #gives me five

Can someone explain what happens what I run the reduce like above?

1

1 Answers

6
votes
 (reduce add-five [0]) #gives me zero

For this line this rule from the documentation is applied:

If coll has only 1 item, it is returned and f is not called

Otherwise it applies the add-five function to 0 and 0 arguments: (add-five 0 0)