<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Clojure Q&amp;A - Recent questions tagged anonymous functions</title>
<link>https://ask.clojure.org/index.php/tag/anonymous%20functions</link>
<description>Powered by Question2Answer</description>
<item>
<title>Feature Request:Make anonymous fn/reify class name IDs per namespace instead of one global counter</title>
<link>https://ask.clojure.org/index.php/14935/feature-request-anonymous-namespace-instead-global-counter</link>
<description>&lt;p&gt;  Compiled anonymous functions and reify classes get names like ns$fn__4532, where the numeric suffix comes from a global AtomicInteger counter&lt;br&gt;
  (RT.nextID()).&lt;/p&gt;
&lt;p&gt; This counter is shared across the entire runtime and is consumed by fn classes, reify classes, constants tables, gensyms, etc.&lt;/p&gt;
&lt;p&gt;  The problem is that because the counter is global, any code change shifts the IDs for everything compiled after it. This makes profiling across builds&lt;br&gt;
  really painful, you can&#039;t meaningfully compare flame graphs because all the class names change even in code you didn&#039;t touch.&lt;/p&gt;
&lt;p&gt;  I&#039;d like to propose scoping the ID counter per-namespace instead. Since the namespace is already part of the class name prefix, uniqueness is still&lt;br&gt;
  guaranteed. This would eliminate the cascading effect across unchanged namespaces and make profiling diffs actually useful.&lt;/p&gt;
&lt;p&gt;I&#039;m not sure what&#039;s the downsides of this, but from what I saw of AtomicInteger, it isn&#039;t used a lot to break things.&lt;/p&gt;
&lt;p&gt;Thank you!&lt;/p&gt;
</description>
<category>Compiler</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/14935/feature-request-anonymous-namespace-instead-global-counter</guid>
<pubDate>Wed, 18 Feb 2026 15:34:38 +0000</pubDate>
</item>
<item>
<title>macroexpansion of macro with anonymous function argument</title>
<link>https://ask.clojure.org/index.php/10269/macroexpansion-of-macro-with-anonymous-function-argument</link>
<description>&lt;p&gt;Hi!&lt;/p&gt;
&lt;p&gt;I have following synthetic macro receiving function expression as argument:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(defmacro func-macro [f]
  `(def x ~f))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Macroexpansion gives me:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;( macroexpand-1 `(func-macro (fn [x] x)))
  -&amp;gt;
  (def runtime.repl/x (clojure.core/fn [runtime.repl/x] runtime.repl/x))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If i execute this code form in REPL then i ll get an error because function has namespace qualified symbol runtime.repl/x as argument.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(runtime.repl/x) - failed: Extra input at: [:fn-tail :arity-1 :params] spec: :clojure.core.specs.alpha/param-list
