El Diego Efe
El Diego Efe

Reputation: 215

Implementation of comp

I'm trying to understand how this function work, which is an implementation of comp (from the chapter "Functional Programming" in the book Clojure for the Brave and True):

(defn two-comp
  [f g]
  (fn [& args]
    (f (apply g args))))

The idea is that it takes two functions and apply them to args. What I don't understand is how the args reach the anonymous function, since they are not entered as arguments to two-comp? How can be two-comp used this way?

Upvotes: 1

Views: 279

Answers (1)

mipadi
mipadi

Reputation: 410652

two-comp returns an anonymous function, which in turn takes args. Look at the body of two-comp:

(fn [& args]
  (f (apply g args)))

fn creates a function, and that function definition follows. The return value of fn is what is returned from two-comp.

Upvotes: 4

Related Questions