<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Clojure Q&amp;A - Recent questions tagged clojure.core</title>
<link>https://ask.clojure.org/index.php/tag/clojure.core</link>
<description></description>
<item>
<title>`eval` using functions with metadata</title>
<link>https://ask.clojure.org/index.php/15115/eval-using-functions-with-metadata</link>
<description>&lt;p&gt;TIL:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(eval (list + 1 2)) ; works
(eval (list (var-get #'+) 1 2)) ; works
(eval (list (fn [a b] (+ a b)) 1 2)) ; works

(defmacro m [] `(~(fn [a b] (+ a b)) 1 2))
(m) ; also works

;; however, if the function has any metadata:
(eval (list (with-meta + {}) 1 2))
;; ^ fails, No matching ctor found for class clojure.lang.AFunction$1
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This was unintuitive to discover.&lt;/p&gt;
&lt;p&gt;I solved it locally with a little helper:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(defn val-&amp;gt;expr [v]
  (if (symbol? v)
    `'~v
    (cond-&amp;gt; v
      (and (fn? v) (not (nil? (meta v))))
      (-&amp;gt; (with-meta nil)
          (list (-&amp;gt; v meta (update-vals val-&amp;gt;expr)))
          (conj `with-meta)))))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;But this takes special handling. I wonder if something similar would not be possible on a &quot;matching ctor&quot;-level, possibly also for &lt;code&gt;comp&lt;/code&gt;, &lt;code&gt;every-pred?&lt;/code&gt;, etc. It would be fairly trivial even in userland to have versions of these functions which carry metadata sufficient to recreate expressions, e.g. &lt;code&gt;comp&lt;/code&gt; storing the list of functions, and then &lt;code&gt;eval&lt;/code&gt;, on encountering &lt;code&gt;No matching ctor found for class clojure.core$comp$fn__5921&lt;/code&gt;, can instead pull out the list of functions from that value and evaluate &lt;code&gt;(list* comp (:composed-fns (meta v)))&lt;/code&gt; (or some such) instead.&lt;/p&gt;
&lt;p&gt;I think this would be great to have in the core lib, at least for metadata and core libs functions like &lt;code&gt;comp&lt;/code&gt;, &lt;code&gt;some-fn&lt;/code&gt;, &lt;code&gt;every-pred?&lt;/code&gt;, and &lt;code&gt;complement&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Note, I'm not talking about adding or changing ctors for these necessarily. A hook for taking unconstructable values and making a constructable value out of them would serve just as well, maybe even a multifn.&lt;/p&gt;
&lt;p&gt;Strictly in userland and without core lib support, I suppose the cleanest way to do something like this is a custom protocol and probably monkeypatch some core libs functions (maybe &lt;code&gt;eval&lt;/code&gt;?)&lt;/p&gt;
&lt;p&gt;So I suppose my questions are:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Is this something you agree would be good to have in general? Or is this by design for some reason I can't see?&lt;/li&gt;
&lt;li&gt;Would this be good and practical to have in the corelib? Oughtn't be a breaking change at least&lt;/li&gt;
&lt;li&gt;If no, is there currently any sane way to apply my solution in a more systemic way than wrapping every relevant value with a &lt;code&gt;val-&amp;gt;expr&lt;/code&gt; call?&lt;/li&gt;
&lt;/ol&gt;
</description>
<category>Syntax and reader</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/15115/eval-using-functions-with-metadata</guid>
<pubDate>Mon, 01 Jun 2026 14:33:00 +0000</pubDate>
</item>
<item>
<title>Optimized str function</title>
<link>https://ask.clojure.org/index.php/14990/optimized-str-function</link>
<description>&lt;p&gt;Opimized version of str function:    &lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(defn my-str
  (^String [] &quot;&quot;)
  (^String [^Object x]
   (if (nil? x) &quot;&quot; (. x (toString))))
  (^String [^Object x &amp;amp; ys]
   (let [sb (StringBuilder. (if (nil? x) &quot;&quot; (. x (toString))))]
     (loop [ys (seq ys)]
       (if-not (nil? ys)
         (let [x (.first ys)]
           (if-not (nil? x)
             (.append sb (.toString ^Object x)))
           (recur (.next ys)))))
     (.toString sb))))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Benchmarks:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(let [xs (vec (range 1000))]
    (criterium/bench
      (apply my-str xs)))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Evaluation count : 2577840 in 60 samples of 42964 calls.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;         Execution time mean : 23.611394 µs

(let [xs (vec (range 1000))]
    (criterium/bench
      (apply str xs)))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Evaluation count : 1375200 in 60 samples of 22920 calls.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;         Execution time mean : 42.015320 µs
&lt;/code&gt;&lt;/pre&gt;
</description>
<category>Sequences</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/14990/optimized-str-function</guid>
<pubDate>Mon, 16 Mar 2026 11:12:53 +0000</pubDate>
</item>
<item>
<title>specifying `:let` as the first element of `for` causes an macro expansion error</title>
<link>https://ask.clojure.org/index.php/14670/specifying-let-first-element-causes-macro-expansion-error</link>
<description>&lt;p&gt;We can use &lt;code&gt;:let&lt;/code&gt; in &lt;code&gt;for&lt;/code&gt; to define local variable like this&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(for [i (range 5)
      :let [inc-i (inc i)]]
  [i inc-i])
;;=&amp;gt; ([0 1] [1 2] [2 3] [3 4] [4 5])
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Otherwise, if we specified &lt;code&gt;:let&lt;/code&gt; as first element, it causes an macro expansion error.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(for [:let [range-end 5]
      i (range range-end)
      :let [inc-i (inc i)]]
  [i inc-i])
;;=&amp;gt; Syntax error macroexpanding for at (*cider-scratch*:555:1).
;;   Can't pop empty vector
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Of course, in this case, we should have wrapped &lt;code&gt;for&lt;/code&gt; in &lt;code&gt;let&lt;/code&gt;. However, I'm curious whether making &lt;code&gt;:let&lt;/code&gt; available only for second and subsequent elements is intended behavior as part of the public interface.&lt;/p&gt;
</description>
<category>Macros</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/14670/specifying-let-first-element-causes-macro-expansion-error</guid>
<pubDate>Sun, 10 Aug 2025 02:40:33 +0000</pubDate>
</item>
<item>
<title>Is RT.longCast() missing an overload for char?</title>
<link>https://ask.clojure.org/index.php/14650/is-rt-longcast-missing-an-overload-for-char</link>
<description>&lt;p&gt;This code resorts to reflection:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(long (char \x))
&lt;/code&gt;&lt;/pre&gt;
&lt;blockquote&gt;&lt;p&gt;call to static method &lt;code&gt;longCast&lt;/code&gt; on &lt;code&gt;clojure.lang.RT&lt;/code&gt; can't be resolved (argument types: &lt;code&gt;char&lt;/code&gt;).&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Casting to &lt;code&gt;int&lt;/code&gt; does not trigger this reflection warning, as &lt;code&gt;RT.intCast(char)&lt;/code&gt; exists.&lt;/p&gt;
&lt;p&gt;It seems to me there should be a &lt;code&gt;RT.longCast(char)&lt;/code&gt; as well?&lt;/p&gt;
</description>
<category>Clojure</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/14650/is-rt-longcast-missing-an-overload-for-char</guid>
<pubDate>Thu, 31 Jul 2025 11:02:56 +0000</pubDate>
</item>
<item>
<title>`merge-with` doesn´t treat pair (vectors of 2) the same way `merge` does</title>
<link>https://ask.clojure.org/index.php/14529/merge-with-doesnt-treat-pair-vectors-the-same-way-merge-does</link>
<description>&lt;pre&gt;&lt;code&gt;;; merge
(merge {:a :b} [:a :c]) ;=&amp;gt; {:a :c}

;; merge-with
(merge-with (fn [_ x] x) {} {:a :b} [:a :c])
;; =&amp;gt; class clojure.lang.Keyword cannot be cast to class java.util.Map$Entry
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I think this happens because &lt;code&gt;merge-with&lt;/code&gt; eventually &lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/clojure/clojure/blob/master/src/clj/clojure/core.clj#L3090&quot;&gt;calls &lt;code&gt;seq&lt;/code&gt; on the &quot;Map&quot;&lt;/a&gt;, expecting to get a &quot;Map Entry&quot; from which then it can extracts &lt;code&gt;key&lt;/code&gt; and &lt;code&gt;value&lt;/code&gt;, and fails when receiving a Vector, because &lt;code&gt;seq&lt;/code&gt; will just yield one element at a time:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(key (first (seq {:a :c}))) ;; =&amp;gt; :a
(key (first (seq [:a :c])))
;; =&amp;gt; class clojure.lang.Keyword cannot be cast to class java.util.Map$Entry
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I think it is justified to expect that &lt;code&gt;(partial merge-with (fn [_ x] x))&lt;/code&gt; should behave exactly as &lt;code&gt;merge&lt;/code&gt; and my proposal is to make it happen, if what I described makes sense.&lt;/p&gt;
&lt;p&gt;Thank you!&lt;/p&gt;
</description>
<category>Collections</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/14529/merge-with-doesnt-treat-pair-vectors-the-same-way-merge-does</guid>
<pubDate>Thu, 01 May 2025 11:25:20 +0000</pubDate>
</item>
<item>
<title>Idiomatic way to use java.util.Optional</title>
<link>https://ask.clojure.org/index.php/14501/idiomatic-way-to-use-java-util-optional</link>
<description>&lt;p&gt;Is there an easy way to work with &lt;code&gt;java.utils.Optional&lt;/code&gt; values in Clojure? I'm currently using 1.12 so I can say things like &lt;code&gt;(Optional/.orElse foo nil)&lt;/code&gt; to convert the optional value to a &quot;normal&quot; value (X or nil), but that and other Optional methods are still sources of tension or boilerplate compared idiomatic Clojure constructs (&lt;code&gt;some-&amp;gt;&lt;/code&gt; for example).&lt;/p&gt;
&lt;p&gt;If such a way doesn't currently exist, can it be considered for addition to Clojure? Maybe opening up &lt;code&gt;deref&lt;/code&gt; to work on Optionals as well.&lt;/p&gt;
</description>
<category>Java Interop</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/14501/idiomatic-way-to-use-java-util-optional</guid>
<pubDate>Mon, 07 Apr 2025 14:16:59 +0000</pubDate>
</item>
<item>
<title>Could we have a msg arg to time?</title>
<link>https://ask.clojure.org/index.php/14350/could-we-have-a-msg-arg-to-time</link>
<description>&lt;p&gt;Very handy in CLJC projects especially to log long running operations. Happy to do PR(s).&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;  [expr msg]
  `(let [start# (. System (nanoTime))
         ret# ~expr]
     (prn (str msg (/ (double (- (. System (nanoTime)) start#)) 1000000.0) &quot; msecs&quot;))
     ret#))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Even better would be fmt-msg.  (format msg-msg (/ (double (- (. System (nanoTime)) start#)) 1000000.0))&lt;/p&gt;
</description>
<category>Clojure</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/14350/could-we-have-a-msg-arg-to-time</guid>
<pubDate>Wed, 22 Jan 2025 21:03:28 +0000</pubDate>
</item>
<item>
<title>Incorrect result when evaluating `not=` on `##NaNs`</title>
<link>https://ask.clojure.org/index.php/14298/incorrect-result-when-evaluating-not-on-nans</link>
<description>&lt;p&gt;Alex Miller asked me to write this up here. I found a bug in clojure.core yesterday that involves &lt;code&gt;not=&lt;/code&gt; when used to compare NaNs. This was reported on Slack and the discussion is here:&lt;br&gt;
&lt;a rel=&quot;nofollow&quot; href=&quot;https://clojurians.slack.com/archives/C03S1KBA2/p1733612992809069&quot;&gt;https://clojurians.slack.com/archives/C03S1KBA2/p1733612992809069&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Here is a more highly summarized version of the discussion from Slack (at least from my perspective). If we fire up the Clojure CLI, we can evaluate the following.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt; Clojure 1.12.0
 user=&amp;gt; (= ##NaN ##NaN)
 false
 user=&amp;gt; (not= ##NaN ##NaN)
 false
 user=&amp;gt; (not (= ##NaN ##NaN))
 true
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The problem here is that &lt;code&gt;=&lt;/code&gt;, &lt;code&gt;not&lt;/code&gt;, and &lt;code&gt;not=&lt;/code&gt; have a relationship between them. Specifically, for any &lt;code&gt;x&lt;/code&gt; and &lt;code&gt;y&lt;/code&gt;, if &lt;code&gt;(= x y)&lt;/code&gt; returns a boolean, then &lt;code&gt;(not (= x y))&lt;/code&gt; should return the opposite boolean, and since &lt;code&gt;not=&lt;/code&gt; is defined as &lt;code&gt;(not (= x y))&lt;/code&gt;, it should return the same value as &lt;code&gt;(not (= x y))&lt;/code&gt; for all &lt;code&gt;x&lt;/code&gt; and &lt;code&gt;y&lt;/code&gt;. This doesn't happen if &lt;code&gt;x&lt;/code&gt; and &lt;code&gt;y&lt;/code&gt; are both &lt;code&gt;##NaN&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Note that there were a lot of calories burned on Slack with suggestions that doubles should never be compared for equality, that NaNs are shifty, not-quite-value objects and should be avoided, that anybody who wants to test for the presence of a NaN should use &lt;code&gt;NaN?&lt;/code&gt; which is already in clojure.core, and that the documentation around equality should be updated to say some of those things. Many of those statements are true or good practice. But all of them miss the broader point.&lt;/p&gt;
&lt;p&gt;This bug has nothing to do specifically to do with NaNs. It just seems that NaNs expose the bug. The real issue is with the contractual relationship between &lt;code&gt;=&lt;/code&gt;, &lt;code&gt;not&lt;/code&gt;, and &lt;code&gt;not=&lt;/code&gt; which appears to be violated in the presence of NaNs. Specifically, &lt;code&gt;not=&lt;/code&gt; is no longer referentially transparent with respect to &lt;code&gt;(not (= ...))&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;On Slack, @potetm decompiled the code generated for these cases and found the following.&lt;/p&gt;
&lt;p&gt;For &lt;code&gt;(not= ##NaN ##NaN)&lt;/code&gt; :&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(clj-java-decompiler.core/decompile
  (not= ##NaN ##NaN))

// Decompiling class: cjd__init
import clojure.lang.*;

public class cjd__init
{
    public static final Var __not_EQ_;
    public static final Object const__1;
    
    public static void load() {
        ((IFn)cjd__init.__not_EQ_.getRawRoot()).invoke(cjd__init.const__1, cjd__init.const__1);
    }
    
    public static void __init0() {
        __not_EQ_ = RT.var(&quot;clojure.core&quot;, &quot;not=&quot;);
        const__1 = Double.NaN;
    }
    
    static {
        __init0();
        Compiler.pushNSandLoader(RT.classForName(&quot;cjd__init&quot;).getClassLoader());
        try {
            load();
            Var.popThreadBindings();
        }
        finally {
            Var.popThreadBindings();
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And then for &lt;code&gt;(not (= ##NaN ##NaN))&lt;/code&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(clj-java-decompiler.core/decompile
  (not (= ##NaN ##NaN)))

// Decompiling class: cjd__init
import clojure.lang.*;

public class cjd__init
{
    public static final Var __not;
    
    public static void load() {
        ((IFn)cjd__init.__not.getRawRoot()).invoke(Util.equiv(Double.NaN, Double.NaN) ? Boolean.TRUE : Boolean.FALSE);
    }
    
    public static void __init0() {
        __not = RT.var(&quot;clojure.core&quot;, &quot;not&quot;);
    }
    
    static {
        __init0();
        Compiler.pushNSandLoader(RT.classForName(&quot;cjd__init&quot;).getClassLoader());
        try {
            load();
            Var.popThreadBindings();
        }
        finally {
            Var.popThreadBindings();
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;It appears that the compiler optimizes the call to &lt;code&gt;=&lt;/code&gt; and does not box the &lt;code&gt;##NaN&lt;/code&gt; values when compiling &lt;code&gt;(not (= ##NaN ##NaN))&lt;/code&gt;, whereas the call to &lt;code&gt;not=&lt;/code&gt; receives the &lt;code&gt;##NaN&lt;/code&gt;s boxed as Doubles. This then causes a subsequent call to &lt;code&gt;clojure.lang.Util/equiv&lt;/code&gt; (after following the call chain through &lt;code&gt;not=&lt;/code&gt; -&amp;gt; &lt;code&gt;=&lt;/code&gt; -&amp;gt; &lt;code&gt;clojure.lang.Util/equiv&lt;/code&gt;) to return &lt;code&gt;true&lt;/code&gt; improperly (since NaNs are never equal to anything, even themselves).&lt;/p&gt;
&lt;p&gt;IMO, this is a bug, albeit a low priority one. Most programmers successfully use floating point math without ever having to deal with NaNs. While NaNs seem to trigger the bug, there may be other cases that also trigger it. I can't speak to that.&lt;/p&gt;
</description>
<category>Compiler</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/14298/incorrect-result-when-evaluating-not-on-nans</guid>
<pubDate>Sun, 08 Dec 2024 21:14:02 +0000</pubDate>
</item>
<item>
<title>Add index-by function to the clojure.core namespace</title>
<link>https://ask.clojure.org/index.php/13607/add-index-by-function-to-the-clojure-core-namespace</link>
<description>&lt;p&gt;What do you think about adding the &lt;code&gt;index-by&lt;/code&gt; function to the core namespace? It's like &lt;code&gt;group-by&lt;/code&gt; but doesn't create a vector:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(defn index-by
  &quot;Return a map where a key is (f item) and a value is item.&quot;
  {:added &quot;1.11&quot;
   :static true}
  [f coll]
  (persistent!
   (reduce
    (fn [ret x]
      (assoc! ret (f x) x))
    (transient {}) coll)))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I've been using this function for years especially for indexing a database result. Often, you have a vector of maps where each map has a unique ID, and you need to build a map like ID =&amp;gt; row. Why don't instroduce such a function? I can create a PR.&lt;/p&gt;
</description>
<category>Clojure</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/13607/add-index-by-function-to-the-clojure-core-namespace</guid>
<pubDate>Thu, 11 Jan 2024 18:34:49 +0000</pubDate>
</item>
<item>
<title>`ClassCastException` when misplacing `nth` args</title>
<link>https://ask.clojure.org/index.php/13539/classcastexception-when-misplacing-nth-args</link>
<description>&lt;p&gt;I accidentally misplaced the args to &lt;code&gt;nth&lt;/code&gt; and was presented with the following error and stacktrace:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;  Show: Project-Only All 
  Hide: Clojure Java REPL Tooling Duplicates  (0 frames hidden)

2. Unhandled clojure.lang.Compiler$CompilerException
   Error compiling NO_SOURCE_FILE at (79:3)
   #:clojure.error{:phase :execution,
                   :line 79,
                   :column 3,
                   :source &quot;NO_SOURCE_FILE&quot;}
             Compiler.java: 3719  clojure.lang.Compiler$InvokeExpr/eval
             Compiler.java:  457  clojure.lang.Compiler$DefExpr/eval
             Compiler.java: 7199  clojure.lang.Compiler/eval
             Compiler.java: 7149  clojure.lang.Compiler/eval
                  core.clj: 3215  clojure.core/eval
                  core.clj: 3211  clojure.core/eval
    interruptible_eval.clj:   87  nrepl.middleware.interruptible-eval/evaluate/fn/fn
                  AFn.java:  152  clojure.lang.AFn/applyToHelper
                  AFn.java:  144  clojure.lang.AFn/applyTo
                  core.clj:  667  clojure.core/apply
                  core.clj: 1990  clojure.core/with-bindings*
                  core.clj: 1990  clojure.core/with-bindings*
               RestFn.java:  425  clojure.lang.RestFn/invoke
    interruptible_eval.clj:   87  nrepl.middleware.interruptible-eval/evaluate/fn
                  main.clj:  437  clojure.main/repl/read-eval-print/fn
                  main.clj:  437  clojure.main/repl/read-eval-print
                  main.clj:  458  clojure.main/repl/fn
                  main.clj:  458  clojure.main/repl
                  main.clj:  368  clojure.main/repl
               RestFn.java: 1523  clojure.lang.RestFn/invoke
    interruptible_eval.clj:   84  nrepl.middleware.interruptible-eval/evaluate
    interruptible_eval.clj:   56  nrepl.middleware.interruptible-eval/evaluate
    interruptible_eval.clj:  152  nrepl.middleware.interruptible-eval/interruptible-eval/fn/fn
                  AFn.java:   22  clojure.lang.AFn/run
               session.clj:  202  nrepl.middleware.session/session-exec/main-loop/fn
               session.clj:  201  nrepl.middleware.session/session-exec/main-loop
                  AFn.java:   22  clojure.lang.AFn/run
               Thread.java:  829  java.lang.Thread/run

1. Caused by java.lang.ClassCastException
   class clojure.lang.PersistentVector cannot be cast to class
   java.lang.Number (clojure.lang.PersistentVector is in unnamed module of
   loader 'app'; java.lang.Number is in module java.base of loader
   'bootstrap')

                      REPL:   82  advent-of-clerk.year-2023.day-04/fn
                      REPL:   79  advent-of-clerk.year-2023.day-04/fn
                  AFn.java:  152  clojure.lang.AFn/applyToHelper
                  AFn.java:  144  clojure.lang.AFn/applyTo
             Compiler.java: 3714  clojure.lang.Compiler$InvokeExpr/eval
             Compiler.java:  457  clojure.lang.Compiler$DefExpr/eval
             Compiler.java: 7199  clojure.lang.Compiler/eval
             Compiler.java: 7149  clojure.lang.Compiler/eval
                  core.clj: 3215  clojure.core/eval
                  core.clj: 3211  clojure.core/eval
    interruptible_eval.clj:   87  nrepl.middleware.interruptible-eval/evaluate/fn/fn
                  AFn.java:  152  clojure.lang.AFn/applyToHelper
                  AFn.java:  144  clojure.lang.AFn/applyTo
                  core.clj:  667  clojure.core/apply
                  core.clj: 1990  clojure.core/with-bindings*
                  core.clj: 1990  clojure.core/with-bindings*
               RestFn.java:  425  clojure.lang.RestFn/invoke
    interruptible_eval.clj:   87  nrepl.middleware.interruptible-eval/evaluate/fn
                  main.clj:  437  clojure.main/repl/read-eval-print/fn
                  main.clj:  437  clojure.main/repl/read-eval-print
                  main.clj:  458  clojure.main/repl/fn
                  main.clj:  458  clojure.main/repl
                  main.clj:  368  clojure.main/repl
               RestFn.java: 1523  clojure.lang.RestFn/invoke
    interruptible_eval.clj:   84  nrepl.middleware.interruptible-eval/evaluate
    interruptible_eval.clj:   56  nrepl.middleware.interruptible-eval/evaluate
    interruptible_eval.clj:  152  nrepl.middleware.interruptible-eval/interruptible-eval/fn/fn
                  AFn.java:   22  clojure.lang.AFn/run
               session.clj:  202  nrepl.middleware.session/session-exec/main-loop/fn
               session.clj:  201  nrepl.middleware.session/session-exec/main-loop
                  AFn.java:   22  clojure.lang.AFn/run
               Thread.java:  829  java.lang.Thread/run
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I was having a hard time spotting the problem, as I was changing a few things at once.  Ultimately I realized that I've misplaced the collection and the index arguments to &lt;code&gt;nth&lt;/code&gt;, but the stacktrace did little to help me realize that: there is no mention of call to &lt;code&gt;nth&lt;/code&gt; there, and the line numbers are not pointing exactly to the problematic line.&lt;/p&gt;
&lt;p&gt;The complete example code here: &lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/a1exsh/advent-of-clerk/blob/cddd42fcd60da2efcda37f222666328cd37fb616/src/advent_of_clerk/year_2023/day_04.clj#L84&quot;&gt;https://github.com/a1exsh/advent-of-clerk/blob/cddd42fcd60da2efcda37f222666328cd37fb616/src/advent_of_clerk/year_2023/day_04.clj#L84&lt;/a&gt; (this is the correct code — to get the error swap the arguments like this: &lt;code&gt;(nth i untouched)&lt;/code&gt;)&lt;/p&gt;
&lt;p&gt;The environment where I was hitting this is evaluating the single loop form in CIDER (Emacs) with &lt;code&gt;C-x C-e&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;It can be readily reproduced in REPL of course, just like this:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;user&amp;gt; (nth 0 [])
Execution error (ClassCastException) at user/eval22783 (REPL:42).
class clojure.lang.PersistentVector cannot be cast to class java.lang.Number (clojure.lang.PersistentVector is in unnamed module of loader 'app'; java.lang.Number is in module java.base of loader 'bootstrap')
&lt;/code&gt;&lt;/pre&gt;
</description>
<category>Errors</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/13539/classcastexception-when-misplacing-nth-args</guid>
<pubDate>Mon, 11 Dec 2023 05:42:28 +0000</pubDate>
</item>
<item>
<title>RT/chunkIteratorSeq does not produce a chunked-seq?</title>
<link>https://ask.clojure.org/index.php/13471/rt-chunkiteratorseq-does-not-produce-a-chunked-seq</link>
<description>&lt;pre&gt;&lt;code&gt;user&amp;gt; (def al (hamf/array-list (range 200))) 
#'user/al
user&amp;gt; (type al)
java.util.ArrayList
user&amp;gt; (chunked-seq? (seq al))
false
user&amp;gt; (instance? java.lang.Iterable al)
trueSyntax error reading source at (REPL:65:41).
Unmatched delimiter: )
user&amp;gt; (instance? java.lang.Iterable al)
true
user&amp;gt; (chunked-seq? (clojure.lang.RT/seq al))
false
user&amp;gt; (chunked-seq? (clojure.lang.RT/chunkIteratorSeq (.iterator al)))
false
user&amp;gt; (type (clojure.lang.RT/chunkIteratorSeq (.iterator al)))
clojure.lang.LazySeq
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If no lazy seq is a chunked seq then the result of clojure.core/map, filter, etc. also cannot ever be a chunked seq as they are currently written.  There are just a whole lot of questions here.&lt;/p&gt;
</description>
<category>Java Interop</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/13471/rt-chunkiteratorseq-does-not-produce-a-chunked-seq</guid>
<pubDate>Thu, 16 Nov 2023 15:05:15 +0000</pubDate>
</item>
<item>
<title>rand-int should check its arg before calling rand</title>
<link>https://ask.clojure.org/index.php/13457/rand-int-should-check-its-arg-before-calling-rand</link>
<description>&lt;p&gt;The current source code for &lt;code&gt;rand-int&lt;/code&gt; is (abridged):&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(defn rand-int [n] (int (rand n)))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;but this means it can take an arg &amp;gt;Integer/MAX_VALUE and randomly throw overflow exceptions:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(rand-int (* Integer/MAX_VALUE 2.0))
;; =&amp;gt; Execution error (ArithmeticException) at java.lang.Math/toIntExact (Math.java:1135).
;;    integer overflow

(rand-int (* Integer/MAX_VALUE 2.0))
;; =&amp;gt; 1536421981
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;etc.&lt;br&gt;
I can see that being able pass a float or double could be useful for &quot;weighting&quot; the range:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(/ (reduce + 0 (map (fn [_] (rand-int 2.5)) (range 1e8)))
   1e8)
;; =&amp;gt; 0.79993543

(/ (reduce + 0 (map (fn [_] (rand-int 2)) (range 1e8)))
   1e8)
;; =&amp;gt; 0.49998904
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;but to be honest I think this is probably not immediately apparent to a lot of users.&lt;/p&gt;
&lt;p&gt;So, if you want that to allow that behaviour, source code could look something like this:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(defn rand-int [n] (int n) (int (rand n)))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And if you didn't it could look something like this:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(defn rand-int [n] (int (rand (int n))))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;But as it currently stands, we don't coerce to an integer, or even check that the input is within Integer range, leading to random overflow exceptions. And random exceptions seem like a bad thing, even for a function called &lt;code&gt;rand-int&lt;/code&gt;.&lt;/p&gt;
</description>
<category>Clojure</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/13457/rand-int-should-check-its-arg-before-calling-rand</guid>
<pubDate>Fri, 10 Nov 2023 17:15:30 +0000</pubDate>
</item>
<item>
<title>clojure.lang.LazySeq synchronized methods pin virtual thread</title>
<link>https://ask.clojure.org/index.php/13318/clojure-lang-lazyseq-synchronized-methods-virtual-thread</link>
<description>&lt;p&gt;Hi&lt;br&gt;
I have a service written in Clojure 1.11 and service is running on Jetty 12.&lt;br&gt;
I tried to run  service on Java 21 using virtual threads in Jetty.&lt;br&gt;
There are following &lt;a rel=&quot;nofollow&quot; href=&quot;https://docs.oracle.com/en/java/javase/21/core/virtual-threads.html#GUID-704A716D-0662-4BC7-8C7F-66EE74B1EDAD&quot;&gt;constraints&lt;/a&gt; preventing virtual thread's unmounting:&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;A virtual thread cannot be unmounted during blocking operations when&lt;br&gt;
it is pinned to its carrier. A virtual thread is pinned in the&lt;br&gt;
following situations:&lt;br&gt;
- The virtual thread runs code inside a synchronized block or method&lt;br&gt;
- The virtual thread runs a native method or a foreign function (see Foreign Function and Memory API)&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;i ran service with '-Djdk.tracePinnedThreads=full' key and found that LazySeq&lt;br&gt;
 synchronized LazySeq &lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/clojure/clojure/blob/master/src/jvm/clojure/lang/LazySeq.java#L53&quot;&gt;methods&lt;/a&gt; pin the virtual thread:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;clojure.lang.LazySeq.sval(LazySeq.java:42) &amp;lt;== monitors:1
clojure.lang.LazySeq.seq(LazySeq.java:51) &amp;lt;== monitors:1
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;(By the way, same problem is raised by synchornized Delay deref &lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/clojure/clojure/blob/master/src/jvm/clojure/lang/Delay.java#L32&quot;&gt;method&lt;/a&gt;)&lt;/p&gt;
&lt;p&gt;As i see Clojure stdlib implementation and Clojure internal stuff itsself use 'synchronized' approach heavily which is incompatible with virtual thread constraints.&lt;br&gt;
I understand that there is a huge amount of code and it is mega expensive to adapt it to be virtual thread's friendly but maybe there are some plans about being compatible with virtual threads constraints.&lt;/p&gt;
&lt;p&gt;Thanks!&lt;/p&gt;
</description>
<category>Sequences</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/13318/clojure-lang-lazyseq-synchronized-methods-virtual-thread</guid>
<pubDate>Sat, 23 Sep 2023 07:31:15 +0000</pubDate>
</item>
<item>
<title>Immutable wrapper around Java arrays?</title>
<link>https://ask.clojure.org/index.php/13224/immutable-wrapper-around-java-arrays</link>
<description>&lt;p&gt;Are there any immutable wrapper around Java arrays in Clojure, similar to &lt;a rel=&quot;nofollow&quot; href=&quot;https://scala-lang.org/api/3.x/scala/collection/immutable/ArraySeq.html&quot;&gt;Scala's ArraySeq&lt;/a&gt;?&lt;/p&gt;
&lt;p&gt;What are your thoughts on introducing such a wrapper to Clojure's core?&lt;/p&gt;
</description>
<category>Collections</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/13224/immutable-wrapper-around-java-arrays</guid>
<pubDate>Mon, 28 Aug 2023 11:20:27 +0000</pubDate>
</item>
<item>
<title>Common operations to set var root - why not in clojure.core?</title>
<link>https://ask.clojure.org/index.php/13135/common-operations-to-set-var-root-why-not-in-clojure-core</link>
<description>&lt;p&gt;I have been using the following function a lot:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt; (defn set-var-root [v value]
   (alter-var-root v (constantly value)))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And I have seen this pattern a lot in other code too. It is like an idiom if I understand correctly.&lt;br&gt;
Why is it not in clojure.core?&lt;/p&gt;
&lt;p&gt;Might it be a good addition and if not, why not?&lt;/p&gt;
&lt;p&gt;I have asked in clojurians slack and was pointed out to&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(.bindRoot avar value)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Which I didn't know before also. But again the question why is it not part of the public API? Is there something wrong with using vars like this?&lt;/p&gt;
&lt;p&gt;To give some context, I am developing a game engine and on app start I am setting global vars for input, graphics context, etc. &lt;/p&gt;
&lt;p&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/damn/gdl/blob/main/src/gdl/game.clj#L13&quot;&gt;https://github.com/damn/gdl/blob/main/src/gdl/game.clj#L13&lt;/a&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(defn- load-gdx-globals []
  (set-var-root #'gdl.app/app           Gdx/app)
  (set-var-root #'gdl.files/files       Gdx/files)
  (set-var-root #'gdl.graphics/graphics Gdx/graphics)
  (set-var-root #'gdl.input/input       Gdx/input))
&lt;/code&gt;&lt;/pre&gt;
</description>
<category>Clojure</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/13135/common-operations-to-set-var-root-why-not-in-clojure-core</guid>
<pubDate>Mon, 07 Aug 2023 09:10:44 +0000</pubDate>
</item>
<item>
<title>what does ^:once meta applied for anon function mean?</title>
<link>https://ask.clojure.org/index.php/13050/what-does-once-meta-applied-for-anon-function-mean</link>
<description>&lt;p&gt;hi!&lt;br&gt;
what does ^:once meta applied for anon function mean?&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(db-query-with-resultset* db sql params
                           (^:once fn* [rset]
                             (process-result-set rset opts))
                          opts)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Thanks &lt;/p&gt;
</description>
<category>Metadata</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/13050/what-does-once-meta-applied-for-anon-function-mean</guid>
<pubDate>Sun, 02 Jul 2023 13:07:57 +0000</pubDate>
</item>
<item>
<title>Is there any performance difference between creating vector via (into [] xs) or (vec xs)?</title>
<link>https://ask.clojure.org/index.php/12996/there-performance-difference-between-creating-vector-into</link>
<description>&lt;p&gt;I can create set/map/vector by two ways:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(into #{} xs) ;; creates PersistentHashSet
(set xs)      ;; creates PersistentHashSet

(into {} xs)  ;; creates PersistentArrayMap or PersistentHashMap (depends on xs size) 
(apply hash-map xs) ;; creates PersistentHashMap

(into [] xs)  ;; creates PersistentVector
(vec xs)      ;; creates PersistentVector
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;(into r xs):&lt;br&gt;
- defines &lt;em&gt;generic&lt;/em&gt; algorithm by conj!-ing input sequence 1 by 1 to passed result collection (uses transient/persistent trick)&lt;/p&gt;
&lt;p&gt;(ctor xs):&lt;br&gt;
- defines precisely type of result data structure&lt;br&gt;
- passes xs sequence as an argument to constructor (which is static java function) so every ctor may define its own logic of creation (maybe by not conj-ing input sequence 1 by 1)&lt;/p&gt;
&lt;p&gt;Are there any performance differences between these two ways?&lt;br&gt;
Maybe there is an established idiom what to use when?&lt;/p&gt;
&lt;p&gt;Thanks!&lt;/p&gt;
</description>
<category>Collections</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/12996/there-performance-difference-between-creating-vector-into</guid>
<pubDate>Sat, 03 Jun 2023 11:35:12 +0000</pubDate>
</item>
<item>
<title>best way to conditionally def a symbol and do some side effect if symbol does not exist?</title>
<link>https://ask.clojure.org/index.php/12934/best-conditionally-symbol-some-side-effect-symbol-does-exist</link>
<description>&lt;p&gt;I was surprised that this code in dev/user.clj doesn't run at start up:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(ns user) ; user ns is loaded by REPL startup

(println &quot;Evaluating dev/user.clj&quot;)
(when-not (resolve 'xxx)
  (def xxx 1)
  (println &quot;defing xxx&quot;))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This worked for me:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(defonce yyy (let [tmp 1] (println (str &quot;deffing yyy as &quot; tmp))
                   tmp))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Something like this was also suggested to me in the clojurians &lt;br&gt;
 [https://clojurians.slack.com/archives/C053AK3F9/p1683882344251029][Slack] :&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(ns user)

(when-not (resolve 'xxx)
  (intern 'user 'xxx 42)
  (prn &quot;!!!&quot;))
&lt;/code&gt;&lt;/pre&gt;
</description>
<category>Namespaces and vars</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/12934/best-conditionally-symbol-some-side-effect-symbol-does-exist</guid>
<pubDate>Fri, 12 May 2023 10:38:38 +0000</pubDate>
</item>
<item>
<title>Clojure equivalent of kotlins '?'</title>
<link>https://ask.clojure.org/index.php/12814/clojure-equivalent-of-kotlins</link>
<description>&lt;p&gt;Kotlin has a '?' operator which can be used to handle nullable types:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;fun f(nullable: String?) {
  val nullable_length = nullable?.length;
  val length_or_zero = nullable_length ?: 0;
  val prefixed_nullable = nullable?.let {
    &quot;prefix_&quot; + it
  }
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Here, &lt;code&gt;nullable_length&lt;/code&gt; is either an integer or null. &lt;code&gt;length_or_zero&lt;/code&gt; is either the length of string or &lt;code&gt;0&lt;/code&gt; if it was null. &lt;code&gt;prefixed_nullable&lt;/code&gt; is either null or the original string with a prefix, depending on whether it was null.&lt;/p&gt;
&lt;p&gt;What is the clojure equivalent of this feature for dealing with &lt;code&gt;nil&lt;/code&gt;s?&lt;/p&gt;
</description>
<category>Clojure</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/12814/clojure-equivalent-of-kotlins</guid>
<pubDate>Wed, 29 Mar 2023 06:27:40 +0000</pubDate>
</item>
<item>
<title>calling take-nth with 0 causes an OutOfMemoryError, and kills the repl.</title>
<link>https://ask.clojure.org/index.php/12736/calling-take-nth-with-causes-outofmemoryerror-and-kills-repl</link>
<description>&lt;p&gt;calling take-nth with 0 causes an OutOfMemoryError, and kills the repl.  Perhaps a validation &amp;gt; 0 required here?&lt;/p&gt;
&lt;p&gt;to reproduce,&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;user=&amp;gt; (take-nth 0 [1 2])
OutOfMemoryError Java heap space
        reply.eval-modes.nrepl/session-responses (nrepl.clj:51)
        reply.eval-modes.nrepl/session-responses (nrepl.clj:51)
        reply.eval-modes.nrepl/session-responses/fn--1340 (nrepl.clj:56)
        clojure.lang.LazySeq.sval (LazySeq.java:42)
        clojure.lang.LazySeq.seq (LazySeq.java:51)
        clojure.lang.RT.seq (RT.java:535)
        clojure.core/seq--5467 (core.clj:139)
        clojure.core/filter/fn--5962 (core.clj:2826)
        clojure.lang.LazySeq.sval (LazySeq.java:42)
        clojure.lang.LazySeq.seq (LazySeq.java:58)
        clojure.lang.RT.seq (RT.java:535)
        clojure.core/seq--5467 (core.clj:139)
Bye for now!
Error printing return value (OutOfMemoryError) at clojure.core/take-nth (core.clj:4289).
&lt;/code&gt;&lt;/pre&gt;
</description>
<category>Transducers</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/12736/calling-take-nth-with-causes-outofmemoryerror-and-kills-repl</guid>
<pubDate>Wed, 08 Mar 2023 07:59:54 +0000</pubDate>
</item>
<item>
<title>Using proxy to extend MemoryStream fails on Close</title>
<link>https://ask.clojure.org/index.php/12549/using-proxy-to-extend-memorystream-fails-on-close</link>
<description>&lt;p&gt;For some unit tests, I need to be able to read the contents of a &lt;code&gt;MemoryStream&lt;/code&gt; after it has been closed. As this isn't possible, I want to create a memory stream proxy where the call to &lt;code&gt;Close&lt;/code&gt; is a no-op, allowing me to read from it even when code outside of my control has attemped closing it. This however won't work.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;REPL&lt;/strong&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(import [System.IO MemoryStream StreamWriter])
(let [stream (proxy [MemoryStream] [] (Close [] (println &quot;close!&quot;)))]
  (doto (StreamWriter. stream)
        (.Write &quot;testing&quot;)
        (.Close)))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;output&lt;/strong&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;close!
Execution error (InvalidProgramException) at System.Diagnostics.StackFrame/Write (NO_FILE:0).
Cannot create boxed ByRef-like values.
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;While it's evident my proxied &lt;code&gt;Close&lt;/code&gt; method is called, an error is thrown just after.&lt;/p&gt;
</description>
<category>ClojureCLR</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/12549/using-proxy-to-extend-memorystream-fails-on-close</guid>
<pubDate>Sun, 15 Jan 2023 00:08:50 +0000</pubDate>
</item>
<item>
<title>slurp without encoding prints erroneous warning</title>
<link>https://ask.clojure.org/index.php/12524/slurp-without-encoding-prints-erroneous-warning</link>
<description>&lt;p&gt;ClojureCLR &lt;code&gt;slurp&lt;/code&gt; erroneously prints a warning unless encoding is explicitly provided:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;$ touch foo

$ clojure -M -e '(slurp &quot;foo&quot;)'
&quot;&quot;

$ Clojure.Main -e '(slurp &quot;foo&quot;)'
WARNING: (slurp f enc) is deprecated, use (slurp f :encoding enc).
&quot;&quot;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The cause of this is found in the &lt;code&gt;normalize-slurp-opts&lt;/code&gt; function, which differs from the JVM version &lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/clojure/clojure-clr/blob/44aeb6f59053f3e5005fa5eb7947805f035976e0/Clojure/Clojure.Source/clojure/core.clj#L7012&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;
</description>
<category>ClojureCLR</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/12524/slurp-without-encoding-prints-erroneous-warning</guid>
<pubDate>Fri, 06 Jan 2023 15:53:49 +0000</pubDate>
</item>
<item>
<title>cant call function from clojure.core, dont know what's messed</title>
<link>https://ask.clojure.org/index.php/12498/cant-call-function-from-clojure-core-dont-know-whats-messed</link>
<description>&lt;p&gt;So, i've been refreshing the language a bit since im still trying to make my clojure knowledge permanent on long-term memory. Had setup a Remote WSL 2.0 connection to an Arch Linux using VSCode on the host: Windows 11 since i default to developing in Linux for everything.&lt;/p&gt;
&lt;p&gt;Problem is simple i guess, but im unable to pinpoint.&lt;/p&gt;
&lt;p&gt;trying to call  (read-line) from clojure.core renders an &lt;code&gt;Could not resolve symbol: read-line&lt;/code&gt; in my Calva's output, which is using Joyride to start the REPL.&lt;/p&gt;
&lt;p&gt;isn't core loaded by default? what's going on &lt;/p&gt;
&lt;pre&gt;&lt;code&gt;;checking if ns is the problem
(ns encoder)

;repl output check
(defn queue [] 0)
(println str (queue))

(def files-list)
;test
(do (print &quot;Name yourself now: &quot;)
    (flush)
    (read-line)) =&amp;gt; Could not resolve symbol: read-line

;debug test
(println &quot;Enter (literal) filename to encode&amp;gt; &quot;)
(def input (read-line)) =&amp;gt; Could not resolve symbol: read-line
(println (str &quot;File to encode: &quot; input))
;cant call read-line from core? wtf

;finish this
(defn add-queue-confirm [filenames]
  (println &quot;Files to process: &quot; filenames))
&lt;/code&gt;&lt;/pre&gt;
</description>
<category>REPL</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/12498/cant-call-function-from-clojure-core-dont-know-whats-messed</guid>
<pubDate>Fri, 23 Dec 2022 14:26:05 +0000</pubDate>
</item>
<item>
<title>Potential race condition in requiring-resolve?</title>
<link>https://ask.clojure.org/index.php/12346/potential-race-condition-in-requiring-resolve</link>
<description>&lt;p&gt;Hello! I've been using a few of the functions from &lt;code&gt;clojure.tools.build.api&lt;/code&gt; in a &lt;code&gt;future&lt;/code&gt; and I sporadically see this error...&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;Execution error (IllegalStateException) at clojure.tools.build.api/create-basis (api.clj:154).
Attempting to call unbound fn: #'clojure.tools.build.tasks.create-basis/create-basis
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;From what I see in tools.build...&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(defn create-basis
  ([]
   ((requiring-resolve 'clojure.tools.build.tasks.create-basis/create-basis)))
  ([params]
   ((requiring-resolve 'clojure.tools.build.tasks.create-basis/create-basis) params)))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;...the code is only really using &lt;code&gt;requiring-resolve&lt;/code&gt; to execute another function. Diving deeper into &lt;code&gt;requiring-resolve&lt;/code&gt; I can see that the first call to &lt;code&gt;resolve&lt;/code&gt; is not synchronized with the same lock that the &lt;code&gt;serialized-require&lt;/code&gt; function uses...&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(defn- serialized-require
  [&amp;amp; args]
  (locking clojure.lang.RT/REQUIRE_LOCK
    (apply require args)))

(defn requiring-resolve
  [sym]
  (if (qualified-symbol? sym)
    (or (resolve sym)
        (do (-&amp;gt; sym namespace symbol serialized-require)
            (resolve sym)))
    (throw (IllegalArgumentException. (str &quot;Not a qualified symbol: &quot; sym)))))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;First, is it possible &lt;code&gt;requiring-resolve&lt;/code&gt; is actually returning &lt;code&gt;clojure.lang.Var$Unbound&lt;/code&gt; from the first call to &lt;code&gt;resolve&lt;/code&gt; when another thread is in the process of requiring that namespace via &lt;code&gt;serialized-require&lt;/code&gt;? Assuming so, should &lt;code&gt;tools.build&lt;/code&gt; (and I guess by extension most Clojure code) avoid using the &lt;code&gt;((requiring-resolve 'some-other/function))&lt;/code&gt; pattern? Is it possible &lt;code&gt;requiring-resolve&lt;/code&gt; could be modified to be thread safe?&lt;/p&gt;
&lt;p&gt;Thanks!&lt;/p&gt;
</description>
<category>Namespaces and vars</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/12346/potential-race-condition-in-requiring-resolve</guid>
<pubDate>Mon, 31 Oct 2022 05:20:05 +0000</pubDate>
</item>
<item>
<title>Add interleave-all (like partition-all is to partition)</title>
<link>https://ask.clojure.org/index.php/12125/add-interleave-all-like-partition-all-is-to-partition</link>
<description>&lt;p&gt;At work we have some situations where we need to interleave lazy sequences in such a way that if one sequence is shorter than the other we want elements from the longer sequence to be concatenated after the interleaving runs out of the shorter sequence(s).&lt;/p&gt;
&lt;p&gt;The following code could be used -- a slight variant of the existing &lt;code&gt;interleave&lt;/code&gt; function:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(defn interleave-all
  &quot;Like interleave, but stops when the longest seq is done, instead of
   the shortest.&quot;
  {:copyright &quot;Rich Hickey, since this is a modified version of interleave&quot;}
  ([] ())
  ([c1] (lazy-seq c1))
  ([c1 c2]
   (lazy-seq
    (let [s1 (seq c1) s2 (seq c2)]
      (cond
       (and s1 s2) ; there are elements left in both
       (cons (first s1) (cons (first s2)
                              (interleave-all (rest s1) (rest s2))))
       s1 ; s2 is done
       s1
       s2 ; s1 is done
       s2))))
  ([c1 c2 &amp;amp; colls]
   (lazy-seq
    (let [ss (filter identity (map seq (conj colls c2 c1)))]
      (concat (map first ss) (apply interleave-all (map rest ss)))))))
&lt;/code&gt;&lt;/pre&gt;
</description>
<category>Sequences</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/12125/add-interleave-all-like-partition-all-is-to-partition</guid>
<pubDate>Wed, 24 Aug 2022 18:47:25 +0000</pubDate>
</item>
<item>
<title>dedupe and partition-by depending on magic value</title>
<link>https://ask.clojure.org/index.php/11835/dedupe-and-partition-by-depending-on-magic-value</link>
<description>&lt;p&gt;I noticed &lt;code&gt;dedupe&lt;/code&gt; and the transducer-returning arity of &lt;code&gt;partition-by&lt;/code&gt; use the magic value &lt;code&gt;:clojure.core/none&lt;/code&gt; to indicate the start of processing. There are some cases where this results in an incorrect result:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;;;;; dedupe

(dedupe
 [:clojure.core/none 1 2 3])
;;=&amp;gt; (1 2 3)
;; expected: (:clojure.core/none 1 2 3)

(dedupe
 [:clojure.core/none :clojure.core/none 1 2 3])
;;=&amp;gt; (1 2 3)
;; expected: (:clojure.core/none 1 2 3)

;; transducing arity is also affected
(sequence
 (dedupe)
 [:clojure.core/none 1 2 3])
;;=&amp;gt; (1 2 3)
;; expected: (:clojure.core/none 1 2 3)
&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;;;;; partition-by

(sequence (partition-by
           {0 0
            1 :clojure.core/none
            2 2})
          (range 3))
;;=&amp;gt; ([0] [1 2])
;; expected: ([0] [1] [2])

;; non-trasducing arity works OK:
(partition-by
 {0 0
  1 :clojure.core/none
  2 2}
 (range 3))
;;=&amp;gt; ((0) (1) (2))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Is this a conscious tradeoff for performance or is it something that might use a bit of additional thinking about better handling?&lt;/p&gt;
</description>
<category>Clojure</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/11835/dedupe-and-partition-by-depending-on-magic-value</guid>
<pubDate>Thu, 28 Apr 2022 14:57:11 +0000</pubDate>
</item>
<item>
<title>Shorthand for if-let and when-let combined to not-empty or seq functions</title>
<link>https://ask.clojure.org/index.php/11733/shorthand-for-let-and-when-let-combined-not-empty-functions</link>
<description>&lt;p&gt;This functions which I think they are usually used could be shortened&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(let [coll (get-coll)]
  (if (seq coll) 
    (handle-true coll)
    (handle-false)))

(when-let [x (seq ...)])

(if-let [x (seq ...)])
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Like:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(if-seq-let [coll (get-coll)]
  (handle-true coll)
  (handle-false))

(when-seq-let [c (get-coll)]
  (handle-true1 coll)
  (handle-true2 coll))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Proposal&lt;/strong&gt; based on if-let and when-let.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(defmacro ^{:private true} assert-args
  [&amp;amp; pairs]
  `(do (when-not ~(first pairs)
         (throw (IllegalArgumentException.
                  (str (first ~'&amp;amp;form) &quot; requires &quot; ~(second pairs) &quot; in &quot; ~'*ns* &quot;:&quot; (:line (meta ~'&amp;amp;form))))))
       ~(let [more (nnext pairs)]
          (when more
            (list* `assert-args more)))))

(defmacro if-seq-let
  &quot;bindings =&amp;gt; binding-form test

  If test is seq, evaluates then with binding-form bound to the value of
  test, if not, yields else&quot;
  ([bindings then]
   `(if-let ~bindings ~then nil))
  ([bindings then else &amp;amp; oldform]
   (assert-args
     (vector? bindings) &quot;a vector for its binding&quot;
     (nil? oldform) &quot;1 or 2 forms after binding vector&quot;
     (= 2 (count bindings)) &quot;exactly 2 forms in binding vector&quot;)
   (let [form (bindings 0) tst (bindings 1)]
     `(let [temp# ~tst]
        (if (seq temp#)
          (let [~form temp#]
            ~then)
          ~else)))))

(defmacro when-seq-let
  &quot;bindings =&amp;gt; binding-form test

  When test is seq, evaluates body with binding-form bound to the value of test&quot;
  [bindings &amp;amp; body]
  (assert-args
    (vector? bindings) &quot;a vector for its binding&quot;
    (= 2 (count bindings)) &quot;exactly 2 forms in binding vector&quot;)
  (let [form (bindings 0) tst (bindings 1)]
    `(let [temp# ~tst]
       (when (seq temp#)
         (let [~form temp#]
           ~@body)))))
&lt;/code&gt;&lt;/pre&gt;
</description>
<category>Clojure</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/11733/shorthand-for-let-and-when-let-combined-not-empty-functions</guid>
<pubDate>Tue, 05 Apr 2022 12:12:28 +0000</pubDate>
</item>
<item>
<title>Hash collisions in case statements try to evaluate the constants</title>
<link>https://ask.clojure.org/index.php/11395/hash-collisions-case-statements-try-evaluate-the-constants</link>
<description>&lt;p&gt;When &lt;code&gt;case&lt;/code&gt; statements use hashing and have a hash collision, they embed a &lt;code&gt;condp&lt;/code&gt; expression. However, this expression inserts the constants directly, without quoting. If any constants include a symbol, then these will lead to an error.&lt;/p&gt;
&lt;p&gt;For example:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(case 'a #{a} 1 :foo 2 a 3)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Leads to: &lt;code&gt;Unable to resolve symbol: a in this context&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;This can be seen when expanding the macro:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(macroexpand '(case 'a #{a} 1 :foo 2 a 3))
(let* [G__151 (quote a)] (case* G__151 0 1 (throw (java.lang.IllegalArgumentException. (clojure.core/str &quot;No matching clause: &quot; G__151))) {0 [-1640525200 (clojure.core/condp clojure.core/= G__151 #{a} 1 a 3 (throw (java.lang.IllegalArgumentException. (clojure.core/str &quot;No matching clause: &quot; G__151))))], 1 [:foo 2]} :compact :hash-equiv #{0}))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This shows a call to &lt;code&gt;case*&lt;/code&gt;. The first and second arguments are 0, 1. When these are used with &lt;code&gt;clojure.core/shift-mask&lt;/code&gt; both &lt;code&gt;'#{a}&lt;/code&gt; and &lt;code&gt;'a&lt;/code&gt; will hash to &lt;code&gt;0&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;The 5th argument contains a map of hash codes to values, which contains:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;{0 [-1640525200 (clojure.core/condp clojure.core/= G__151
                  #{a} 1
                  a 3
                  (throw (java.lang.IllegalArgumentException. (clojure.core/str &quot;No matching clause: &quot; G__151))))],
 1 [:foo 2]}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This &lt;code&gt;condp&lt;/code&gt; block contains unquoted values for &lt;code&gt;#{a}&lt;/code&gt; and &lt;code&gt;a&lt;/code&gt;, so they will incorrectly attempt to match on the value of the current binding for &lt;code&gt;a&lt;/code&gt; (if such a binding exists).&lt;/p&gt;
&lt;p&gt;Since &lt;code&gt;case&lt;/code&gt; will interpret every test-constant as a literal (explicitly stating that they need not be quoted), these values should be quoted when they appear in an embedded Clojure expression like this this &lt;code&gt;condp&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;The following is a suggested patch to correct the generated code:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;diff --git a/src/clj/clojure/core.clj b/src/clj/clojure/core.clj
index 0dba6fcd..b21d4556 100644
--- a/src/clj/clojure/core.clj
+++ b/src/clj/clojure/core.clj
@@ -6690,7 +6690,7 @@ fails, attempts to require sym's namespace and retries.&quot;
                       (next ks) (next vs))
                     m))
         assoc-multi (fn [m h bucket]
-                      (let [testexprs (apply concat bucket)
+                      (let [testexprs (mapcat (fn [kv] [(list 'quote (first kv)) (second kv)]) bucket)
                             expr `(condp = ~expr-sym ~@testexprs ~default)]
                         (assoc m h expr)))
         hmap (reduce1
diff --git a/test/clojure/test_clojure/control.clj b/test/clojure/test_clojure/control.clj
index 92846ad3..f3fe436b 100644
--- a/test/clojure/test_clojure/control.clj
+++ b/test/clojure/test_clojure/control.clj
@@ -421,7 +421,14 @@
          :b 1
          :c -2
          :d 4294967296
-         :d 3))
+         :d 3)
+    (are [result input] (= result (case input
+                                    #{a} :set
+                                    :foo :keyword
+                                    a :symbol))
+         :symbol 'a
+         :keyword :foo
+         :set '#{a}))
   (testing &quot;test warn for hash collision&quot;
     (should-print-err-message
      #&quot;Performance warning, .*:\d+ - hash collision of some case test constants; if selected, those entries will be tested sequentially..*\r?\n&quot;
&lt;/code&gt;&lt;/pre&gt;
</description>
<category>Macros</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/11395/hash-collisions-case-statements-try-evaluate-the-constants</guid>
<pubDate>Mon, 13 Dec 2021 20:54:22 +0000</pubDate>
</item>
<item>
<title>Does the `any?` function have the correct behavior?</title>
<link>https://ask.clojure.org/index.php/11260/does-the-any-function-have-the-correct-behavior</link>
<description>&lt;p&gt;Clojure's &lt;code&gt;any?&lt;/code&gt; function seems to just return &lt;code&gt;true&lt;/code&gt; always.&lt;br&gt;
&lt;code&gt;(defn any?
  &quot;Returns true given any argument.&quot;
  {:tag Boolean
   :added &quot;1.9&quot;}
  [x] true)&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Contrast this with &lt;code&gt;not-any?&lt;/code&gt;.&lt;br&gt;
&lt;code&gt;(def
 ^{:tag Boolean
   :doc &quot;Returns false if (pred x) is logical true for any x in coll,
  else true.&quot;
   :arglists '([pred coll])
   :added &quot;1.0&quot;}
 not-any? (comp not some))&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;I believe &lt;code&gt;any?&lt;/code&gt; should take two arguments and return &lt;code&gt;true&lt;/code&gt; if any predicate is &lt;code&gt;true&lt;/code&gt;.&lt;br&gt;
&lt;code&gt;(def
 ^{:tag Boolean
   :doc &quot;Returns true if (pred x) is logical true for any x in coll,
  else false.&quot;
   :arglists '([pred coll])
   :added &quot;1.9&quot;}
 any? (comp boolean some))&lt;/code&gt;&lt;br&gt;
&lt;a rel=&quot;nofollow&quot; href=&quot;https://clojuredocs.org/clojure.core/any_q&quot;&gt;Credit to Kofrasa on ClojureDocs.&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Here are some tests to run:&lt;/p&gt;
&lt;p&gt;&lt;code&gt;`&lt;/code&gt;&lt;br&gt;
(ns user&lt;br&gt;
  (:require [clojure.test :refer [are deftest]]&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;        [clojure.test.check.clojure-test :refer [defspec]]
        [clojure.test.check.generators :as gen]
        [clojure.test.check.properties :as prop])
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;  (:refer-clojure :exclude [any?]))&lt;/p&gt;
&lt;p&gt;(deftest clojure-any&lt;br&gt;
  (are [x y] (= ((comp boolean some) true? x) (clojure.core/any? y))&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;[true] true
[false] false ;; this fails
[true true] true
[false true] true
[false false] false ;; so does this
))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;(def&lt;br&gt;
 ^{:tag Boolean&lt;br&gt;
   :doc &quot;Returns true if (pred x) is logical true for any x in coll,&lt;br&gt;
  else false.&quot;&lt;br&gt;
   :arglists '([pred coll])&lt;br&gt;
   :added &quot;1.9&quot;}&lt;br&gt;
 any? (comp boolean some))&lt;/p&gt;
&lt;p&gt;(defspec correct-any 1000&lt;br&gt;
  (prop/for-all [v (gen/such-that not-empty (gen/vector gen/boolean))]&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;            (= ((comp boolean some) true? v)
               (any? true? v)
               (not (not-any? true? v)))))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code&gt;`&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Apologies for the messed-up formatting.&lt;br&gt;
&lt;a rel=&quot;nofollow&quot; href=&quot;https://gist.github.com/wildwestrom/9568e9c659d30a2a663a9a0a8235a6b9&quot;&gt;https://gist.github.com/wildwestrom/9568e9c659d30a2a663a9a0a8235a6b9&lt;/a&gt;&lt;/p&gt;
</description>
<category>Syntax and reader</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/11260/does-the-any-function-have-the-correct-behavior</guid>
<pubDate>Tue, 09 Nov 2021 22:30:57 +0000</pubDate>
</item>
<item>
<title>What would transducers-first Clojure look like?</title>
<link>https://ask.clojure.org/index.php/11132/what-would-transducers-first-clojure-look-like</link>
<description>&lt;p&gt;Rich says in the &lt;a rel=&quot;nofollow&quot; href=&quot;https://clojure.org/about/history&quot;&gt;History of Clojure&lt;/a&gt;:&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;I think transducers are a fundamental primitive that decouples critical logic from list/sequence processing and construction, and if I had Clojure to do all over I would put them at the bottom.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;This fascinates me, and I am really curious what would transducers-first Clojure look like? I don't really expect Rich to answer here (I should have asked this at his HOPL talk...) but perhaps someone who is closer to core might have some insight.&lt;/p&gt;
&lt;p&gt;I would love to see a Clojure where transducers are the default and &lt;em&gt;easiest&lt;/em&gt; way to solve things, where all the tutorials and gists and example code everywhere around would be using them and a context (vector, lazy, first etc.) was only chosen at the edges. Currently it is &lt;em&gt;easier&lt;/em&gt; (but perhaps not &lt;em&gt;simpler&lt;/em&gt;) to write lazy code than transducing code.&lt;/p&gt;
&lt;p&gt;I am wondering if transducers-first Clojure would address the above or would it mostly be about the plumbing behind a clojure.core similar to the current one.&lt;/p&gt;
&lt;p&gt;For my personal case, I actively have to fight laziness - I don't think I encountered a single place in our codebase where it would be beneficial but as it's still the easiest way of writing clojure I have to put in extra effort to not accidentally venture over to lazy land and to spot these patterns in PRs of others.&lt;/p&gt;
&lt;p&gt;I would love a clojure.core2 where transducers are the default, it's easy to write transducing code, potentially with less typing/ceremonies, the likes of comp and into [] etc. And stepping over to lazy land is always an opt-in thing, like the impure parts of clojure - stm, transients etc. I'm not entirely sure how a core library like that would look but it's something I'd love to try.&lt;/p&gt;
&lt;p&gt;Moreover, there are some transducer-specific optimisations like &lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/cgrand/xforms#on-key-value-pairs&quot;&gt;https://github.com/cgrand/xforms#on-key-value-pairs&lt;/a&gt; that could be considered. And perhaps there could also be a way to use the multiple-coll arities of map as part of a transducer somehow.&lt;/p&gt;
&lt;p&gt;There could also be more baked-in support for an xform arg in functions that currently take a coll to preprocess them, like run! and str/join - if you actively use transducers you start to notice things like 'hey, I don't even need a collection there' and start to look for different transducing contexts - so instead of (count (into [] xform input)) you'd go (x/count xform input)&lt;/p&gt;
</description>
<category>Transducers</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/11132/what-would-transducers-first-clojure-look-like</guid>
<pubDate>Sat, 02 Oct 2021 19:32:19 +0000</pubDate>
</item>
<item>
<title>using `proxy` might make an otherwise additive change breaking</title>
<link>https://ask.clojure.org/index.php/10383/using-proxy-might-make-otherwise-additive-change-breaking</link>
<description>&lt;p&gt;Given an interface&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;public interface Foo {
    void bar(Object o);
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And a proxy over that interface:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(proxy [Foo] []
  (bar [o] (baz o)))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;When the interface gets updated to&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;public interface Foo {
  void bar(Object o);

  // java 8 default method, quote from 
  // https://docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html
  // &quot;Default methods enable you to add new functionality to the
  // interfaces of your libraries and ensure binary compatibility
  // with code written for older versions of those interfaces.&quot;
  default void bar(Object o, Object p) {
    bar(o);
  }
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Calls to &lt;code&gt;instanceCreatedByProxy.bar(o, p)&lt;/code&gt; will not work as it could be expected.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;reify&lt;/code&gt; is not affected by this.&lt;/p&gt;
&lt;p&gt;Repro repo: &lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/imrekoszo/proxy-default&quot;&gt;https://github.com/imrekoszo/proxy-default&lt;/a&gt;&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;There is no indication of this behavior &lt;a rel=&quot;nofollow&quot; href=&quot;https://clojure.github.io/clojure/clojure.core-api.html#clojure.core/proxy&quot;&gt;in the docstring&lt;/a&gt; of &lt;code&gt;proxy&lt;/code&gt;, the only reference I saw to this is an &lt;a rel=&quot;nofollow&quot; href=&quot;https://clojuredocs.org/clojure.core/proxy#example-542692d4c026201cdc327040&quot;&gt;example on clojuredocs&lt;/a&gt;.&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;This is troublesome as it might prevent an otherwise non-breaking library upgrade. If the code calling &lt;code&gt;proxy&lt;/code&gt; is under one's control they could use &lt;code&gt;reify&lt;/code&gt; instead (especially as this is &lt;a rel=&quot;nofollow&quot; href=&quot;https://clojure.org/reference/datatypes#_reify&quot;&gt;recommended in the official docs&lt;/a&gt;). However if it is in an external library this is not a viable workaround.&lt;/p&gt;
&lt;p&gt;Is this a bug or a known limitation of &lt;code&gt;proxy&lt;/code&gt;?&lt;br&gt;
If the latter, then could this perhaps be made more visible, for example through a warning in the docstring?&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;Edit:&lt;br&gt;
- &lt;a rel=&quot;nofollow&quot; href=&quot;https://clojurians-log.clojureverse.org/clojure/2021-03-29/1617036667.062000&quot;&gt;link to original slack thread&lt;/a&gt;&lt;br&gt;
- added link to &lt;code&gt;reify&lt;/code&gt; recommendation&lt;/p&gt;
</description>
<category>Clojure</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/10383/using-proxy-might-make-otherwise-additive-change-breaking</guid>
<pubDate>Tue, 30 Mar 2021 10:33:11 +0000</pubDate>
</item>
<item>
<title>Unnecessary check in case?</title>
<link>https://ask.clojure.org/index.php/10271/unnecessary-check-in-case</link>
<description>&lt;p&gt;Given&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(let [c (char 110)]
        (case c \n (println &quot;lol&quot;)))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If I run &lt;code&gt;decompile&lt;/code&gt; from &lt;a rel=&quot;nofollow&quot; href=&quot;https://clojars.org/com.clojure-goes-fast/clj-java-decompiler&quot;&gt;https://clojars.org/com.clojure-goes-fast/clj-java-decompiler&lt;/a&gt;&lt;br&gt;
I get the following output:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;public final class json$fn__15103 extends AFunction {
public static final Object const__2;
public static final Var const__3;
public static final Var const__4;

public static Object invokeStatic() {
    final char G__15104;
    final char c = G__15104 = RT.charCast(110L);
    switch (Util.hash(G__15104)) {
        case 110: {
            if (Util.equiv((Object)G__15104, json$fn__15103.const__2)) {
                return ((IFn)json$fn__15103.const__3.getRawRoot()).invoke(&quot;lol&quot;);
            }
            break;
        }
    }
    throw new IllegalArgumentException((String)((IFn)json$fn__15103.const__4.getRawRoot()).invoke(&quot;No matching clause: &quot;, G__15104));
}

@Override
public Object invoke() {
    return invokeStatic();
}

static {
    const__2 = 'n';
    const__3 = RT.var(&quot;clojure.core&quot;, &quot;println&quot;);
    const__4 = RT.var(&quot;clojure.core&quot;, &quot;str&quot;);
} 
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;My question is (given that this decompilation is correct) why does the compiler emit the &lt;code&gt;if&lt;/code&gt; test, and more interestingly, is there a way to get rid of it (by means of instructing the compiler?). &lt;/p&gt;
&lt;p&gt;It might also be that I should not care about this because the JIT will take care of it for me, but still.&lt;/p&gt;
&lt;p&gt;Also, one could argue, I guess, that since we know that the thing I'm case'ing on is a &lt;code&gt;char&lt;/code&gt; there would be no need to do a &lt;code&gt;Util.hash&lt;/code&gt; on it?&lt;/p&gt;
</description>
<category>Compiler</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/10271/unnecessary-check-in-case</guid>
<pubDate>Sun, 28 Feb 2021 10:07:12 +0000</pubDate>
</item>
<item>
<title>clojure.core/case behaves weirdly for certain inputs</title>
<link>https://ask.clojure.org/index.php/10020/clojure-core-case-behaves-weirdly-for-certain-inputs</link>
<description>&lt;p&gt;&lt;code&gt;case&lt;/code&gt; seems to behave inconsistently  for collections containing &lt;code&gt;java.lang.Integer&lt;/code&gt;s.&lt;br&gt;
For example, take this function:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(defn test-case [v]
  (case v
    [1 -1] :one
    [1] :two
    :default))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Calling it with &lt;code&gt;[1 -1]&lt;/code&gt; or &lt;code&gt;[1]&lt;/code&gt; as an argument yields the expected result, and so does calling it with &lt;code&gt;[(int 1)]&lt;/code&gt; (=&amp;gt; &lt;code&gt;:one&lt;/code&gt;). However, with two elements, this does not work anymore:&lt;/p&gt;
&lt;p&gt;&lt;code&gt;(test-case [(int 1) (int -1)])&lt;/code&gt; =&amp;gt; &lt;code&gt;:default&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;To me this seems like a bug, considering that &lt;code&gt;(= [1 -1] [(int 1) (int -1)])&lt;/code&gt; =&amp;gt; &lt;code&gt;true&lt;/code&gt;. &lt;/p&gt;
&lt;p&gt;Is there anything I'm missing here?&lt;/p&gt;
&lt;p&gt;This was tested on Clojure version &lt;code&gt;1.10.1.547&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Edit: only now saw the &lt;a rel=&quot;nofollow&quot; href=&quot;https://ask.clojure.org/index.php/2374/case-fails-for-vectors-with-negative-numbers&quot;&gt;relevant issue&lt;/a&gt;. Sorry about that.&lt;/p&gt;
</description>
<category>Clojure</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/10020/clojure-core-case-behaves-weirdly-for-certain-inputs</guid>
<pubDate>Wed, 06 Jan 2021 00:59:43 +0000</pubDate>
</item>
<item>
<title>Suspected bug in `clojure.core/partition`</title>
<link>https://ask.clojure.org/index.php/9937/suspected-bug-in-clojure-core-partition</link>
<description>&lt;p&gt;2 problems:&lt;br&gt;
- The docstring of clojure.core/partition is not clear on what &quot;last partition&quot; means.&lt;br&gt;
- The behavior of partition is strange, IHMO it has a bug.&lt;/p&gt;
&lt;p&gt;Please take a look at this:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(partition 3 1 [:a :b :c] (range 5))

; actual behavior
; =&amp;gt; ((0 1 2) (1 2 3) (2 3 4) (3 4 :a))

; expected behavior, option 1
; =&amp;gt; ((0 1 2) (1 2 3) (2 3 4))

; expected behavior, option 2
; =&amp;gt; ((0 1 2) (1 2 3) (2 3 4) (3 4 :a) (4 :a :b))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I would expect the &quot;last partition&quot; to be defined as:&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;The last partition to contain elements from the input collection (not the padding collection) which are not present in any previous partition.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;This is consistent with option 1 above.&lt;/p&gt;
</description>
<category>Sequences</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/9937/suspected-bug-in-clojure-core-partition</guid>
<pubDate>Thu, 10 Dec 2020 02:54:45 +0000</pubDate>
</item>
</channel>
</rss>