This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2023-07-11
Channels
- # announcements (6)
- # babashka (35)
- # beginners (6)
- # biff (2)
- # calva (1)
- # cider (16)
- # clj-kondo (14)
- # clojure (176)
- # clojure-austin (13)
- # clojure-europe (7)
- # clojure-norway (6)
- # clojure-uk (12)
- # clojurescript (4)
- # cursive (11)
- # data-science (3)
- # datalog (10)
- # datomic (45)
- # events (1)
- # fulcro (2)
- # hyperfiddle (29)
- # leiningen (14)
- # lsp (2)
- # meander (2)
- # off-topic (24)
- # polylith (5)
- # re-frame (2)
- # shadow-cljs (21)
- # specter (14)
- # tools-deps (16)
- # xtdb (5)
Why is arity for many functions not just called with &args? For instance:
(source complement)
=>
(defn complement
"Takes a fn f and returns a fn that takes the same arguments as f,
has the same effects, if any, and returns the opposite truth value."
{:added "1.0"
:static true}
[f]
(fn
([] (not (f)))
([x] (not (f x)))
([x y] (not (f x y)))
([x y & zs] (not (apply f x y zs)))))
Why not just:
(source complement)
=>
(defn complement
"Takes a fn f and returns a fn that takes the same arguments as f,
has the same effects, if any, and returns the opposite truth value."
{:added "1.0"
:static true}
[f]
(fn
([] (not (f)))
([x] (not (f x)))
([x & zs] (not (apply f x zs)))))
Shouldn't that work as well?I believe the short answer is that common (small) arities are unrolled for performance
✅ 1
I asked this about a month ago, alex’s responses: https://clojurians.slack.com/archives/C053AK3F9/p1685660897898439?thread_ts=1685659605.985079&cid=C053AK3F9 https://clojurians.slack.com/archives/C053AK3F9/p1685660947113869?thread_ts=1685659605.985079&cid=C053AK3F9
💯 2
We actually only "need" one arity:
(defn complement [f]
#(not (apply f %&)))