Passing functions as arguments to clojure

I have a function that takes a function and a number and returns a function application on a number and a cubic function:

(defn something [fn x]
  (fn x))

(defn cube [x]
  (* x x x))

When I call the function as follows, it works:

(something cube 4)

but this returns an error:

(something Math/sin 3.14)

However, this works:

(something #(Math/sin %) 3.14)

What is the explanation?

+5
source share
1 answer

Math.sinnot a function! This method is direct from Java and does not understand the various rules that Clojure functions must follow. If you wrap it in a function, then this function can act as a proxy server, passing arguments to the mute method and returning the results to your smart, function-oriented context.

+13
source

All Articles