# msgid "" msgstr "" msgid "title: Clojure auto curry" msgstr "" msgid "layout: post" msgstr "" msgid "lang: en" msgstr "" msgid "ref: clojure-auto-curry" msgstr "" msgid "A naive `add` definition, alongside its usage and macroexpansion:" msgstr "" msgid "" "user=> (defcurry add\n" " [a b c d e]\n" " (+ 1 2 3 4 5))\n" "#'user/add\n" "\n" "user=> (add 1)\n" "#object[clojure.core$partial$fn__5857 0x2c708440 \"clojure.core$partial$fn__5857@2c708440\"]\n" "\n" "user=> (add 1 2 3 4)\n" "#object[clojure.core$partial$fn__5863 0xf4c0e4e \"clojure.core$partial$fn__5863@f4c0e4e\"]\n" "\n" "user=> ((add 1) 2 3 4 5)\n" "15\n" "\n" "user=> (((add 1) 2 3) 4 5)\n" "15\n" "\n" "user=> (use 'clojure.pprint)\n" "nil\n" "\n" "user=> (pprint\n" " (macroexpand\n" " '(defcurry add\n" " [a b c d e]\n" " (+ 1 2 3 4 5))))\n" "(def\n" " add\n" " (clojure.core/fn\n" " ([a b c d e] (+ 1 2 3 4 5))\n" " ([a] (clojure.core/partial add a))\n" " ([a b] (clojure.core/partial add a b))\n" " ([a b c] (clojure.core/partial add a b c))\n" " ([a b c d] (clojure.core/partial add a b c d))))\n" "nil\n" msgstr "" msgid "" "This simplistic `defcurry` definition doesn't support optional parameters, " "multi-arity, `&` rest arguments, docstrings, etc., but it could certainly " "evolve to do so." msgstr "" msgid "" "I like how `defcurry` is so short, and abdicates the responsability of doing" " the multi-arity logic to Clojure's built-in multi-arity support. Simple and" " elegant." msgstr "" msgid "Same Clojure as before, now with auto-currying via macros." msgstr "" msgid "" "Here's a simple macro defined by [Loretta " "He](http://lorettahe.github.io/clojure/2016/09/22/clojure-auto-curry) to " "create Clojure functions that are curried on all arguments, relying on " "Clojure's multi-arity support:" 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 "" #~ msgid "" #~ "A simple macro defined by [Loretta " #~ "He](http://lorettahe.github.io/clojure/2016/09/22/clojure-auto-curry) to " #~ "create Clojure functions that are curried on all arguments, relying on " #~ "Clojure's multi-arity support:" #~ 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 " #~ "any library, sticking to ANSI Common Lisp." #~ msgstr "" #~ msgid "" #~ "(defun curry-n (n fn)\n" #~ " (if (= 0 n)\n" #~ " (funcall fn)\n" #~ " (lambda (&rest rest)\n" #~ " (curry-n (something n) fn))))\n" #~ "\n" #~ "(defun add (a b c d e)\n" #~ " (curry-n\n" #~ " (length '(a b c d e))\n" #~ " (lambda (&rest rest)\n" #~ " (apply #'+ rest))))\n" #~ msgstr "" #~ msgid "" #~ "(defun curry-n (n fn)\n" #~ " (if (= 0 n)\n" #~ " (funcall fn)\n" #~ " (lambda (&rest rest)\n" #~ " (curry-n (something n) fn))))\n" #~ "\n" #~ "(defun add (a b c d e)\n" #~ " (curry-n\n" #~ " (length '(a b c d e))\n" #~ " (lambda (&rest rest)\n" #~ " (apply #'+ rest))))\n" #~ msgstr ""