<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Clojure Q&amp;A - Recent questions in Collections</title>
<link>https://ask.clojure.org/index.php/questions/clojure/collections</link>
<description></description>
<item>
<title>select-keys on nil map</title>
<link>https://ask.clojure.org/index.php/14654/select-keys-on-nil-map</link>
<description>&lt;p&gt;I know that &lt;a rel=&quot;nofollow&quot; href=&quot;https://ask.clojure.org/index.php/1913/use-transients-with-select-keys-if-possible&quot;&gt;https://ask.clojure.org/index.php/1913/use-transients-with-select-keys-if-possible&lt;/a&gt; exists to optimize &lt;code&gt;select-keys&lt;/code&gt; for many keys, but I noticed that it does a lot of work even if the original map is &lt;code&gt;nil&lt;/code&gt;. Would there be interest in a patch that returns an empty map when given &lt;code&gt;nil&lt;/code&gt;? Something like:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(defn select-keys*
  &quot;Returns a map containing only those entries in map whose key is in keys&quot;
  {:added &quot;1.0&quot;
   :static true}
  [map keyseq]
  (if map
    (loop [ret {} keys (seq keyseq)]
      (if keys
        (let [entry (. clojure.lang.RT (find map (first keys)))]
          (recur
           (if entry
             (conj ret entry)
             ret)
           (next keys)))
        (with-meta ret (meta map))))
    {}))
&lt;/code&gt;&lt;/pre&gt;
</description>
<category>Collections</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/14654/select-keys-on-nil-map</guid>
<pubDate>Fri, 01 Aug 2025 14:42: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>partition-all with step yields surprising result</title>
<link>https://ask.clojure.org/index.php/14273/partition-all-with-step-yields-surprising-result</link>
<description>&lt;p&gt;I was expecting &lt;code&gt;(partition-all n step coll)&lt;/code&gt; to be equal to &lt;code&gt;(partition n step nil coll)&lt;/code&gt; but that's not always the case.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(partition-all 3 1 (range 5))
;=&amp;gt; ((0 1 2) (1 2 3) (2 3 4) (3 4) (4))
;; Notice the second to last partition is (3 4).  Why not stop there?

;; My work-around is to use a nil pad.  This gives my expected result
(partition 3 1 nil  (range 5))
;=&amp;gt; ((0 1 2) (1 2 3) (2 3 4) (3 4))
&lt;/code&gt;&lt;/pre&gt;
</description>
<category>Collections</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/14273/partition-all-with-step-yields-surprising-result</guid>
<pubDate>Tue, 26 Nov 2024 21:01:19 +0000</pubDate>
</item>
<item>
<title>Gather all keys and values of nested map that satisfies some condition</title>
<link>https://ask.clojure.org/index.php/14231/gather-all-keys-values-nested-that-satisfies-some-condition</link>
<description>&lt;p&gt;Hi,&lt;br&gt;
From a nested map, I'd like to build a map that saves all key - value pairs whose keys satisfy some conditions.&lt;br&gt;
I'm using babashka.&lt;br&gt;
Tried some different solutions, but I'm not able to retrieve all pairs.&lt;/p&gt;
&lt;p&gt;The first one, using (I think) a more functional approach &lt;code&gt;j-h5&lt;/code&gt;.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;    #!/usr/bin/env bb
    
    ; expected output: {&quot;k23&quot; {&quot;k231&quot; &quot;v231&quot;} &quot;k24&quot; &quot;v24&quot;}
 (def j {&quot;k1&quot; &quot;v1&quot; &quot;k2&quot; {&quot;k21&quot; &quot;v21&quot; &quot;k22&quot; &quot;v22&quot; &quot;k23&quot; {&quot;k231&quot; &quot;v231&quot;} &quot;k24&quot; &quot;v24&quot;} &quot;k3&quot; &quot;v3&quot;})
(println j)

(println &quot;Doing j-h5&quot;)
(defn j-h5 [s mymap]
  (let [first-k (first (first s))
        first-v (second (first s)) ]
    (cond 
      (empty? s) nil
      (or (= &quot;k23&quot; first-k) (= &quot;k24&quot; first-k))
      (do 
        (assoc mymap first-k first-v)
        (when (and ( map? first-v) (seq first-v))
            (println  (str &quot;doing recursion1 when k:&quot; first-k &quot;  and v:&quot; first-v))
            (recur first-v mymap))
        )
      (map? first-v)
            (do 
              (println  (str &quot;doing recursion2 when k:&quot; first-k &quot;  and v:&quot; first-v))
              (recur first-v mymap))
      :else (recur (rest s) mymap) 
      )))

(j-h5 j {})
&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;; mymap defined outside of function   
(println &quot;Doing j-h1&quot;)
(def  mymap {})
(defn j-h1 [s]
  (let [first-k (first (first s))
        first-v (second (first s)) ]
    (cond 
      (empty? s) mymap
      (or (= &quot;k23&quot; first-k) (= &quot;k24&quot; first-k))
      (do 
        (assoc mymap first-k first-v)
        (when (and ( map? first-v) (seq first-v))
            (println  (str &quot;doing recursion1 when k:&quot; first-k &quot;  and v:&quot; first-v))
            (recur first-v))
        )
      (map? first-v)
            (do 
              (println  (str &quot;doing recursion2 when k:&quot; first-k &quot;  and v:&quot; first-v))
              (recur first-v))
      :else (recur (rest s)) 
      )))

