An almost curry example from Clojure Programming

In this code

(defn faux-curry [& args] (apply partial partial args)) 

as I should understand this part :

 (apply partial partial args) 

In my understanding, "partial" takes a function and some values, and then returns a new function with fixed fixed source function variables. Does he first apply “partial” to ... a second “partial” that does something with arguments? Any better way to understand?

+4
source share
1 answer

Manual evaluation using sample arguments can help:

 (apply partial partial [+ 1 2 3]) ; 1. ; => (partial partial + 1 2 3) ; 2. ; => (fn [& args] (apply partial + 1 2 (concat [3] args))) ; 3. 

We replace the sample arguments in the faux-curry body with 1; then applying apply manually in 2; then applying the first partial manually in 3. (Note that [3] in 3. there will actually be a seq of “remaining arguments” to the external partial in the compiled code.)

The main thing to note is that partial is a function like any other, therefore, in particular, it can be passed as the first partial argument.

+5
source

Source: https://habr.com/ru/post/1484517/


All Articles