runtime.repl/x - failed: vector? at: [:fn-tail :arity-n :params] spec: :clojure.core.specs.alpha/param-list
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;But if i am calling this macro in REPL then all things work!&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;( (func-macro (fn [x] x)) 100)
    -&amp;gt;
    100
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Why runtime.repl/x is OK for function definition if i am calling macro in REPL? &lt;/p&gt;
&lt;p&gt;Thanks!&lt;/p&gt;
&lt;p&gt;Andray&lt;/p&gt;
</description>
<category>Macros</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/10269/macroexpansion-of-macro-with-anonymous-function-argument</guid>
<pubDate>Sun, 28 Feb 2021 09:10:19 +0000</pubDate>
</item>
<item>
<title>Should the threading macros handle lambdas?</title>
<link>https://ask.clojure.org/index.php/9023/should-the-threading-macros-handle-lambdas</link>
<description>&lt;p&gt;I recently thought that I ran into a bug in &lt;code&gt;clojure.spec&lt;/code&gt; because it threw an exception when I put a lambda like &lt;code&gt;(fn [x] x)&lt;/code&gt; or &lt;code&gt;#(identity %)&lt;/code&gt; into the &lt;code&gt;-&amp;gt;&lt;/code&gt; threading macro without wrapping it first in an extra parens like &lt;code&gt;((fn [x] x))&lt;/code&gt;.  This is the most common repeated &quot;mistake&quot; I&#039;ve made in Clojure over the years, and I&#039;ve heard from others that it&#039;s a common misconception about the threading macros.  This &quot;mistake&quot; in my case is based on my assumption that the lambda is a fundamental &quot;thing&quot; or value in the language.  But, in fact, Clojure is just data, mostly macros, and each macro handles its input data in different ways.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;-&amp;gt;&lt;/code&gt; threading macro treats a lambda as a list instead of as a fundamental &quot;function value.&quot;  When one does &lt;code&gt;(-&amp;gt; :hmm (fn [x] x))&lt;/code&gt; it is expanded like: &lt;code&gt;(fn :hmm [x] x)&lt;/code&gt; which obviously is not what is intended.   &lt;em&gt;However&lt;/em&gt;, it does have a special convenience case for a non-list thing like a symbol, keyword, etc, which it assumes is something that can behave as a function with one argument, and wraps it in parens before threading.  This special case adds value because it interprets the &lt;em&gt;intention&lt;/em&gt; of the user and avoids forcing them to add parens when they&#039;re unnecessary. &lt;/p&gt;
&lt;p&gt;I assert that the basic lambda forms &lt;code&gt;(fn)&lt;/code&gt; and &lt;code&gt;#()&lt;/code&gt; should and easily can be handled similarly to a symbol, by interpreting the &lt;em&gt;intention&lt;/em&gt; of the user.  The value from doing so would make the threading macros simpler and more intuitive to use, would remove an unnecessary exception that affects numerous users,  and would advance the language towards treating the lambda forms as core &quot;values.&quot;  This last point is a philosophical one, but important in my opinion for making the language &quot;just work&quot; the way a new or non-expert user expects when getting stuff done.&lt;/p&gt;
&lt;p&gt;The existing special case of wrapping a non-list form already demonstrates the value of providing some interpretation of the &lt;em&gt;intention&lt;/em&gt; of the user, but there now exists an inconsistency between this special case, and the absence of it for two of the most common forms of arguably one of the most foundational &quot;values&quot; in the language, the function itself.&lt;/p&gt;
&lt;p&gt;Have you been bitten by this &quot;mistake&quot;?&lt;/p&gt;
&lt;p&gt;If you&#039;d like to try it side by side with the existing &lt;code&gt;-&amp;gt;&lt;/code&gt; macro, here&#039;s a version called &lt;code&gt;t-&amp;gt;&lt;/code&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(defmacro t-&amp;gt;
  &quot;Threads the expr through the forms. Inserts x as the
  second item in the first form, making a list of it if it is a lambda or not a
  list already. If there are more forms, inserts the first form as the
  second item in second form, etc.&quot;
  {:added &quot;1.0&quot;}
  [x &amp;amp; forms]
  (loop [x x, forms forms]
    (if forms
      (let [form (first forms)
            threaded (if (and (seq? form) (not (#{&#039;fn &#039;fn*} (first form))))
                       (with-meta `(~(first form) ~x ~@(next form)) (meta form))
                       (list form x))]
        (recur threaded (next forms)))
      x)))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I&#039;ve prepared a patch for &lt;code&gt;-&amp;gt;&lt;/code&gt; and &lt;code&gt;-&amp;gt;&amp;gt;&lt;/code&gt; that handles this use case and has no breaking impact that I can conceive of, keeping the existing behavior, while adding the new.  It passes all of the core Clojure tests, but I have not yet tested it on outside code beyond my own.  That would obviously be necessary before it could be considered a possible change in core.  &lt;/p&gt;
&lt;pre&gt;&lt;code&gt;From 686831062a574486413022af31e8c7a07b78cd24 Mon Sep 17 00:00:00 2001
From: Thomas Spellman &amp;lt;thos37@gmail.com&amp;gt;
Date: Mon, 13 Jan 2020 20:39:45 -0800
Subject: [PATCH] thread macros

---
 src/clj/clojure/core.clj             | 12 ++++++------
 test/clojure/test_clojure/macros.clj | 12 ++++++++++++
 2 files changed, 18 insertions(+), 6 deletions(-)

diff --git a/src/clj/clojure/core.clj b/src/clj/clojure/core.clj
index 8e98e072..fe43289b 100644
--- a/src/clj/clojure/core.clj
+++ b/src/clj/clojure/core.clj
@@ -1670,42 +1670,42 @@
   (. (. System (getProperties)) (get \&quot;os.name\&quot;))
 
   but is easier to write, read, and understand.&quot;
   {:added &quot;1.0&quot;}
   ([x form] `(. ~x ~form))
   ([x form &amp;amp; more] `(.. (. ~x ~form) ~@more)))
 
 (defmacro -&amp;gt;
-  &quot;Threads the expr through the forms. Inserts x as the
-  second item in the first form, making a list of it if it is not a
+  &quot;Threads the expr through the forms. Inserts x as the second item
+  in the first form, making a list of it if it is a lambda or not a
   list already. If there are more forms, inserts the first form as the
   second item in second form, etc.&quot;
   {:added &quot;1.0&quot;}
   [x &amp;amp; forms]
   (loop [x x, forms forms]
     (if forms
       (let [form (first forms)
-            threaded (if (seq? form)
+            threaded (if (and (seq? form) (not (#{&#039;fn &#039;fn*} (first form))))
                        (with-meta `(~(first form) ~x ~@(next form)) (meta form))
                        (list form x))]
         (recur threaded (next forms)))
       x)))
 
 (defmacro -&amp;gt;&amp;gt;
-  &quot;Threads the expr through the forms. Inserts x as the
-  last item in the first form, making a list of it if it is not a
+  &quot;Threads the expr through the forms. Inserts x as the last item
+  in the first form, making a list of it if it is a lambda or not a
   list already. If there are more forms, inserts the first form as the
   last item in second form, etc.&quot;
   {:added &quot;1.1&quot;}
   [x &amp;amp; forms]
   (loop [x x, forms forms]
     (if forms
       (let [form (first forms)
-            threaded (if (seq? form)
+            threaded (if (and (seq? form) (not (#{&#039;fn &#039;fn*} (first form))))
               (with-meta `(~(first form) ~@(next form)  ~x) (meta form))
               (list form x))]
         (recur threaded (next forms)))
       x)))
 
 (def map)
 
 (defn ^:private check-valid-options
diff --git a/test/clojure/test_clojure/macros.clj b/test/clojure/test_clojure/macros.clj
index ce17bb38..9fb1fa9e 100644
--- a/test/clojure/test_clojure/macros.clj
+++ b/test/clojure/test_clojure/macros.clj
@@ -106,8 +106,20 @@
   (is (nil? (loop []
               (as-&amp;gt; 0 x
                 (when-not (zero? x)
                   (recur))))))
   (is (nil? (loop [x nil] (some-&amp;gt; x recur))))
   (is (nil? (loop [x nil] (some-&amp;gt;&amp;gt; x recur))))
   (is (= 0 (loop [x 0] (cond-&amp;gt; x false recur))))
   (is (= 0 (loop [x 0] (cond-&amp;gt;&amp;gt; x false recur)))))
+
+(deftest -&amp;gt;lambda-test
+  (is (= &#039;a (-&amp;gt; &#039;a ((fn [x] x)))))
+  (is (= &#039;a (-&amp;gt; &#039;a (fn [x] x))))
+  (is (= &#039;a (-&amp;gt; &#039;a #(identity %))))
+  (is (= &#039;a (-&amp;gt; &#039;a (#(identity %))))))
+
+(deftest -&amp;gt;&amp;gt;lambda-test
+  (is (= &#039;a (-&amp;gt;&amp;gt; &#039;a ((fn [x] x)))))
+  (is (= &#039;a (-&amp;gt;&amp;gt; &#039;a (fn [x] x))))
+  (is (= &#039;a (-&amp;gt;&amp;gt; &#039;a #(identity %))))
+  (is (= &#039;a (-&amp;gt;&amp;gt; &#039;a (#(identity %))))))
-- 
2.21.0 (Apple Git-122.2)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Logged: &lt;a rel=&quot;nofollow&quot; href=&quot;https://clojure.atlassian.net/browse/CLJ-2553&quot;&gt;https://clojure.atlassian.net/browse/CLJ-2553&lt;/a&gt;&lt;/p&gt;
</description>
<category>Clojure</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/9023/should-the-threading-macros-handle-lambdas</guid>
<pubDate>Sun, 12 Jan 2020 23:21:15 +0000</pubDate>
</item>
<item>
<title>When should I prefer comp and partial to anonymous functions?</title>
<link>https://ask.clojure.org/index.php/8373/when-should-prefer-comp-and-partial-to-anonymous-functions</link>
<description>&lt;p&gt;There are places in code where I need functions that I define and use only once, in the same place I define them. There are 2 main ways to define such functions, one is anonymous fns:&lt;br&gt;
&lt;code&gt;(map #(blepify-with blep (:blop %)) xs)&lt;/code&gt;&lt;br&gt;
Another is higher order functions:&lt;br&gt;
&lt;code&gt;(map (comp (partial blepify-with blep) :blop) xs)&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;I personally don&#039;t use &lt;code&gt;partial&lt;/code&gt;, and always prefer anonymous functions, because:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;anonymous functions go through var dereference on invokation, which means I can redefine &lt;code&gt;blepify-with&lt;/code&gt; and new definition is used automatically. It helps greatly during development, and just as performant in production with direct linking;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;partial&lt;/code&gt; makes code less readable by hiding function arity. Explicit is better than implicit, and Cursive can show me immediately if I pass wrong amount of arguments to function&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;I also rarely use &lt;code&gt;comp&lt;/code&gt;. In addition to same argument about var dereferencing, it makes me read code from right to left. On small examples I provided it does not really matter, but as it grows bigger, threading macros greatly enhance readability. &lt;br&gt;
They only time I find &lt;code&gt;comp&lt;/code&gt; useful is when inversed order of function application is negated by inverse order of data processing, such as transducers: &lt;code&gt;(comp (filter even?) (map inc))&lt;/code&gt; is intended to be read from left to right. Even though &lt;code&gt;(map inc)&lt;/code&gt; is invoked first, data processed by this transducer will first go through &lt;code&gt;even?&lt;/code&gt; predicate, and then through &lt;code&gt;inc&lt;/code&gt; transform.&lt;/p&gt;
&lt;p&gt;My question is, is there something more to &lt;code&gt;partial&lt;/code&gt; and &lt;code&gt;comp&lt;/code&gt; that I&#039;m missing? Maybe it&#039;s a matter of taste and I&#039;m just not used to them, and they are just as readable, and not having to redefine usage places is overrated? When should I prefer &lt;code&gt;comp&lt;/code&gt; and &lt;code&gt;partial&lt;/code&gt; to anonymous functions?&lt;/p&gt;
</description>
<category>Clojure</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/8373/when-should-prefer-comp-and-partial-to-anonymous-functions</guid>
<pubDate>Thu, 08 Aug 2019 11:12:10 +0000</pubDate>
</item>
</channel>
</rss>