(j-h1 j)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;How can this be fixed?&lt;/p&gt;
</description>
<category>Collections</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/14231/gather-all-keys-values-nested-that-satisfies-some-condition</guid>
<pubDate>Thu, 07 Nov 2024 21:38:20 +0000</pubDate>
</item>
<item>
<title>Apply two functions to a collection and to another that is a transformation of the other one</title>
<link>https://ask.clojure.org/index.php/14206/apply-functions-collection-another-transformation-other</link>
<description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;The challenge is to get the server name part of a collection of strings of fully qualified domain names.&lt;br&gt;
From: &lt;code&gt;[&quot;server1.oo.com&quot; &quot;s2.ee.com&quot;]&lt;/code&gt;&lt;br&gt;
To: &lt;code&gt;[&quot;server1&quot; &quot;s2&quot;] &lt;/code&gt;&lt;/p&gt;
&lt;p&gt;I can do this to a single element as:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(subs &quot;server1.oo.com&quot; 0 (str/index-of &quot;server1.oo.com&quot; \.))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;or &lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(subs &quot;server1.oo.com&quot; 0 (#(str/index-of % \.) &quot;server1.oo.com&quot;))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;But nested &lt;code&gt;#()&lt;/code&gt; functions seem not to be possible.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(#(subs % 0 (#(str/index-of % \.) &quot;server1.oo.com&quot;)) &quot;server1.oo.com&quot;)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;returns an error.&lt;/p&gt;
&lt;p&gt;When using the collection, something like this works:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(let 
  [s-col [&quot;server1.oo.com&quot; &quot;s2.ee.com&quot;]]   
  (map #(subs % 0 5) s-col))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;But, how can I use the &lt;code&gt;(str/index-of % \.)&lt;/code&gt; instead of that hard-coded &lt;code&gt;5&lt;/code&gt;?&lt;br&gt;
All my attempts were unsuccessful.&lt;/p&gt;
</description>
<category>Collections</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/14206/apply-functions-collection-another-transformation-other</guid>
<pubDate>Tue, 22 Oct 2024 22:13:46 +0000</pubDate>
</item>
<item>
<title>How to persist Clojure data structures to disk?</title>
<link>https://ask.clojure.org/index.php/14203/how-to-persist-clojure-data-structures-to-disk</link>
<description>&lt;p&gt;Hello,&lt;br&gt;
I'm building a web crawler in Clojure that records its results in a map of maps. The data structure basically looks like this:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;{&quot;https://example.org&quot; {:base-url &quot;https://example.org&quot;
                        :crawled {&quot;/blog&quot; {:relevant true :status 200}
                                  &quot;/contact&quot; {:relevant false :status 200}}
 &quot;https://foo.com&quot; {:base-url &quot;https://foo.com&quot;
                    :crawled {&quot;/home&quot; {:relevant true :status 200}}}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The web crawler is feed with a list of websites. However, for the sake of efficiency I want to avoid crawling already crawled websites. Therefore, I was looking for a way to persist the above data structure so that it can be consulted in subsequent runs to check if a given website was already crawled. (Of course, I have other reasons to store the results too, e.g. to do various analysis on the captured data)&lt;/p&gt;
&lt;p&gt;First, I thought &lt;strong&gt;EDN&lt;/strong&gt; would be a perfect solution. To my surprise, &lt;code&gt;clojure.edn&lt;/code&gt; does not have &lt;code&gt;write&lt;/code&gt; functions. A lot of advice online says to use &lt;code&gt;pr-str&lt;/code&gt;. However, using &lt;code&gt;pr-str&lt;/code&gt; has caveats as pointed out in &lt;a rel=&quot;nofollow&quot; href=&quot;https://nitor.com/en/articles/pitfalls-and-bumps-clojures-extensible-data-notation-edn&quot;&gt;https://nitor.com/en/articles/pitfalls-and-bumps-clojures-extensible-data-notation-edn&lt;/a&gt; (e.g. possbile truncations when &lt;em&gt;print-length&lt;/em&gt; is set). And it's totally possible to generate invalid edn as pointed out in this comment by @alexmiller: &lt;a rel=&quot;nofollow&quot; href=&quot;https://ask.clojure.org/index.php/10278/should-pr-str-on-a-function-produce-valid-edn-syntax?show=10282#c10282&quot;&gt;https://ask.clojure.org/index.php/10278/should-pr-str-on-a-function-produce-valid-edn-syntax?show=10282#c10282&lt;/a&gt;. &lt;/p&gt;
&lt;p&gt;Despite &lt;a rel=&quot;nofollow&quot; href=&quot;https://clojuredocs.org/clojure.edn&quot;&gt;https://clojuredocs.org/clojure.edn&lt;/a&gt; claiming that it is &quot;designed to be used in a similar way to JSON or XML&quot; I have the impression it is more geared towards writing edn files by hand (i.e. configuration files).&lt;/p&gt;
&lt;p&gt;Looking for alternatives, I stumbled upon Transit. However, its mainly intended &quot;as a wire protocol for transferring data between applications&quot;. And the README further says&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;If storing Transit data durably, readers and writers are expected to use the same version of Transit and you are responsible for migrating/transforming/re-storing that data when and if the transit format changes.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Because in Clojure, data is code and code is data and because of the existence of EDN, I really thought persisting simple data structures like the above would be a breeze in Clojure. &lt;/p&gt;
&lt;p&gt;Instead I see these sub-optimal options with my current knowledge:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Hope that &lt;code&gt;pr-str&lt;/code&gt; produces valid EDN without loss in my particular case&lt;/li&gt;
&lt;li&gt;Build a custom XML serialization&lt;/li&gt;
&lt;li&gt;Use Transit&lt;/li&gt;
&lt;li&gt;Use JSON?! (what about keywords etc.?!)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Is there a more idiomatic way to store and read back in simple Clojure data structures?&lt;/p&gt;
</description>
<category>Collections</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/14203/how-to-persist-clojure-data-structures-to-disk</guid>
<pubDate>Tue, 22 Oct 2024 08:45:52 +0000</pubDate>
</item>
<item>
<title>Optimize `select-keys`</title>
<link>https://ask.clojure.org/index.php/13964/optimize-select-keys</link>
<description>&lt;p&gt;Some preliminary testing show that &lt;code&gt;select-keys&lt;/code&gt; can be almost twice as fast if it were rewritten to use a transient map rather than a proper map as it does today.&lt;/p&gt;
&lt;p&gt;In order to do so, &lt;code&gt;select-keys&lt;/code&gt; would need to move to after where &lt;code&gt;transient&lt;/code&gt; and its friends are defined. &lt;/p&gt;
&lt;p&gt;If there is any interest in pursuing this, I'd be happy to provide a problem statement, more data on the performance,  a couple of possible solutions, and perhaps ultimately a patch.&lt;/p&gt;
</description>
<category>Collections</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/13964/optimize-select-keys</guid>
<pubDate>Tue, 11 Jun 2024 07:18:28 +0000</pubDate>
</item>
<item>
<title>Cheshire parse-string coerce keys to keywords</title>
<link>https://ask.clojure.org/index.php/13777/cheshire-parse-string-coerce-keys-to-keywords</link>
<description>&lt;p&gt;Just learning Clojure. I have the following json string:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(def json &quot;{\&quot;a\&quot;: 1, \&quot;b\&quot;: \&quot;val\&quot;, \&quot;c\&quot;: {\&quot;D/E\&quot;: [1, 2, 3]}}}&quot;)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If the key-fn param is true in the Cheshire parse-string call I got the following map: &lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(def parsed-json (cheshire/parse-string json true))
;; ==&amp;gt; {:a 1, :b &quot;val&quot;, :c #:D{:E [1 2 3]}}
(type parsed-json)
;; =&amp;gt; clojure.lang.PersistentArrayMap
(type (:c parsed-json))
;; =&amp;gt; clojure.lang.PersistentArrayMap
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;How can I access the [1 2 3] array?  I don't understand what is #:D here, and how I can access :E.&lt;/p&gt;
</description>
<category>Collections</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/13777/cheshire-parse-string-coerce-keys-to-keywords</guid>
<pubDate>Wed, 06 Mar 2024 18:06:15 +0000</pubDate>
</item>
<item>
<title>CLJ-2713 (IDrop) causes exception in previously working code</title>
<link>https://ask.clojure.org/index.php/13677/clj-2713-idrop-causes-exception-in-previously-working-code</link>
<description>&lt;p&gt;In Clojure 1.11.1:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;user=&amp;gt; (split-at Long/MAX_VALUE [1 2 3 4 5 6])
[(1 2 3 4 5 6) ()]
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In Clojure 1.12 alpha7:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;user=&amp;gt; (split-at Long/MAX_VALUE [1 2 3 4 5 6])
Execution error (ArithmeticException) at java.lang.Math/toIntExact (Math.java:1371).
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Possibly IDrop should take a &lt;code&gt;long&lt;/code&gt; parameter (instead of &lt;code&gt;int&lt;/code&gt;) for compatibility?&lt;/p&gt;
</description>
<category>Collections</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/13677/clj-2713-idrop-causes-exception-in-previously-working-code</guid>
<pubDate>Fri, 09 Feb 2024 17:51:12 +0000</pubDate>
</item>
<item>
<title>Why doesn't clojure.set provide a &quot;symmetric difference&quot; function?</title>
<link>https://ask.clojure.org/index.php/13324/why-doesnt-clojure-provide-symmetric-difference-function</link>
<description>&lt;p&gt;Is there a particular reason why clojure.set does not provide a &lt;code&gt;symmetric-difference&lt;/code&gt; function?&lt;/p&gt;
&lt;p&gt;It seems a relatively frequent thing to want.&lt;/p&gt;
</description>
<category>Collections</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/13324/why-doesnt-clojure-provide-symmetric-difference-function</guid>
<pubDate>Mon, 25 Sep 2023 20:44:24 +0000</pubDate>
</item>
<item>
<title>Use transducer arity of `into` in `clojure.walk` namespace</title>
<link>https://ask.clojure.org/index.php/13281/use-transducer-arity-of-into-in-clojure-walk-namespace</link>
<description>&lt;p&gt;It's simple. Just like that:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;gt; clojure.walk/walk:
(outer (reduce (fn [r x] (conj r (inner x))) form form))
=&amp;gt; (outer (into form (map inner) form))
(outer (into (empty form) (map inner form)))
=&amp;gt; (outer (into (empty form) (map inner) form))
&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;&amp;gt; clojure.walk/keywordize-keys:
(postwalk (fn [x] (if (map? x) (into {} (map f x)) x)) m)
=&amp;gt; (postwalk (fn [x] (if (map? x) (into {} (map f) x) x)) m)
&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;&amp;gt; clojure.walk/strigify-keys:
(postwalk (fn [x] (if (map? x) (into {} (map f x)) x)) m)
=&amp;gt; (postwalk (fn [x] (if (map? x) (into {} (map f) x) x)) m)
&lt;/code&gt;&lt;/pre&gt;
</description>
<category>Collections</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/13281/use-transducer-arity-of-into-in-clojure-walk-namespace</guid>
<pubDate>Wed, 13 Sep 2023 22:49:05 +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>Efficient merge</title>
<link>https://ask.clojure.org/index.php/13206/efficient-merge</link>
<description>&lt;p&gt;I was wondering why the &lt;code&gt;merge&lt;/code&gt; function doesn't use transients and if there could be a better version of that.&lt;/p&gt;
&lt;p&gt;Instead of this:&lt;br&gt;
&lt;code&gt;(defn merge [&amp;amp; maps] (when (some identity maps) (reduce #(conj (or %1 {}) %2) maps)))&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;we can use transducers to keep the code still easy to read and have better performance:&lt;br&gt;
&lt;code&gt;(defn merge [&amp;amp; maps] (when (some identity maps) (into {} cat maps)))&lt;/code&gt;&lt;/p&gt;
</description>
<category>Collections</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/13206/efficient-merge</guid>
<pubDate>Sat, 26 Aug 2023 18:28:54 +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>merging pairs of pairs?</title>
<link>https://ask.clojure.org/index.php/12856/merging-pairs-of-pairs</link>
<description>&lt;p&gt;i want to create a list of pairs, and can build the list best as a series of pairs of pairs: &lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(for [ni (range n)
          di (range d)]
     (let [j1 (mod (inc di) n)
              j2 (mod (- n (inc di)) n) ]
          (conj [] [ni j1] [ni j2]) ))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;this gets me close:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;([[0 1] [0 9]] [[0 2] [0 8]] [[1 1] [1 9]]...
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;but is adding an extra level of brackets.  what i want would look like this:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;([0 1] [0 9] [0 2] [0 8] [1 1] [1 9] ...
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I am confident there is a simple solution for this but I can't figure out what it!?&lt;/p&gt;
</description>
<category>Collections</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/12856/merging-pairs-of-pairs</guid>
<pubDate>Fri, 14 Apr 2023 20:29:10 +0000</pubDate>
</item>
<item>
<title>A faster merge?</title>
<link>https://ask.clojure.org/index.php/12780/a-faster-merge</link>
<description>&lt;p&gt;&lt;code&gt;merge&lt;/code&gt; is implemented as &lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(defn merge
 ;; stuff removed
  [&amp;amp; maps]
  (when (some identity maps)
    (reduce1 #(conj (or %1 {}) %2) maps)))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This is somewhat suboptimal from a couple of reasons. &lt;code&gt;reduce1&lt;/code&gt; is slower than &lt;code&gt;reduce&lt;/code&gt; (might not matter when the number of maps is small, but perhaps more importantly, in the case where you just want to merge two maps, it does a lot of unnecessary work, since merging two maps is basically &lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(when (or m1 m2) (conj (or m1 {}) m2))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Would it be reasonable to have a multi arity version of &lt;code&gt;merge&lt;/code&gt; which optimized for this case?&lt;br&gt;
A quick benchmark shows a potential for a 4 times speed up in this case.&lt;/p&gt;
&lt;p&gt;I've looked through Jira, but surprisingly, I wasn't able to find a ticket for this? &lt;/p&gt;
</description>
<category>Collections</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/12780/a-faster-merge</guid>
<pubDate>Fri, 17 Mar 2023 11:35:44 +0000</pubDate>
</item>
<item>
<title>submap? - recursively check if one map is contained in another</title>
<link>https://ask.clojure.org/index.php/12749/submap-recursively-check-if-one-map-is-contained-in-another</link>
<description>&lt;p&gt;&lt;code&gt;(submap? m1 m2)&lt;/code&gt; recursively checks if m1 is contained in m2&lt;/p&gt;
&lt;p&gt;Usage:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(deftest submap
  (is (submap? {} {}))
  (is (not (submap? {:k 1} {})))
  (is (submap? {:k 1} {:k 1 :j 2}))
  (is (submap? {:k {:kk 1}} {:k {:kk 1 :j 2}}))
  (is (not (submap? {:k {:kk 1}} {:j 2}))))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This need often arises in tests, where you want to check if the map returned by function-under-test has a certain shape and while assuming an open world - i.e. you want to ignore any additional keys so that changing function-under-test doesn't break the tests:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;;; BAD: doesn't assume open world
(is (= {:k :v} (function-under-test)))

;; BAD: not expressive
(is (= :v (:k (function-under-test))))

;; BAD: not expressive, need to write :k twice
(is (= {:k :v} (select-keys (function-under-test) [:k])))

;; GOOD: expressive
(is (submap? {:k :v} (function-under-test)))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Implementations already exist in many codebases (400+ hits on Github code search). Examples:&lt;/p&gt;
&lt;p&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/clojure/tools.deps/blob/ecc80420c1b734b384f7a42df91684cdbc37ddc6/src/test/clojure/clojure/tools/deps/util.clj#LL12-L25C18&quot;&gt;https://github.com/clojure/tools.deps/blob/ecc80420c1b734b384f7a42df91684cdbc37ddc6/src/test/clojure/clojure/tools/deps/util.clj#LL12-L25C18&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/ptaoussanis/encore/blob/96547700c03a5e81d784bcc9253980d7355f2f2b/src/taoensso/encore.cljc#L2071&quot;&gt;https://github.com/ptaoussanis/encore/blob/96547700c03a5e81d784bcc9253980d7355f2f2b/src/taoensso/encore.cljc#L2071&lt;/a&gt;&lt;/p&gt;
</description>
<category>Collections</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/12749/submap-recursively-check-if-one-map-is-contained-in-another</guid>
<pubDate>Thu, 09 Mar 2023 19:24:33 +0000</pubDate>
</item>
<item>
<title>Function to create a concrete list</title>
<link>https://ask.clojure.org/index.php/12669/function-to-create-a-concrete-list</link>
<description>&lt;p&gt;The core collection data structures (lists, vectors, maps, and sets) have variadic functions to create them: &lt;code&gt;list&lt;/code&gt;, &lt;code&gt;vector&lt;/code&gt;, &lt;code&gt;hash-map&lt;/code&gt;, &lt;code&gt;hash-set&lt;/code&gt;. 2 of them have 1-arity functions that take in a collection and convert them to the desired concrete collection type: &lt;code&gt;vec&lt;/code&gt; and &lt;code&gt;set&lt;/code&gt;. Maps and lists don't have such a function. I think I understand why maps don't: Hard/impossible to convert between it and other collection types when it requires pairs without a middle step to perform the pairing.&lt;/p&gt;
&lt;p&gt;But it seems that there's a missing function that takes in a single object of any collection type and converts it to a concrete PersistentList. Something as simple as &lt;code&gt;(defn to-list [coll] (clojure.lang.PersistentList/create coll))&lt;/code&gt;. Is there a specific reason for its exclusion?&lt;/p&gt;
&lt;p&gt;My reasons for asking/desiring such a function are because I am building and then traversing nested data structures, and it can be challenging to correctly handle branches where the output of the various Clojure core functions that operate on &quot;lists&quot; end up returning non-PersistentLists (such as &lt;code&gt;list*&lt;/code&gt; and &lt;code&gt;cons&lt;/code&gt; and syntax quote). I wrote my own &lt;code&gt;to-list&lt;/code&gt; and have been peppering it where necessary to make my decision trees easier after realizing that &lt;code&gt;list*&lt;/code&gt; didn't fit the bill.&lt;/p&gt;
&lt;p&gt;I know that the sequence abstraction is a big part of the mindset of Clojure, but &lt;code&gt;list?&lt;/code&gt; not returning true on things I wrote that I know for sure have a bounded size has led to some frustration.&lt;/p&gt;
&lt;p&gt;I'm primarily interested in hearing about any reasoning for the deliberate exclusion of such a function but alongside that request, if the core team is interested, I can provide a patch for its addition.&lt;/p&gt;
</description>
<category>Collections</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/12669/function-to-create-a-concrete-list</guid>
<pubDate>Wed, 15 Feb 2023 19:10:11 +0000</pubDate>
</item>
<item>
<title>Addition/Multiplication operations on loop</title>
<link>https://ask.clojure.org/index.php/12466/addition-multiplication-operations-on-loop</link>
<description>&lt;p&gt;Hi! &lt;br&gt;
It's me again. I followed up on the previous answers that have been posted on my questions to do another operation but I don't get exactly what I want. &lt;br&gt;
Here's the situation:&lt;/p&gt;
&lt;p&gt;My first table (products) looks like : (product_name price)&lt;br&gt;
([1 (candies 6.5)]
[2 (sweets 1.75)]&lt;br&gt;
[3 (jam 2.99)]
[4 (gum 1.25)])&lt;/p&gt;
&lt;p&gt;My first table (products) looks like : (customer_name product_name quantity)&lt;br&gt;
([1 (Sara candies 3)]
[2 (Joe jam 3)]&lt;br&gt;
[3 (Sara gum 1)])&lt;/p&gt;
&lt;p&gt;What I'm trying to do is if i type for example Sara, I'll get the sum of the sales made by Sara, which means: (3&lt;em&gt;6.5 + 1&lt;/em&gt;1.25) = $20.75 (in this case)&lt;/p&gt;
&lt;p&gt;I'm fine for the input part (I get the name of the customer as an input from the terminal)&lt;br&gt;
However, my code:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(defn sales_prices_with_cond [name_cus] (map
 (fn [y z]
   (if (= (str name_cus) (str (nth (first z) 1)))
   list (* (Double. (nth (second y) 1)) (Integer/parseInt (nth (second z) 2))))
   )
products 
sales))
(println (reduce + sales_prices_with_cond &quot;Sara&quot;))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Gives me the sum of ALL the sales*quantities. It's like the condition is skipped or maybe not well written ...&lt;br&gt;
I also tried with (some) and got the same result...&lt;/p&gt;
&lt;p&gt;Please Help :') .&lt;/p&gt;
</description>
<category>Collections</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/12466/addition-multiplication-operations-on-loop</guid>
<pubDate>Sat, 10 Dec 2022 05:55:32 +0000</pubDate>
</item>
<item>
<title>Help with looping/filtering elements</title>
<link>https://ask.clojure.org/index.php/12460/help-with-looping-filtering-elements</link>
<description>&lt;p&gt;Hi!&lt;br&gt;
I'm a newbie in Clojure and I'm struggling with looping through data in data structures.&lt;/p&gt;
&lt;p&gt;I have a list that looks like this : (list1)&lt;br&gt;
([1 (item1 item2 item3)]&lt;br&gt;
 [2 (item21 item22 item33)])&lt;/p&gt;
&lt;p&gt;And another one that looks like this: (list2)&lt;br&gt;
([1 (rec1 rec2 rec3)]&lt;br&gt;
 [2 (rec21 rec22 rec33)])&lt;/p&gt;
&lt;p&gt;And the last one is: (list3)&lt;br&gt;
([1 (1 1 3)]&lt;br&gt;
 [2 (2 2 3)])&lt;/p&gt;
&lt;p&gt;Now, I have to loop into list3 and create a new collection that replaces the 1 (in my list3) by &quot;item1&quot; and replaces the second 1 (in my list3 too) by &quot;rec1&quot; ... and so on for all the vectors in list3...&lt;/p&gt;
&lt;p&gt;The three lists have the exact same number of records. &lt;/p&gt;
&lt;p&gt;My third list should look like :&lt;br&gt;
([1 (item1 rec1 3)]&lt;br&gt;
 [2 (item21 rec22 3]) &lt;/p&gt;
&lt;p&gt;Any idea how to proceed? &lt;/p&gt;
</description>
<category>Collections</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/12460/help-with-looping-filtering-elements</guid>
<pubDate>Fri, 09 Dec 2022 20:19:28 +0000</pubDate>
</item>
<item>
<title>Can group-by be generalized?</title>
<link>https://ask.clojure.org/index.php/12319/can-group-by-be-generalized</link>
<description>&lt;p&gt;It would be nice if &lt;code&gt;group-by&lt;/code&gt; let users control the aggregated collection type and manipulate values before aggregation. It's a common scenarios when grouping a collection of maps according to one key, and maybe aggregating, even numerically according to another.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;group-by&lt;/code&gt; could be generalized like so:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(defn group-by
  &quot;Returns a map of the elements of coll keyed by the result of
  f on each element. The value at each key will be a vector of the
  corresponding elements, in the order they appeared in coll.&quot;
  {:added &quot;1.2&quot;
   :static true}
  ([kf coll]
   (group-by kf [] coll))
  ([kf init coll]
   (group-by kf identity init coll))
  ([kf vf init coll]
   (group-by kf vf conj init coll))
  ([kf vf rf init coll]
   (persistent!
    (reduce
     (fn [ret x]
       (let [k (kf x)]
         (assoc! ret k (rf (get ret k init) (vf x)))))
     (transient {}) coll))))
&lt;/code&gt;&lt;/pre&gt;
</description>
<category>Collections</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/12319/can-group-by-be-generalized</guid>
<pubDate>Tue, 18 Oct 2022 15:37:22 +0000</pubDate>
</item>
<item>
<title>Why does `(str (map identity [1 2]))` return `clojure.lang.LazySeq@`? Is this a bug?</title>
<link>https://ask.clojure.org/index.php/12310/why-does-str-map-identity-return-clojure-lang-lazyseq-this</link>
<description>&lt;p&gt;Why does &lt;code&gt;(str (map identity [1 2]))&lt;/code&gt; return &lt;code&gt;clojure.lang.LazySeq@3e2&lt;/code&gt;? &lt;br&gt;
Is this a bug? &lt;br&gt;
For example, &lt;code&gt;(str (seq (map identity [1 2])))&lt;/code&gt; returns &lt;code&gt;(1 2)&lt;/code&gt; while &lt;code&gt;(str (doall (map identity [1 2])))&lt;/code&gt; still returns  &lt;code&gt;clojure.lang.LazySeq@3e2&lt;/code&gt;.&lt;/p&gt;
</description>
<category>Collections</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/12310/why-does-str-map-identity-return-clojure-lang-lazyseq-this</guid>
<pubDate>Wed, 12 Oct 2022 19:21:59 +0000</pubDate>
</item>
<item>
<title>Does it make sense to define filter-keys/filter-vals functions for maps in clojure.core?</title>
<link>https://ask.clojure.org/index.php/12291/does-make-sense-define-filter-keys-filter-functions-clojure</link>
<description>&lt;p&gt;Hi!&lt;/p&gt;
&lt;p&gt;Does it make sense to define filter-keys/filter-vals functions for map which takes a map and a predicate and returns a map satisfied predicate?&lt;/p&gt;
&lt;p&gt;For example, there is an utility lib called medley and it defines such functions for maps as &lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/weavejester/medley/blob/master/src/medley/core.cljc#L142&quot;&gt;filter-keys&lt;/a&gt;, &lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/weavejester/medley/blob/master/src/medley/core.cljc#L148&quot;&gt;filter-vals&lt;/a&gt; and some others.&lt;/p&gt;
&lt;p&gt;It seems that there is a reason for not having it in the core lib.&lt;br&gt;
Using core lib i can use &lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/clojure/clojure/blob/master/src/clj/clojure/core.clj#L1555&quot;&gt;select-keys&lt;/a&gt; to query a map with a vector of keys and get a map with requested keys if map has it but there is no more advanced query functions for map except powerful reduce-kv which mixes condition + new map building.&lt;/p&gt;
&lt;p&gt;Clojure set lib defines a &lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/clojure/clojure/blob/master/src/clj/clojure/set.clj#L65&quot;&gt;set/select&lt;/a&gt; function allowing to apply a predicate over a set and gets a set as a result.&lt;/p&gt;
&lt;p&gt;Any ideas why there is no similar functions for maps?&lt;/p&gt;
</description>
<category>Collections</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/12291/does-make-sense-define-filter-keys-filter-functions-clojure</guid>
<pubDate>Sat, 08 Oct 2022 13:40:25 +0000</pubDate>
</item>
<item>
<title>How read list of files, read all data from and compare with another list of keys?</title>
<link>https://ask.clojure.org/index.php/12276/read-list-files-read-data-from-compare-with-another-list-keys</link>
<description>&lt;p&gt;How to read a list of files, compare the text in the files with a set of keys, and return the key with the value true or false if that key is found in the file?&lt;/p&gt;
&lt;p&gt;For example I have list of my files:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;[&quot;src/cljs/project/cofx.cljs&quot;
 &quot;src/cljs/project/datetime.cljs&quot;
 &quot;src/cljs/project/core.cljs&quot;
 &quot;src/cljs/project/app_db.cljs&quot;]
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;and set of keys:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;#{:tooltip/terminate
  :member-services/description
  :event-errors/link-duplicated
  :firm-admin/pre-approved-applicants}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I want read all files (I used &lt;code&gt;slurp&lt;/code&gt;), compare text from all files with first key and return this key and true and name of file if key exist in this file, or false if key not exist in all files.&lt;/p&gt;
&lt;p&gt;for example:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;[:tooltip/termiante true &quot;src/cljs/project/cofx.cljs&quot;
 :member-services/description false]
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Do you have any tips? Because this task very difficult for me.&lt;/p&gt;
</description>
<category>Collections</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/12276/read-list-files-read-data-from-compare-with-another-list-keys</guid>
<pubDate>Mon, 03 Oct 2022 12:26:25 +0000</pubDate>
</item>
<item>
<title>`update-keys` and `update-vals` missing the `&amp; args` from `update`</title>
<link>https://ask.clojure.org/index.php/12107/update-keys-and-update-vals-missing-the-args-from-update</link>
<description>&lt;p&gt;The &lt;code&gt;&amp;amp; args&lt;/code&gt; in &lt;code&gt;update&lt;/code&gt;, &lt;code&gt;swap!&lt;/code&gt; and etc are very ergonomic and compose really well, so I was surprised to learn that &lt;code&gt;update-keys&lt;/code&gt; and &lt;code&gt;update-vals&lt;/code&gt; don't have that, they're only &lt;code&gt;[m f]&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;What is the reason for this?&lt;/p&gt;
&lt;p&gt;I think adding &lt;code&gt;&amp;amp; args&lt;/code&gt; to these functions would be more consistent with the existing core functions and help avoid anon functions where not needed.&lt;/p&gt;
</description>
<category>Collections</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/12107/update-keys-and-update-vals-missing-the-args-from-update</guid>
<pubDate>Tue, 09 Aug 2022 15:03:31 +0000</pubDate>
</item>
<item>
<title>SortedMap not IEditableCollection</title>
<link>https://ask.clojure.org/index.php/12093/sortedmap-not-ieditablecollection</link>
<description>&lt;p&gt;It seems to me that the reason &lt;a rel=&quot;nofollow&quot; href=&quot;https://ask.clojure.org/index.php/12073/update-vals-not-preserving-sorted-maps&quot;&gt;https://ask.clojure.org/index.php/12073/update-vals-not-preserving-sorted-maps&lt;/a&gt; is a question at all, is that &lt;code&gt;PersistentTreeMap&lt;/code&gt; doesn't implement &lt;code&gt;IEditableCollection&lt;/code&gt;. &lt;/p&gt;
&lt;p&gt;So to satisfy my own curiosity, why is this? &lt;/p&gt;
</description>
<category>Collections</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/12093/sortedmap-not-ieditablecollection</guid>
<pubDate>Mon, 01 Aug 2022 06:46:53 +0000</pubDate>
</item>
<item>
<title>`update-vals` not preserving sorted maps?</title>
<link>https://ask.clojure.org/index.php/12073/update-vals-not-preserving-sorted-maps</link>
<description>&lt;p&gt;Hello! We recently found that when applying &lt;code&gt;update-vals&lt;/code&gt; to a sorted map, it's converted back into an unsorted array/hash map due to the collection not being transientable. Example given below. As a note, this differs from &lt;code&gt;medley/map-vals&lt;/code&gt; which preserves sorting and which we were using for this functionality previously to 1.11. &lt;/p&gt;
&lt;p&gt;Is this intended behavior? If so, I'd be curious about the intent behind it :) Thanks!&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;user&amp;gt; (def m (sorted-map :a 0 :b 0))
;; =&amp;gt; #'user/m
user&amp;gt; (type m)
;; =&amp;gt; clojure.lang.PersistentTreeMap
user&amp;gt; (type (update-vals m inc))
;; =&amp;gt; clojure.lang.PersistentArrayMap
user&amp;gt; (type (medley.core/map-vals inc m))
;; =&amp;gt; clojure.lang.PersistentTreeMap
&lt;/code&gt;&lt;/pre&gt;
</description>
<category>Collections</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/12073/update-vals-not-preserving-sorted-maps</guid>
<pubDate>Wed, 27 Jul 2022 08:35:05 +0000</pubDate>
</item>
<item>
<title>Cast to Map$Entry Exception When Trying to (Into {}) After Sequence Operations</title>
<link>https://ask.clojure.org/index.php/11879/cast-mapentry-exception-trying-after-sequence-operations</link>
<description>&lt;p&gt;Is it intended behavior to get this exception  :&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;class clojure.lang.Keyword cannot be cast to class java.util.Map$Entry
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;on this piece of code :&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(def a [[&quot;Active&quot; :Transit-RLC &quot;Active&quot;]])

(-&amp;gt;&amp;gt; a
     (transduce
       (comp 
             (map drop-last)
             (map reverse))
       conj [])
     (into {}))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;changing the transducer to &lt;code&gt;(map (-&amp;gt; [(second %) (first %)])&lt;/code&gt; solves the issue.&lt;br&gt;
Thanks&lt;/p&gt;
</description>
<category>Collections</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/11879/cast-mapentry-exception-trying-after-sequence-operations</guid>
<pubDate>Sun, 08 May 2022 09:17:12 +0000</pubDate>
</item>
<item>
<title>Should clojure.lang.Range/LongRange be made to implement clojure.lang.Reversible?</title>
<link>https://ask.clojure.org/index.php/11850/should-clojure-range-longrange-implement-clojure-reversible</link>
<description>&lt;p&gt;Sometimes I want to iterate through a range like &lt;code&gt;(range 100)&lt;/code&gt;, but in reverse order. For this, there are two existing choices:&lt;/p&gt;
&lt;p&gt;1) The programmer thinks about the would be the right start/end/step to construct the reversed range, and constructs the range accordingly, with a negative step:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(range 99 -1 -1)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;2) Use &lt;code&gt;reverse&lt;/code&gt; as in &lt;code&gt;(reverse (range 100))&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Solution 1 is not great since well it is harder to think about because you have to change the start/end because of the reversed bounds inclusive/exclusive bounds on the start and end. It would also require more mental math than I like to do, in the case where &lt;code&gt;step&lt;/code&gt; is not 1. For instance, quickly solve how to reverse &lt;code&gt;(range 23 96 7)&lt;/code&gt;! The answer is &lt;code&gt;(range 93 22 -7)&lt;/code&gt;, but it's not ideal to have to do this math in your head and to commit it to code when really what you meant was &lt;code&gt;(reverse (range 23 96 7))&lt;/code&gt;. For instance, if you tomorrow want to change the lower bound from 23 inclusive to 24 inclusive, you have to actually recompute the upper bound and change the code to &lt;code&gt;(range 94 23 -7)&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;This would get even more difficult/impossible for the programmer if they're using non-integer ranges:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(range 1.5 5.4 1.23)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Solution 2 is not great since it takes O(n) space and time:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(defn reverse
  &quot;Returns a seq of the items in coll in reverse order. Not lazy.&quot;
  {:added &quot;1.0&quot;
   :static true}
  [coll]
    (reduce1 conj () coll))
&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;So then what?&lt;/h2&gt;
&lt;h3&gt;Reversible&lt;/h3&gt;
&lt;p&gt;Maybe the &lt;code&gt;clojure.lang.Range&lt;/code&gt;/&lt;code&gt;clojure.lang.LongRange&lt;/code&gt; objects could implement &lt;code&gt;clojure.lang.Reversible&lt;/code&gt;, since they can be reversed in O(1) time and space, with something like:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;public ISeq rseq() {
    final Number difference = Numbers.minus(end, start);
    final Number remainder = Numbers.remainder(difference, step);
    final Number last = Numbers.isZero(remainder) ?
            Numbers.minus(end, step) : Numbers.minus(end, remainder);
    final Number reverseStep = Numbers.minus(step);
    BoundsCheck bc;
    Object newEnd;
    if (Numbers.isPos(reverseStep)) {
        newEnd = Numbers.inc(start);
        bc = positiveStep(newEnd);
    } else {
        newEnd = Numbers.dec(start);
        bc = negativeStep(newEnd);
    }
    return new Range(last, newEnd, reverseStep, bc);
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And then the reversing could be done in clojure using the &lt;code&gt;rseq&lt;/code&gt; function&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(rseq (range 10))
=&amp;gt; (9 8 7 6 5 4 3 2 1 0)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Problems with this approach and/or challenges:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Still doesn't work on &quot;empty ranges&quot; because when you do &lt;code&gt;(range 0 0)&lt;/code&gt; you actually get an empty list (not a Range object actually). So since lists aren't Reversible, you'd get an exception when you try to rseq it. This could be fixed by allowing there to be an empty Range object, or perhaps by making empty lists Reversible? Anyways probably too aggressive of a change. I actually don't think there should be a problem with allowing an empty Range, it's not clear to me if it's important that empty ranges are specifically lists, but for instance it isn't the case that the empty Vector is a list, or that the empty Set/Map are the empty list, so it doesn't seem to be bizarre that there would be an empty Range. &lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;some more thought would have to go into the algorithm to compute the reverse range in the case of floating point ranges, since the above algorithm doesn't produce the exact same result in reverse. For instance:&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;pre&gt;&lt;code&gt;(range 1.5 5.4 1.23)
=&amp;gt; (1.5 2.73 3.96 5.1899999999999995)
(rseq (range 1.5 5.4 1.23))
=&amp;gt; (5.1899999999999995 3.9599999999999995 2.7299999999999995 1.4999999999999996)
&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;If not that, then maybe &lt;code&gt;:reverse true&lt;/code&gt; keyword arg&lt;/h3&gt;
&lt;p&gt;Maybe the reversibility belongs more in the &lt;code&gt;range&lt;/code&gt; function:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(range 10 :reverse true)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;or similar.&lt;/p&gt;
&lt;p&gt;Anyways I am not saying Clojure's wrong or anything, just submitting some food for thought. Thanks for reading!&lt;/p&gt;
</description>
<category>Collections</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/11850/should-clojure-range-longrange-implement-clojure-reversible</guid>
<pubDate>Sun, 01 May 2022 00:13:06 +0000</pubDate>
</item>
<item>
<title>Is lazy sequence chunking deterministic?</title>
<link>https://ask.clojure.org/index.php/11687/is-lazy-sequence-chunking-deterministic</link>
<description>&lt;p&gt;I know that lazy sequences can be processed in a chunked manner.  That is, it may be that if my program needs &lt;em&gt;n&lt;/em&gt; elements from a lazy sequence, more than &lt;em&gt;n&lt;/em&gt; might be realized.&lt;/p&gt;
&lt;p&gt;Can I assume that if I run the same code a second time in the same version of Clojure, that exactly the same elements will get realized?  I would think so, but I don't want to assume that  without knowing.&lt;/p&gt;
&lt;p&gt;(I'm including additional info here in case anyone is interested in why this matters, but I don't think it's necessary to know my use case in order to answer the question.  So &lt;strong&gt;feel free to skip this paragraph&lt;/strong&gt;. I'm running simulations that make use of a pseudorandom number generator.  These involve random walks that are truncated if the walk encounters a &quot;target&quot;.  It's convenient to generate a walk in advance as a lazy sequence, and then stop realizing its elements when a target is found.  Normally, I throw away intermediate results such as the original random walk. They exist in a data structure at one time, but then I don't use them in the end.   However, I want to be able to go back and rerun a simulation, sometimes, in order to examine the intermediate results.  It's easy enough to store the seed that I used for a simulation run, and use it to set the random number generator to start in the same state.  However, chunking can cause the random number generator to be called more times than are strictly necessary.  That's OK, but if chunking can vary depending on something other than my code and the Clojure version, that could mean that rerunning a simulation with the same seed would not be guaranteed to produce the same results.  This is not at present a big deal.  I can make the intermediate data non-lazy without much of a performance hit.  However, I'm still wondering whether that's necessary.)&lt;/p&gt;
</description>
<category>Collections</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/11687/is-lazy-sequence-chunking-deterministic</guid>
<pubDate>Mon, 28 Mar 2022 16:11:27 +0000</pubDate>
</item>
<item>
<title>clojure.set different behaviour depending on the position of nil</title>
<link>https://ask.clojure.org/index.php/11614/clojure-set-different-behaviour-depending-the-position-nil</link>
<description>&lt;p&gt;If a nil reaches, e.g., &lt;code&gt;clojure.set/union&lt;/code&gt; or &lt;code&gt;clojure.set/intersection&lt;/code&gt;, the output depends on the position. I think those functions should give back always a hash-set, irrespective of &lt;code&gt;nil&lt;/code&gt;s or the &lt;em&gt;position&lt;/em&gt; of &lt;code&gt;nil&lt;/code&gt;.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;$ clj
Clojure 1.10.3
user=&amp;gt; (require '[clojure.set :refer [intersection union]])
nil
user=&amp;gt; (union #{} nil)
#{}
user=&amp;gt; (union nil #{})
nil
user=&amp;gt; (intersection #{} nil)
#{}
user=&amp;gt; (intersection nil #{})
nil
user=&amp;gt; (union #{} nil nil)
nil
user=&amp;gt; (union nil #{} nil)
nil
user=&amp;gt; (union nil nil #{})
#{}
user=&amp;gt; (intersection #{} nil nil)
nil
user=&amp;gt; (intersection nil #{} nil)
nil
user=&amp;gt; (intersection nil nil #{})
#{}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I think the order of the arguments shouldn't matter and should always be a set, in my opinion, just assuming &lt;code&gt;nil&lt;/code&gt; as an empty set. Plus if one intends to call later &lt;code&gt;(set-of-things thing)&lt;/code&gt; an exception might arise (this is what bit me).&lt;/p&gt;
&lt;p&gt;And maybe this is not limited to union and intersection only and more functions have this &quot;issue&quot;&lt;/p&gt;
</description>
<category>Collections</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/11614/clojure-set-different-behaviour-depending-the-position-nil</guid>
<pubDate>Sun, 06 Mar 2022 08:50:03 +0000</pubDate>
</item>
<item>
<title>Common patterns for optional fields interact poorly with spec</title>
<link>https://ask.clojure.org/index.php/11584/common-patterns-optional-fields-interact-poorly-with-spec</link>
<description>&lt;p&gt;I do lots of work with spec, and a frequent problem that I run into is in constructing a spec'ed object that is a map with optional fields.&lt;/p&gt;
&lt;p&gt;A &lt;a rel=&quot;nofollow&quot; href=&quot;https://grep.app/search?q=assoc%28.%2A%5Cn%29%7B0%2C5%7D.%2A%3A.%2A%5C%28®exp=true&amp;amp;filter[lang][0]=Clojure&quot;&gt;common pattern&lt;/a&gt; looks like this:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(assoc {::required-field :value}
       ::optional-field (when some-condition
                          :optional-value))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;There is a significant issue with this pattern however: if &lt;code&gt;::optional-field&lt;/code&gt; is on a &lt;code&gt;spec/keys&lt;/code&gt; &lt;code&gt;:opt&lt;/code&gt; field that is non-nilable, then every object that excludes the optional field will be invalid according to that spec because it will have a &lt;code&gt;nil&lt;/code&gt; value instead of omitting the key.&lt;/p&gt;
&lt;p&gt;The alternative to this approach is to use something like &lt;code&gt;cond-&amp;gt;&lt;/code&gt; as seen below:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(cond-&amp;gt; {::required-field :value}
  some-condition (assoc ::optional-field :optional-value))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This pattern is far from the only usage of &lt;code&gt;cond-&amp;gt;&lt;/code&gt;, but it &lt;a rel=&quot;nofollow&quot; href=&quot;https://grep.app/search?q=%5C%28cond-%3E%28.%2A%5Cn%29%7B1%2C3%7D.%2A%5C%28assoc%5Cs®exp=true&amp;amp;filter[lang][0]=Clojure&quot;&gt;appears fairly frequently&lt;/a&gt;. I see this pattern as detrimental because it requires the condition to be surfaced all the way at the top here, and there cannot be a way for some function used to produce &lt;code&gt;:optional-value&lt;/code&gt; to abort via nil punning while still producing a valid value without requiring additional flow control and local bindings.&lt;/p&gt;
&lt;p&gt;A more flexible solution to this problem uses &lt;code&gt;if-some&lt;/code&gt;, but this solution is verbose and becomes unwieldy when multiple optional fields need to be included at once, as can be seen below.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(let [m {::required-field :value}
      m (if-some [v (produces-optional-value)]
          (assoc m ::optional-field v)
          m)
      m (if-some [v (produces-other-value)]
          (assoc m ::other-field v)
          m)]
  m)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;One solution to this problem is the function &lt;code&gt;assoc-some&lt;/code&gt;, which is like &lt;code&gt;assoc&lt;/code&gt;, but when given a &lt;code&gt;nil&lt;/code&gt; value it elides the key. This function is provided in &lt;a rel=&quot;nofollow&quot; href=&quot;http://weavejester.github.io/medley/medley.core.html#var-assoc-some&quot;&gt;medley&lt;/a&gt;, and &lt;a rel=&quot;nofollow&quot; href=&quot;https://grep.app/search?q=assoc-some&amp;amp;filter[lang][0]=Clojure&quot;&gt;sees some use&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The above unwieldy example would, with &lt;code&gt;assoc-some&lt;/code&gt;, look like this:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(assoc-some {::required-field :value}
            ::optional-field (produces-optional-value)
            ::other-field (produces-other-value))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This is likely not the only solution to this problem, but I think it is a problem worth considering.&lt;/p&gt;
&lt;p&gt;An example of another instance where the named solution &lt;em&gt;wouldn't&lt;/em&gt; work, but that appears to follow more or less the same pattern is &lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/IGJoshua/farolero/blob/950d81e4a417770dd9d51b67358dbb2cc89b2cc2/src/cljc/farolero/core.cljc#L503-L516&quot;&gt;this code in farolero&lt;/a&gt;, which would be solved by a parallel function (that I have not yet found an implementation of in the wild besides my own) &lt;code&gt;update-some&lt;/code&gt; which will update a key with a new value, and if that value is &lt;code&gt;nil&lt;/code&gt; will remove the key.&lt;/p&gt;
</description>
<category>Collections</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/11584/common-patterns-optional-fields-interact-poorly-with-spec</guid>
<pubDate>Tue, 15 Feb 2022 21:48:34 +0000</pubDate>
</item>
<item>
<title>subvec doesn't preserve metadata</title>
<link>https://ask.clojure.org/index.php/11558/subvec-doesnt-preserve-metadata</link>
<description>&lt;p&gt;The vector returned by &lt;code&gt;subvec&lt;/code&gt; has not metadata even when the original vector has metadata.&lt;/p&gt;
&lt;p&gt;Example:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(meta (subvec 
       (with-meta [0 1 2 3 4 5] {:foo 42})
       0))
;; nil
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Expected value:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt; {:foo 42}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Live demo &lt;a rel=&quot;nofollow&quot; href=&quot;https://klipse-embed.herokuapp.com/?src=KG1ldGElMjAoc3VidmVjJTIwJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwKHdpdGgtbWV0YSUyMCU1QjAlMjAxJTIwMiUyMDMlMjA0JTIwNSU1RCUyMCU3QiUzQWZvbyUyMDQyJTdEKSUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMDApKQ%3D%3D&amp;amp;lang=clojure&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;
</description>
<category>Collections</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/11558/subvec-doesnt-preserve-metadata</guid>
<pubDate>Thu, 10 Feb 2022 15:36:55 +0000</pubDate>
</item>
<item>
<title>Somewhat confusing sorted-set behavior</title>
<link>https://ask.clojure.org/index.php/11409/somewhat-confusing-sorted-set-behavior</link>
<description>&lt;p&gt;Just ran into some interesting behavior with &lt;code&gt;sorted-set&lt;/code&gt;. Namely, the contract to the user for a normal &lt;code&gt;set&lt;/code&gt; is &lt;code&gt;nil&lt;/code&gt; when the element doesn't exist. However, with &lt;code&gt;sorted-set&lt;/code&gt;, the call will throw if the comparator doesn't gracefully handle the type. The &lt;em&gt;reason&lt;/em&gt; makes sense (the comparator requires the ability to sanely compare elements), but the interface is somewhat confusing in the &quot;no membership&quot; case since we know that the element doesn't exist in the set if its type can't be compared by default. &lt;/p&gt;
&lt;p&gt;In other words, I guess I'm proposing that a sane default behavior for a sorted-set using the default comparator should be &lt;code&gt;nil&lt;/code&gt; rather than &lt;code&gt;throw&lt;/code&gt; when the type cannot be compared, in order to allow &lt;code&gt;nil&lt;/code&gt;-punning. &lt;/p&gt;
&lt;p&gt;As an example, this case was encountered when the form was passed to a keyword function &lt;code&gt;(:foobar form)&lt;/code&gt;, which allows nil-punning on a lot of other collections. Here's a small snippet:&lt;/p&gt;
&lt;p&gt;&lt;code&gt;`&lt;/code&gt;&lt;br&gt;
user&amp;gt; (def normal #{&quot;0000&quot;})&lt;/p&gt;
&lt;p&gt;;; =&amp;gt; #'user/normal&lt;/p&gt;
&lt;p&gt;user&amp;gt; (def sorted-version (sorted-set &quot;0000&quot;))&lt;/p&gt;
&lt;p&gt;;; =&amp;gt; #'user/sorted-version&lt;/p&gt;
&lt;p&gt;user&amp;gt; normal&lt;/p&gt;
&lt;p&gt;;; =&amp;gt; #{&quot;0000&quot;}&lt;/p&gt;
&lt;p&gt;user&amp;gt; sorted-version&lt;/p&gt;
&lt;p&gt;;; =&amp;gt; #{&quot;0000&quot;}&lt;/p&gt;
&lt;p&gt;user&amp;gt; (:foobar normal)&lt;/p&gt;
&lt;p&gt;;; =&amp;gt; nil&lt;/p&gt;
&lt;p&gt;user&amp;gt; (:foobar sorted-version)&lt;/p&gt;
&lt;p&gt;Execution error (ClassCastException) at user/eval246355 (form-init59552761865518221.clj:8123).&lt;br&gt;
class java.lang.String cannot be cast to class clojure.lang.Keyword (java.lang.String is in module java.base of loader 'bootstrap'; clojure.lang.Keyword is in unnamed module of loader 'app')&lt;br&gt;
&lt;code&gt;`&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;I think my options for handling this are either try/catch or write a custom comparator, and both seem a little wonky :) &lt;/p&gt;
&lt;p&gt;I'm probably missing something, but thought it was interesting anyway :) &lt;/p&gt;
</description>
<category>Collections</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/11409/somewhat-confusing-sorted-set-behavior</guid>
<pubDate>Thu, 23 Dec 2021 12:22:35 +0000</pubDate>
</item>
<item>
<title>Rest for map destructuring</title>
<link>https://ask.clojure.org/index.php/11184/rest-for-map-destructuring</link>
<description>&lt;p&gt;I suggest adding &lt;code&gt;:rest&lt;/code&gt; key for map destructuring syntax. &lt;br&gt;
How do you think the following feature would be useful?&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(let [m {:a 1 :b 2}
      {:keys [a] :rest r} m]
  (assert (= {:b 2} r)))
      
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Or even like the rest of an array &lt;code&gt;[a b &amp;amp; r]&lt;/code&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(let [m {:a 1 :b 2}
      {:keys [a]
       :&amp;amp; r} m]
  (assert (= {:b 2} r)))
&lt;/code&gt;&lt;/pre&gt;
</description>
<category>Collections</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/11184/rest-for-map-destructuring</guid>
<pubDate>Mon, 25 Oct 2021 10:04:49 +0000</pubDate>
</item>
<item>
<title>Vector equiv() causes lazy seqs to be fully realized</title>
<link>https://ask.clojure.org/index.php/11171/vector-equiv-causes-lazy-seqs-to-be-fully-realized</link>
<description>&lt;p&gt;The implementation of &lt;code&gt;equiv()&lt;/code&gt; for PersistentVector involves calling &lt;code&gt;.size()&lt;/code&gt; on the object to be compared, even when it is a lazy seq.&lt;/p&gt;
&lt;p&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/clojure/clojure/blob/b8132f92f3c3862aa6cdd8a72e4e74802a63f673/src/jvm/clojure/lang/APersistentVector.java#L98&quot;&gt;https://github.com/clojure/clojure/blob/b8132f92f3c3862aa6cdd8a72e4e74802a63f673/src/jvm/clojure/lang/APersistentVector.java#L98&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;As a result, a predicate such as  &lt;code&gt;(= [1] (range))&lt;/code&gt; does not terminate. &lt;br&gt;
More practically, comparing vectors against large partially-realised seqs may cause them to be fully evaluated, where one might reasonably expect &lt;code&gt;=&lt;/code&gt; to short circuit to &lt;code&gt;false&lt;/code&gt; on the first non-matching element.&lt;/p&gt;
&lt;p&gt;Is this an intentional tradeoff for the more common use case where calling .size() on the second arg does not come with a performance cost? &lt;/p&gt;
&lt;p&gt;Interestingly, this size comparison is not implemented for Clojure's persistent lists even though they are counted collections, i.e.  &lt;code&gt;(= '(1) (range))&lt;/code&gt; terminates. &lt;/p&gt;
&lt;p&gt;Thanks to @opqdonut on Clojurians Slack for helping to pinpoint the cause in the Java source.&lt;/p&gt;
</description>
<category>Collections</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/11171/vector-equiv-causes-lazy-seqs-to-be-fully-realized</guid>
<pubDate>Fri, 15 Oct 2021 10:12:53 +0000</pubDate>
</item>
<item>
<title>Persistent collections can implement equiv() more efficiently</title>
<link>https://ask.clojure.org/index.php/11124/persistent-collections-implement-equiv-more-efficiently</link>
<description>&lt;p&gt;I found that structural equality between persistent collections makes very few assumptions which lead to inefficient implementations, especially for vectors and maps.&lt;/p&gt;
&lt;p&gt;The thrust of the implementation is dispatching via methods which directly iterate over the underlying arrays.&lt;/p&gt;
&lt;p&gt;These implementations aren't the prettiest or most idiomatic but they're efficient. If this gets implemented it would look different in Java anyway.&lt;/p&gt;
&lt;p&gt;I tried these alternative implementations and found dramatic speed ups:&lt;/p&gt;
&lt;h3&gt;Vector&lt;/h3&gt;
&lt;pre&gt;&lt;code&gt;(let [die (clojure.lang.Reduced. false)]
  (defn vec-eq
    [^PersistentVector v ^Iterable y]
    (let [iy (.iterator y)]
      (.reduce v (fn [_ x] (if (= x (.next iy)) true die)) true))))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This works well when comparing vectors and for vector x list&lt;br&gt;
Current implementation goes through a loop from 0 to count and calls nth for every element. nth calls arrayFor() every time, while both reduce and an iterator get the backing array once per array.&lt;/p&gt;
&lt;h3&gt;Map&lt;/h3&gt;
&lt;pre&gt;&lt;code&gt;(let [o (Object.)
      die (clojure.lang.Reduced. false)
      eq (fn [m2] (fn [b k v]
                   (let [v' (.valAt ^IPersistentMap m2 k o)]
                     (if (.equals o v')
                       die
                       (if (= v v') true die)))))]
  (defn map-eq
    [m1 m2]
    (.kvreduce ^IKVReduce m1 (eq m2) true)))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Here, too, the implementation iterates directly over the underlying array structure.&lt;br&gt;
Current implementation casts the array to seq then iterates over it while getting entries from the other map via the &lt;code&gt;Map&lt;/code&gt; interface.&lt;br&gt;
This implementation avoids casting the map to a sequence and does not allocate entries.&lt;/p&gt;
&lt;h3&gt;Sequences&lt;/h3&gt;
&lt;p&gt;When the receiver is a list the object compared against it and the receiver will be cast to a seq.&lt;/p&gt;
&lt;p&gt;It could be more efficient to compare it with other collections via an iterator&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(defn iter-eq
  [^Iterable x ^Iterable y]
  (let [ix (.iterator x)
        iy (.iterator y)]
    (loop []
      (if (.hasNext ix)
        (if (= (.next ix) (.next iy))
          (recur)
          false)
        true))))
&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;Benchmarking&lt;/h3&gt;
&lt;p&gt;With criterium, vec-eq wins both cases. There are diminishing returns with size increase but still at n=64 vec-eq is twice as fast as =.&lt;br&gt;
map-eq is also 2-3x faster for bigger maps and up to 10x faster for smaller maps&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(doseq [n [1 2 4 8 16 32 64]
        :let [v1 (vec (range n))
              v2 (vec (range n))]]
  (println 'iter-eq n (iter-eq v1 v2))
  (cc/quick-bench (iter-eq v1 v2))
  (println 'vec-eq n (vec-eq v1 v2))
  (cc/quick-bench (vec-eq v1 v2))
  (println '= n (= v1 v2))
  (cc/quick-bench (= v1 v2)))


(doseq [n [1 2 4 8 16 32 64]
        :let [v1 (vec (range n))
              v2 (list* (range n))]]
  (println 'iter-eq n (iter-eq v1 v2))
  (cc/quick-bench (iter-eq v1 v2))
  (println 'vec-eq n (vec-eq v1 v2))
  (cc/quick-bench (vec-eq v1 v2))
  (println '= n (= v1 v2))
  (cc/quick-bench (= v1 v2)))
&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;(doseq [n [1 2 4 8 16 32 64]
        :let [m1 (zipmap (range n) (range n))
              m2 (zipmap (range n) (range n))]]
  (cc/quick-bench (map-eq m1 m2))
  (cc/quick-bench (= m1 m2)))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Addendum:&lt;br&gt;
Also checked the following cases:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(doseq [n [10000 100000]
        :let [v1 (vec (range n))
              v2 (assoc v1 (dec (count v1)) 7)]]
  (cc/quick-bench (vec-eq v1 v2))
  (cc/quick-bench (iter-eq v1 v2))
  (cc/quick-bench (= v1 v2)))

(doseq [n [100000]
        :let [m1 (zipmap (range n) (range n))
              m2 (assoc m1 (key (last m1)) 7)]]
  (cc/quick-bench (map-eq m1 m2))
  (cc/quick-bench (= m1 m2)))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Optimized implementations still win by huge margins&lt;/p&gt;
</description>
<category>Collections</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/11124/persistent-collections-implement-equiv-more-efficiently</guid>
<pubDate>Thu, 30 Sep 2021 16:57:25 +0000</pubDate>
</item>
<item>
<title>get/find/assoc on vectors overflows key when passed large longs</title>
<link>https://ask.clojure.org/index.php/11080/get-find-assoc-vectors-overflows-key-when-passed-large-longs</link>
<description>&lt;p&gt;Is this expected behavior?&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;clj
Clojure 1.10.3
user=&amp;gt; (get [1 2 42] 4294967295)
nil
user=&amp;gt; (get [1 2 42] 4294967296)
1
user=&amp;gt; (get [1 2 42] 4294967297)
2
user=&amp;gt; (get [1 2 42] 4294967298)
42
user=&amp;gt; (assoc [1 2 42] 4294967298 :wow)
[1 2 :wow]
user=&amp;gt; (find [1 2 42] 4294967298)
[4294967298 42]
user=&amp;gt; (.intValue 4294967298)
2
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;(Note this is with Clojure 1.10.3, but the syntax highlighting seems to truncate the last part).&lt;/p&gt;
&lt;p&gt;AFAICT the &lt;code&gt;get&lt;/code&gt; is related to &lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/clojure/clojure/blob/b8132f92f3c3862aa6cdd8a72e4e74802a63f673/src/jvm/clojure/lang/APersistentVector.java#L353&quot;&gt;this line&lt;/a&gt; in APersistentVector. The other two are caused by similar surrounding lines.&lt;/p&gt;
</description>
<category>Collections</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/11080/get-find-assoc-vectors-overflows-key-when-passed-large-longs</guid>
<pubDate>Wed, 22 Sep 2021 17:19:46 +0000</pubDate>
</item>
<item>
<title>Would it be funner if clojure.set/difference worked better with sequences?</title>
<link>https://ask.clojure.org/index.php/10972/would-funner-clojure-difference-worked-better-with-sequences</link>
<description>&lt;p&gt;Thanks as ever for Clojure- we love it, and use it all day every day to solve worthy problems  in ways that delight us as programmers and wow our users as well.&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;I was naively taking set differences, hoping for this, and was pleased:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;gt; (clojure.set/difference #{:a :b :c} '(:a 1 2))
#{:c :b}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then I did this, and was surprised:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;gt; (clojure.set/difference #{:a :b :c} '(:a 1 2 3))
Execution error (IllegalArgumentException)
contains? not supported on type: clojure.lang.PersistentList
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then I read the implementation of &lt;code&gt;clojure.set/difference&lt;/code&gt;, and I get it now:&lt;/p&gt;
&lt;p&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/clojure/clojure/blob/a29f9b/src/clj/clojure/set.clj#L49-62&quot;&gt;https://github.com/clojure/clojure/blob/a29f9b/src/clj/clojure/set.clj#L49-62&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;But, I do wonder if it would be funner if this just worked. And since funner is probably a more important criteria to me than it is to you, I wonder what the real tradeoffs are here.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Is it performance? &lt;/li&gt;
&lt;li&gt;Or, what would be the downside of implicitly converting to something that implements &lt;code&gt;contains?&lt;/code&gt; at the critical moment?&lt;/li&gt;
&lt;/ul&gt;
</description>
<category>Collections</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/10972/would-funner-clojure-difference-worked-better-with-sequences</guid>
<pubDate>Wed, 25 Aug 2021 22:15:08 +0000</pubDate>
</item>
<item>
<title>Could we expose the data behind ranges?</title>
<link>https://ask.clojure.org/index.php/10758/could-we-expose-the-data-behind-ranges</link>
<description>&lt;p&gt;dtype-next, libpython-clj, and tech.ml.dataset all assign special meaning to ranges but they often have to deconstruct them to do so.  For libpython, we create actual python ranges in some cases.  For dtype-next and friends, a range with increment of 1 may indicate a sub-buffer operation as opposed to an indexed-buffer operation -- sub-buffer retains the ability for System/arraycopy and the like to work correctly and is thus a major optimization in some cases.  &lt;/p&gt;
&lt;p&gt; I would like to be able to get the start, step, and end (when it exists) from any object created via &lt;code&gt;clojure.core/range&lt;/code&gt;.  I can currently do this via reflection or via subtraction of the first and second members of the sequence generated via the range along with count but I think it may be a safe and reasonable change to be able to query this information directly.&lt;/p&gt;
&lt;p&gt;Most of this could be achieved by simply making the members start, step, and end members of LongRange and Range public as they are &lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/clojure/clojure/blob/master/src/jvm/clojure/lang/LongRange.java#L27&quot;&gt;already final&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;As a separate discussion it may be worth considering having finite ranges work like persistent vectors.  I don't want to confuse the above point with this one but it sometimes more efficient to index into ranges via nth or an IFn invoke pathway than it is to use Clojure's sequence abstraction.  Finite ranges are logically to me more like persistent vectors in nature and potentially they could behave precisely like persistent vectors including w/r/t hashcode, deriving from &lt;code&gt;java.util.RandomAccess&lt;/code&gt;, and having conj produce either a new range or a persistent vector depending on if the value fits properly in the sequence.&lt;/p&gt;
</description>
<category>Collections</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/10758/could-we-expose-the-data-behind-ranges</guid>
<pubDate>Sat, 10 Jul 2021 12:45:09 +0000</pubDate>
</item>
<item>
<title>bug in walker with array-maps</title>
<link>https://ask.clojure.org/index.php/10253/bug-in-walker-with-array-maps</link>
<description>&lt;p&gt;The walker doesn't handle array-maps properly, IMO. It converts them to regular maps and loses the ordering information.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;gt; (def x (array-map :a 1 :b 2 :c 3 :d 4 :e 5 :f 6 :g 7 :h 8 :i 9 :j 10))
#'x
&amp;gt; (class x)
clojure.lang.PersistentArrayMap
&amp;gt; x
{:e 5, :g 7, :c 3, :j 10, :h 8, :b 2, :d 4, :f 6, :i 9, :a 1}
&amp;gt; (def xw (clojure.walk/postwalk identity x))
#'xw
&amp;gt; (class xw)
clojure.lang.PersistentHashMap
&amp;gt; xw
{:e 5, :g 7, :c 3, :j 10, :h 8, :b 2, :d 4, :f 6, :i 9, :a 1}
&lt;/code&gt;&lt;/pre&gt;
</description>
<category>Collections</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/10253/bug-in-walker-with-array-maps</guid>
<pubDate>Sat, 27 Feb 2021 00:06:52 +0000</pubDate>
</item>
<item>
<title>Why does (conj) returns vector but (conj nil 1) returns list and (conj nil) returns nil?</title>
<link>https://ask.clojure.org/index.php/10063/why-does-conj-returns-vector-conj-returns-list-conj-returns</link>
<description>&lt;p&gt;Why does &lt;code&gt;(conj)&lt;/code&gt; returns vector but &lt;code&gt;(conj nil 1)&lt;/code&gt; returns list and &lt;code&gt;(conj nil)&lt;/code&gt; returns nil?&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(conj)
;=&amp;gt; []
(conj nil)
;=&amp;gt; nil
(conj nil 1)
;=&amp;gt; (1)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;It seems a little inconsistent no?&lt;/p&gt;
</description>
<category>Collections</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/10063/why-does-conj-returns-vector-conj-returns-list-conj-returns</guid>
<pubDate>Sun, 17 Jan 2021 19:57:13 +0000</pubDate>
</item>
<item>
<title>Couples of consecutive elements in a coll</title>
<link>https://ask.clojure.org/index.php/9974/couples-of-consecutive-elements-in-a-coll</link>
<description>&lt;p&gt;What's the most idiomatic way of writing a function that, given a coll &lt;code&gt;(1 2 3 4 5)&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Returns&lt;/p&gt;
&lt;p&gt;&lt;code&gt;((1 2) (2 3) (3 4) (4 5))&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;i.e. consecutive couples of elements in a collection.&lt;/p&gt;
&lt;p&gt;My naive solution would be:&lt;/p&gt;
&lt;p&gt;&lt;code&gt;(map (fn [a b] [a b]) a (rest a))&lt;/code&gt;&lt;/p&gt;
</description>
<category>Collections</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/9974/couples-of-consecutive-elements-in-a-coll</guid>
<pubDate>Thu, 24 Dec 2020 13:22:59 +0000</pubDate>
</item>
<item>
<title>How do I update map elements conditionally?</title>
<link>https://ask.clojure.org/index.php/9477/how-do-i-update-map-elements-conditionally</link>
<description>&lt;p&gt;I have some code that's massaging a data structure from one format to another (SVG hiccup attributes to CLJFX attributes). The format are quite similar but when an attribute is present I need to occasionally futz with it a bit. For instance, maybe I need to (read-string ..) a string into a number, or update a keyword so it's spelled a bit different, etc.&lt;/p&gt;
&lt;p&gt;Right now my code is of the form&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(-&amp;gt; svg-attributes
  (update :stroke #(case %
                     nil :black
                     &quot;none&quot; :black
                     (if (= \# %)
                       %
                       (keyword %))))
  (update :fill #(case %
                   nil &quot;transparent&quot;
                   &quot;none&quot; &quot;transparent&quot;
                   (if (= \# %)
                     %
                     (keyword %))))
  (update :points #(case %
                     nil []
                     [] []
                     (map read-string (-&amp;gt; %
                                          (clojure.string/split #&quot;[ ,]&quot;)))))
  (update :stroke-width #(case %
                           nil 1.0
                           %))
  (update :stroke-dasharray #(case %
                               nil []
                               (map read-string (-&amp;gt; %
                                                    (clojure.string/split #&quot;[ ,]&quot;)))))
  (clojure.set/rename-keys {:stroke-dasharray :stroke-dash-array})
  (update :font-size #(case %
                        nil 10
                        &quot;none&quot; 10
                        %))))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The issue is that I keep having to catch the &lt;code&gt;nil&lt;/code&gt; case and jamming in some defaults... which works most of the time.. but  I'd rather skip updating when there is nothing to update.&lt;/p&gt;
&lt;p&gt;I saw this question, which is quite similar: &lt;a rel=&quot;nofollow&quot; href=&quot;https://ask.clojure.org/index.php/8387/how-to-avoid-nil-values-in-maps&quot;&gt;https://ask.clojure.org/index.php/8387/how-to-avoid-nil-values-in-maps&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Is the idiomatic solution to do a &lt;code&gt;(cond-&amp;gt; &lt;/code&gt; with a &lt;code&gt;(some? (:somekeyword %))&lt;/code&gt; on each line ? I feel like I'm not reaching for the right tool here! Maybe someone can give me a better suggestion :)&lt;/p&gt;
</description>
<category>Collections</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/9477/how-do-i-update-map-elements-conditionally</guid>
<pubDate>Mon, 20 Jul 2020 05:59:14 +0000</pubDate>
</item>
<item>
<title>Strange behavior of apply vector with a function as element</title>
<link>https://ask.clojure.org/index.php/9267/strange-behavior-of-apply-vector-with-a-function-as-element</link>
<description>&lt;p&gt;I don't understand why &lt;code&gt;apply vector&lt;/code&gt; on a list doesn't return the same collection than &lt;code&gt;vector&lt;/code&gt; or &lt;code&gt;[]&lt;/code&gt;, when at least one of the elements of the list is a function&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(= [1 2 3] (apply vector '(1 2 3)))
=&amp;gt; true   
(= [int 2 3] (apply vector '(int 2 3)))
=&amp;gt; false
(= [int 2 3] (vector int 2 3))
=&amp;gt; true
(apply vector '(int 2 3))
=&amp;gt; [int 2 3]
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Clojure 1.8.0 Java HotSpot(TM) 64-Bit Server VM 1.8.0_91-b14&lt;/p&gt;
</description>
<category>Collections</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/9267/strange-behavior-of-apply-vector-with-a-function-as-element</guid>
<pubDate>Sun, 26 Apr 2020 09:07:26 +0000</pubDate>
</item>
<item>
<title>Retrieving the domain/range of finite functions</title>
<link>https://ask.clojure.org/index.php/9251/retrieving-the-domain-range-of-finite-functions</link>
<description>&lt;p&gt;One of the great features of Clojure is that it considers collections as functions which means that it is possible to &lt;strong&gt;invoke&lt;/strong&gt; them&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;({:a 1, :b 2} :a) =&amp;gt; 1
(#{:a :b} :a) =&amp;gt; :a
([:a :b :c] 1) =&amp;gt; :b
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;However it would be nice to be able to query the &lt;strong&gt;domain&lt;/strong&gt; and &lt;strong&gt;range&lt;/strong&gt; of those functions independently of their specific type.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;keys&lt;/code&gt; and &lt;code&gt;vals&lt;/code&gt; functions currently implement this but only for Maps. I think it would be a good idea to make those functions more generic by accepting sets and vectors like in the following examples:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(keys  #{:a :b}) =&amp;gt; (:a :b)
(vals #{:a :b}) =&amp;gt; (:a :b)
(keys [:a :b :c]) =&amp;gt; (0 1 2)
(vals [:a :b :c]) =&amp;gt; (:a :b :c)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The specific use case I have in mind where this feature could be useful is to define a shorter syntax for explicit foreign key mapping in a data oriented DSL for relational data modeling, where &lt;code&gt;#{:attr1 :attr2}&lt;/code&gt; could be used as a shortcut for &lt;code&gt;{:attr1 :attr1, :attr2 :attr2}&lt;/code&gt;.&lt;/p&gt;
</description>
<category>Collections</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/9251/retrieving-the-domain-range-of-finite-functions</guid>
<pubDate>Sun, 19 Apr 2020 18:32:29 +0000</pubDate>
</item>
<item>
<title>Clojure functions to implement the Map interface. Why or Why not?</title>
<link>https://ask.clojure.org/index.php/8798/clojure-functions-to-implement-the-map-interface-why-why-not</link>
<description>&lt;p&gt;From a theoretical standpoint, there really isn't a difference between a Map and a 1-arity function&lt;/p&gt;
&lt;p&gt;What do you think of having a Function implement the Map interface? Will it be useful? If this  was  thought and  you guys  decided not to go with it, why not? Would love to learn the thinking&lt;/p&gt;
</description>
<category>Collections</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/8798/clojure-functions-to-implement-the-map-interface-why-why-not</guid>
<pubDate>Thu, 31 Oct 2019 17:13:37 +0000</pubDate>
</item>
<item>
<title>Should I prefer nil to empty collections?</title>
<link>https://ask.clojure.org/index.php/8415/should-i-prefer-nil-to-empty-collections</link>
<description>&lt;p&gt;This is a question about intersection of 3 different clojure aspects:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Collection functions treat &lt;code&gt;nil&lt;/code&gt; as empty collection. Functions like &lt;code&gt;filter&lt;/code&gt;, &lt;code&gt;map&lt;/code&gt;, and even &lt;code&gt;assoc&lt;/code&gt; happily accept &lt;code&gt;nil&lt;/code&gt; for coll.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;if&lt;/code&gt; and derived macros (&lt;code&gt;when&lt;/code&gt;, &lt;code&gt;and&lt;/code&gt;, &lt;code&gt;or&lt;/code&gt; etc.) treat &lt;code&gt;nil&lt;/code&gt; as falsey value.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;empty?&lt;/code&gt; coerces it's input to &lt;code&gt;seq&lt;/code&gt;, which leads to unnecessary allocations, and it's idiomatic to check for not-emptyness using &lt;code&gt;seq&lt;/code&gt; instead of &lt;code&gt;(not (empty? xs))&lt;/code&gt;. This is a common source of confusion, because &lt;code&gt;(not (empty? xs))&lt;/code&gt;'s intent feels more clear then &lt;code&gt;seq&lt;/code&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;With that said, I feel like consciously representing every empty collection in application as &lt;code&gt;nil&lt;/code&gt; might be useful, because this:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(let [xs (get-non-empty-coll-or-nil)]
  (if xs
    (do-stuff-with xs)
    (do-nothing)))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;  ...is more performant and clear than this:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(let [xs (get-possibly-empty-coll)]
  (if (seq xs)
    (do-stuff-with xs)
    (do-nothing)))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Two downsides I see to this approach:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;having to use &lt;code&gt;(fnil conj [])&lt;/code&gt; or &lt;code&gt;(fnil conj #{})&lt;/code&gt; instead of &lt;code&gt;conj&lt;/code&gt; to ensure collections are vectors/sets, because &lt;code&gt;conj&lt;/code&gt;-ing to &lt;code&gt;nil&lt;/code&gt; creates a list, which I personally almost never use.&lt;/li&gt;
&lt;li&gt;having to run all incoming collections on the boundaries of a system through &lt;code&gt;not-empty&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;What do you think?&lt;/p&gt;
</description>
<category>Collections</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/8415/should-i-prefer-nil-to-empty-collections</guid>
<pubDate>Wed, 14 Aug 2019 09:12:35 +0000</pubDate>
</item>
<item>
<title>= - doc perhaps not precise enough</title>
<link>https://ask.clojure.org/index.php/8400/doc-perhaps-not-precise-enough</link>
<description>&lt;p&gt;looking at =&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(defn ^boolean =
  &quot;Equality. Returns true if x equals y, false if not. Compares
  numbers and collections in a type-independent manner.  Clojure's immutable data
  structures define -equiv (and thus =) as a value, not an identity,
  comparison.&quot;
  ([x] true)
  ([x y]
    (if (nil? x)
      (nil? y)
      (or (identical? x y)
        ^boolean (-equiv x y))))
  ([x y &amp;amp; more]
     (if (= x y)
       (if (next more)
         (recur y (first more) (next more))
         (= y (first more)))
       false)))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;evaluating the following seams surprising to me:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(= [1] #{1})
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;because a vector is a collection and a set is also a collection, so when it says &lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;Compares numbers and collections in a type-independent manner.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;i think it should say:&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;Compares numbers, sequentials, maps and sets in a type-independent&lt;br&gt;
manner.&lt;/p&gt;
&lt;/blockquote&gt;
</description>
<category>Collections</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/8400/doc-perhaps-not-precise-enough</guid>
<pubDate>Sun, 11 Aug 2019 08:57:06 +0000</pubDate>
</item>
<item>
<title>peek does not support transient vectors</title>
<link>https://ask.clojure.org/index.php/4216/peek-does-not-support-transient-vectors</link>
<description>&lt;p&gt;Appears to be an oversight, given that peek is a read operation.&lt;/p&gt;
&lt;p&gt;Input:&lt;/p&gt;
&lt;p&gt;&lt;code&gt;(peek (transient [:a :b :c]))&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Expected outcome:&lt;/p&gt;
&lt;p&gt;&lt;code&gt;:c&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Actual outcome:&lt;/p&gt;
&lt;p&gt;&lt;code&gt;Unhandled java.lang.ClassCastException
clojure.lang.PersistentVector$TransientVector cannot be cast to clojure.lang.IPersistentStack&lt;/code&gt;&lt;/p&gt;
</description>
<category>Collections</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/4216/peek-does-not-support-transient-vectors</guid>
<pubDate>Tue, 08 Jan 2019 05:25:53 +0000</pubDate>
</item>
</channel>
</rss>