# msgid "" msgstr "" msgid "title: Clojure auto curry" msgstr "" msgid "layout: post" msgstr "" msgid "lang: en" 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 "" 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 "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 "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 " #~ "any library, sticking to ANSI Common Lisp." #~ 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" #~ " (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 ""