diff options
4 files changed, 352 insertions, 84 deletions
diff --git a/_tils/2021-04-24-clojure-auto-curry.md b/_tils/2021-04-24-clojure-auto-curry.md index 0d50cfa..c1e277f 100644 --- a/_tils/2021-04-24-clojure-auto-curry.md +++ b/_tils/2021-04-24-clojure-auto-curry.md @@ -4,6 +4,8 @@ title: Clojure auto curry date: 2021-04-24 1 +updated_at: 2021-04-27 + layout: post lang: en @@ -16,11 +18,11 @@ Here's a simple macro defined by [Loretta He][lorettahe] to create Clojure funct ```clojure (defmacro defcurry - [fname args & body] + [name args & body] (let [partials (map (fn [n] - `(~(subvec args 0 n) (partial ~fname ~@(take n args)))) + `(~(subvec args 0 n) (partial ~name ~@(take n args)))) (range 1 (count args)))] - `(defn ~fname + `(defn ~name (~args ~@body) ~@partials))) ``` @@ -72,3 +74,62 @@ Simple and elegant. Same Clojure as before, now with auto-currying via macros. [lorettahe]: http://lorettahe.github.io/clojure/2016/09/22/clojure-auto-curry + +## Comparison with Common Lisp + +My attempt at writing an equivalent for Common Lisp gives me: + +```lisp +(defun partial (fn &rest args) + (lambda (&rest args2) + (apply fn (append args args2)))) + +(defun curry-n (n func) + (cond ((< n 0) (error "Too many arguments")) + ((zerop n) (funcall func)) + (t (lambda (&rest rest) + (curry-n (- n (length rest)) + (apply #'partial func rest)))))) + +(defmacro defcurry (name args &body body) + `(defun ,name (&rest rest) + (let ((func (lambda ,args ,@body))) + (curry-n (- ,(length args) (length rest)) + (apply #'partial func rest))))) +``` + +Without built-in multi-arity support, we have to do more work, like tracking the number of arguments consumed so far. +We also have to write `#'partial` ourselves. +That is, without dependending on any library, sticking to ANSI Common Lisp. + +The usage is pretty similar: + +```lisp +* (defcurry add (a b c d e) + (+ a b c d e)) +ADD + +* (add 1) +#<FUNCTION (LAMBDA (&REST REST) :IN CURRY-N) {100216419B}> + +* (funcall (add 1) 2 3 4) +#<FUNCTION (LAMBDA (&REST REST) :IN CURRY-N) {100216537B}> + +* (funcall (add 1) 2 3 4 5) +15 + +* (funcall (funcall (add 1) 2 3) 4 5) +15 + +* (macroexpand-1 + '(defcurry add (a b c d e) + (+ a b c d e))) +(DEFUN ADD (&REST REST) + (LET ((FUNC (LAMBDA (A B C D E) (+ A B C D E)))) + (CURRY-N (- 5 (LENGTH REST)) (APPLY #'PARTIAL FUNC REST)))) +T +``` + +This also require `funcall`s, since we return a `lambda` that doesn't live in the function namespace. + +Like the Clojure one, it doesn't support optional parameters, `&rest` rest arguments, docstrings, etc., but it also could evolve to do so. diff --git a/locale/eo/LC_MESSAGES/_tils/2021-04-24-clojure-auto-curry.po b/locale/eo/LC_MESSAGES/_tils/2021-04-24-clojure-auto-curry.po index 14036ab..ab59a4f 100644 --- a/locale/eo/LC_MESSAGES/_tils/2021-04-24-clojure-auto-curry.po +++ b/locale/eo/LC_MESSAGES/_tils/2021-04-24-clojure-auto-curry.po @@ -14,17 +14,6 @@ msgstr "" msgid "ref: clojure-auto-curry" msgstr "" -msgid "" -"(defmacro defcurry\n" -" [fname args & body]\n" -" (let [partials (map (fn [n]\n" -" `(~(subvec args 0 n) (partial ~fname ~@(take n args))))\n" -" (range 1 (count args)))]\n" -" `(defn ~fname\n" -" (~args ~@body)\n" -" ~@partials)))\n" -msgstr "" - msgid "A naive `add` definition, alongside its usage and macroexpansion:" msgstr "" @@ -90,6 +79,102 @@ msgstr "" msgid "date: 2021-04-24 1" msgstr "" +msgid "" +"(defmacro defcurry\n" +" [name args & body]\n" +" (let [partials (map (fn [n]\n" +" `(~(subvec args 0 n) (partial ~name ~@(take n args))))\n" +" (range 1 (count args)))]\n" +" `(defn ~name\n" +" (~args ~@body)\n" +" ~@partials)))\n" +msgstr "" + +msgid "Comparison with Common Lisp" +msgstr "" + +msgid "My attempt at writing an equivalent for Common Lisp gives me:" +msgstr "" + +msgid "" +"(defun partial (fn &rest args)\n" +" (lambda (&rest args2)\n" +" (apply fn (append args args2))))\n" +"\n" +"(defun curry-n (n func)\n" +" (cond ((< n 0) (error \"Too many arguments\"))\n" +" ((zerop n) (funcall func))\n" +" (t (lambda (&rest rest)\n" +" (curry-n (- n (length rest))\n" +" (apply #'partial func rest))))))\n" +"\n" +"(defmacro defcurry (name args &body body)\n" +" `(defun ,name (&rest rest)\n" +" (let ((func (lambda ,args ,@body)))\n" +" (curry-n (- ,(length args) (length rest))\n" +" (apply #'partial func rest)))))\n" +msgstr "" + +msgid "" +"Without built-in multi-arity support, we have to do more work, like tracking" +" the number of arguments consumed so far. We also have to write `#'partial` " +"ourselves. That is, without dependending on any library, sticking to ANSI " +"Common Lisp." +msgstr "" + +msgid "The usage is pretty similar:" +msgstr "" + +msgid "" +"* (defcurry add (a b c d e)\n" +" (+ a b c d e))\n" +"ADD\n" +"\n" +"* (add 1)\n" +"#<FUNCTION (LAMBDA (&REST REST) :IN CURRY-N) {100216419B}>\n" +"\n" +"* (funcall (add 1) 2 3 4)\n" +"#<FUNCTION (LAMBDA (&REST REST) :IN CURRY-N) {100216537B}>\n" +"\n" +"* (funcall (add 1) 2 3 4 5)\n" +"15\n" +"\n" +"* (funcall (funcall (add 1) 2 3) 4 5)\n" +"15\n" +"\n" +"* (macroexpand-1\n" +" '(defcurry add (a b c d e)\n" +" (+ a b c d e)))\n" +"(DEFUN ADD (&REST REST)\n" +" (LET ((FUNC (LAMBDA (A B C D E) (+ A B C D E))))\n" +" (CURRY-N (- 5 (LENGTH REST)) (APPLY #'PARTIAL FUNC REST))))\n" +"T\n" +msgstr "" + +msgid "" +"This also require `funcall`s, since we return a `lambda` that doesn't live " +"in the function namespace." +msgstr "" + +msgid "" +"Like the Clojure one, it doesn't support optional parameters, `&rest` rest " +"arguments, docstrings, etc., but it also could evolve to do so." +msgstr "" + +msgid "updated_at: 2021-04-27" +msgstr "" + +#~ msgid "" +#~ "(defmacro defcurry\n" +#~ " [fname args & body]\n" +#~ " (let [partials (map (fn [n]\n" +#~ " `(~(subvec args 0 n) (partial ~fname ~@(take n args))))\n" +#~ " (range 1 (count args)))]\n" +#~ " `(defn ~fname\n" +#~ " (~args ~@body)\n" +#~ " ~@partials)))\n" +#~ msgstr "" + #~ msgid "date: 2021-04-24" #~ msgstr "" @@ -100,12 +185,6 @@ msgstr "" #~ "Clojure's multi-arity support:" #~ msgstr "" -#~ msgid "Comparison with Common Lisp" -#~ msgstr "" - -#~ msgid "My attempt at writing an equivalent for Common Lisp gives me:" -#~ msgstr "" - #~ msgid "" #~ "Without built-in multi-arity support, we have to do more work, like tracking" #~ " the number of arguments consumed so far. That is, without dependending on " @@ -113,16 +192,6 @@ msgstr "" #~ msgstr "" #~ msgid "" -#~ "This also require `funcall`s, since we return a `lambda` that doesn't live " -#~ "in the function namespace." -#~ msgstr "" - -#~ msgid "" -#~ "Like the Clojure one, it doesn't support optional parameters, `&rest` rest " -#~ "arguments, docstrings, etc., but it also could evolve to do so." -#~ msgstr "" - -#~ msgid "" #~ "(defun curry-n (n fn)\n" #~ " (if (= 0 n)\n" #~ " (funcall fn)\n" diff --git a/locale/fr/LC_MESSAGES/_tils/2021-04-24-clojure-auto-curry.po b/locale/fr/LC_MESSAGES/_tils/2021-04-24-clojure-auto-curry.po index 14036ab..ab59a4f 100644 --- a/locale/fr/LC_MESSAGES/_tils/2021-04-24-clojure-auto-curry.po +++ b/locale/fr/LC_MESSAGES/_tils/2021-04-24-clojure-auto-curry.po @@ -14,17 +14,6 @@ msgstr "" msgid "ref: clojure-auto-curry" msgstr "" -msgid "" -"(defmacro defcurry\n" -" [fname args & body]\n" -" (let [partials (map (fn [n]\n" -" `(~(subvec args 0 n) (partial ~fname ~@(take n args))))\n" -" (range 1 (count args)))]\n" -" `(defn ~fname\n" -" (~args ~@body)\n" -" ~@partials)))\n" -msgstr "" - msgid "A naive `add` definition, alongside its usage and macroexpansion:" msgstr "" @@ -90,6 +79,102 @@ msgstr "" msgid "date: 2021-04-24 1" msgstr "" +msgid "" +"(defmacro defcurry\n" +" [name args & body]\n" +" (let [partials (map (fn [n]\n" +" `(~(subvec args 0 n) (partial ~name ~@(take n args))))\n" +" (range 1 (count args)))]\n" +" `(defn ~name\n" +" (~args ~@body)\n" +" ~@partials)))\n" +msgstr "" + +msgid "Comparison with Common Lisp" +msgstr "" + +msgid "My attempt at writing an equivalent for Common Lisp gives me:" +msgstr "" + +msgid "" +"(defun partial (fn &rest args)\n" +" (lambda (&rest args2)\n" +" (apply fn (append args args2))))\n" +"\n" +"(defun curry-n (n func)\n" +" (cond ((< n 0) (error \"Too many arguments\"))\n" +" ((zerop n) (funcall func))\n" +" (t (lambda (&rest rest)\n" +" (curry-n (- n (length rest))\n" +" (apply #'partial func rest))))))\n" +"\n" +"(defmacro defcurry (name args &body body)\n" +" `(defun ,name (&rest rest)\n" +" (let ((func (lambda ,args ,@body)))\n" +" (curry-n (- ,(length args) (length rest))\n" +" (apply #'partial func rest)))))\n" +msgstr "" + +msgid "" +"Without built-in multi-arity support, we have to do more work, like tracking" +" the number of arguments consumed so far. We also have to write `#'partial` " +"ourselves. That is, without dependending on any library, sticking to ANSI " +"Common Lisp." +msgstr "" + +msgid "The usage is pretty similar:" +msgstr "" + +msgid "" +"* (defcurry add (a b c d e)\n" +" (+ a b c d e))\n" +"ADD\n" +"\n" +"* (add 1)\n" +"#<FUNCTION (LAMBDA (&REST REST) :IN CURRY-N) {100216419B}>\n" +"\n" +"* (funcall (add 1) 2 3 4)\n" +"#<FUNCTION (LAMBDA (&REST REST) :IN CURRY-N) {100216537B}>\n" +"\n" +"* (funcall (add 1) 2 3 4 5)\n" +"15\n" +"\n" +"* (funcall (funcall (add 1) 2 3) 4 5)\n" +"15\n" +"\n" +"* (macroexpand-1\n" +" '(defcurry add (a b c d e)\n" +" (+ a b c d e)))\n" +"(DEFUN ADD (&REST REST)\n" +" (LET ((FUNC (LAMBDA (A B C D E) (+ A B C D E))))\n" +" (CURRY-N (- 5 (LENGTH REST)) (APPLY #'PARTIAL FUNC REST))))\n" +"T\n" +msgstr "" + +msgid "" +"This also require `funcall`s, since we return a `lambda` that doesn't live " +"in the function namespace." +msgstr "" + +msgid "" +"Like the Clojure one, it doesn't support optional parameters, `&rest` rest " +"arguments, docstrings, etc., but it also could evolve to do so." +msgstr "" + +msgid "updated_at: 2021-04-27" +msgstr "" + +#~ msgid "" +#~ "(defmacro defcurry\n" +#~ " [fname args & body]\n" +#~ " (let [partials (map (fn [n]\n" +#~ " `(~(subvec args 0 n) (partial ~fname ~@(take n args))))\n" +#~ " (range 1 (count args)))]\n" +#~ " `(defn ~fname\n" +#~ " (~args ~@body)\n" +#~ " ~@partials)))\n" +#~ msgstr "" + #~ msgid "date: 2021-04-24" #~ msgstr "" @@ -100,12 +185,6 @@ msgstr "" #~ "Clojure's multi-arity support:" #~ msgstr "" -#~ msgid "Comparison with Common Lisp" -#~ msgstr "" - -#~ msgid "My attempt at writing an equivalent for Common Lisp gives me:" -#~ msgstr "" - #~ msgid "" #~ "Without built-in multi-arity support, we have to do more work, like tracking" #~ " the number of arguments consumed so far. That is, without dependending on " @@ -113,16 +192,6 @@ msgstr "" #~ msgstr "" #~ msgid "" -#~ "This also require `funcall`s, since we return a `lambda` that doesn't live " -#~ "in the function namespace." -#~ msgstr "" - -#~ msgid "" -#~ "Like the Clojure one, it doesn't support optional parameters, `&rest` rest " -#~ "arguments, docstrings, etc., but it also could evolve to do so." -#~ msgstr "" - -#~ msgid "" #~ "(defun curry-n (n fn)\n" #~ " (if (= 0 n)\n" #~ " (funcall fn)\n" diff --git a/locale/pt/LC_MESSAGES/_tils/2021-04-24-clojure-auto-curry.po b/locale/pt/LC_MESSAGES/_tils/2021-04-24-clojure-auto-curry.po index 14036ab..ab59a4f 100644 --- a/locale/pt/LC_MESSAGES/_tils/2021-04-24-clojure-auto-curry.po +++ b/locale/pt/LC_MESSAGES/_tils/2021-04-24-clojure-auto-curry.po @@ -14,17 +14,6 @@ msgstr "" msgid "ref: clojure-auto-curry" msgstr "" -msgid "" -"(defmacro defcurry\n" -" [fname args & body]\n" -" (let [partials (map (fn [n]\n" -" `(~(subvec args 0 n) (partial ~fname ~@(take n args))))\n" -" (range 1 (count args)))]\n" -" `(defn ~fname\n" -" (~args ~@body)\n" -" ~@partials)))\n" -msgstr "" - msgid "A naive `add` definition, alongside its usage and macroexpansion:" msgstr "" @@ -90,6 +79,102 @@ msgstr "" msgid "date: 2021-04-24 1" msgstr "" +msgid "" +"(defmacro defcurry\n" +" [name args & body]\n" +" (let [partials (map (fn [n]\n" +" `(~(subvec args 0 n) (partial ~name ~@(take n args))))\n" +" (range 1 (count args)))]\n" +" `(defn ~name\n" +" (~args ~@body)\n" +" ~@partials)))\n" +msgstr "" + +msgid "Comparison with Common Lisp" +msgstr "" + +msgid "My attempt at writing an equivalent for Common Lisp gives me:" +msgstr "" + +msgid "" +"(defun partial (fn &rest args)\n" +" (lambda (&rest args2)\n" +" (apply fn (append args args2))))\n" +"\n" +"(defun curry-n (n func)\n" +" (cond ((< n 0) (error \"Too many arguments\"))\n" +" ((zerop n) (funcall func))\n" +" (t (lambda (&rest rest)\n" +" (curry-n (- n (length rest))\n" +" (apply #'partial func rest))))))\n" +"\n" +"(defmacro defcurry (name args &body body)\n" +" `(defun ,name (&rest rest)\n" +" (let ((func (lambda ,args ,@body)))\n" +" (curry-n (- ,(length args) (length rest))\n" +" (apply #'partial func rest)))))\n" +msgstr "" + +msgid "" +"Without built-in multi-arity support, we have to do more work, like tracking" +" the number of arguments consumed so far. We also have to write `#'partial` " +"ourselves. That is, without dependending on any library, sticking to ANSI " +"Common Lisp." +msgstr "" + +msgid "The usage is pretty similar:" +msgstr "" + +msgid "" +"* (defcurry add (a b c d e)\n" +" (+ a b c d e))\n" +"ADD\n" +"\n" +"* (add 1)\n" +"#<FUNCTION (LAMBDA (&REST REST) :IN CURRY-N) {100216419B}>\n" +"\n" +"* (funcall (add 1) 2 3 4)\n" +"#<FUNCTION (LAMBDA (&REST REST) :IN CURRY-N) {100216537B}>\n" +"\n" +"* (funcall (add 1) 2 3 4 5)\n" +"15\n" +"\n" +"* (funcall (funcall (add 1) 2 3) 4 5)\n" +"15\n" +"\n" +"* (macroexpand-1\n" +" '(defcurry add (a b c d e)\n" +" (+ a b c d e)))\n" +"(DEFUN ADD (&REST REST)\n" +" (LET ((FUNC (LAMBDA (A B C D E) (+ A B C D E))))\n" +" (CURRY-N (- 5 (LENGTH REST)) (APPLY #'PARTIAL FUNC REST))))\n" +"T\n" +msgstr "" + +msgid "" +"This also require `funcall`s, since we return a `lambda` that doesn't live " +"in the function namespace." +msgstr "" + +msgid "" +"Like the Clojure one, it doesn't support optional parameters, `&rest` rest " +"arguments, docstrings, etc., but it also could evolve to do so." +msgstr "" + +msgid "updated_at: 2021-04-27" +msgstr "" + +#~ msgid "" +#~ "(defmacro defcurry\n" +#~ " [fname args & body]\n" +#~ " (let [partials (map (fn [n]\n" +#~ " `(~(subvec args 0 n) (partial ~fname ~@(take n args))))\n" +#~ " (range 1 (count args)))]\n" +#~ " `(defn ~fname\n" +#~ " (~args ~@body)\n" +#~ " ~@partials)))\n" +#~ msgstr "" + #~ msgid "date: 2021-04-24" #~ msgstr "" @@ -100,12 +185,6 @@ msgstr "" #~ "Clojure's multi-arity support:" #~ msgstr "" -#~ msgid "Comparison with Common Lisp" -#~ msgstr "" - -#~ msgid "My attempt at writing an equivalent for Common Lisp gives me:" -#~ msgstr "" - #~ msgid "" #~ "Without built-in multi-arity support, we have to do more work, like tracking" #~ " the number of arguments consumed so far. That is, without dependending on " @@ -113,16 +192,6 @@ msgstr "" #~ msgstr "" #~ msgid "" -#~ "This also require `funcall`s, since we return a `lambda` that doesn't live " -#~ "in the function namespace." -#~ msgstr "" - -#~ msgid "" -#~ "Like the Clojure one, it doesn't support optional parameters, `&rest` rest " -#~ "arguments, docstrings, etc., but it also could evolve to do so." -#~ msgstr "" - -#~ msgid "" #~ "(defun curry-n (n fn)\n" #~ " (if (= 0 n)\n" #~ " (funcall fn)\n" |