<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Clojure Q&amp;A - Recent questions tagged reduce</title>
<link>https://ask.clojure.org/index.php/tag/reduce</link>
<description>Powered by Question2Answer</description>
<item>
<title>reduce on a dropped vector-of never terminates with 65505 or more elements</title>
<link>https://ask.clojure.org/index.php/15302/reduce-dropped-vector-never-terminates-with-65505-elements</link>
<description>&lt;p&gt;Clojure 1.12.7, JDK 21.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;reduce&lt;/code&gt; over a &lt;code&gt;drop&lt;/code&gt;ped &lt;code&gt;vector-of&lt;/code&gt; never returns once the vector has 65505 or more elements. A regular vector works.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(reduce + (drop 1 (into (vector-of :long) (range 65504))))
;; =&amp;gt; 2145354256
(reduce + (drop 1 (into (vector-of :long) (range 65505))))
;; never returns
(reduce + (drop 1 (vec (range 65505))))
;; =&amp;gt; 2145419760
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The same happens on other paths that reduce a &lt;code&gt;VecSeq&lt;/code&gt; wrapped in a &lt;code&gt;LazySeq&lt;/code&gt; (&lt;code&gt;into&lt;/code&gt;, &lt;code&gt;transduce&lt;/code&gt;, &lt;code&gt;run!&lt;/code&gt;, &lt;code&gt;reduce&lt;/code&gt; after &lt;code&gt;lazy-seq&lt;/code&gt;). &lt;code&gt;(reduce f (seq v))&lt;/code&gt; goes through the iterator and is not affected.&lt;/p&gt;
&lt;p&gt;The cause is the chunk step in &lt;code&gt;VecSeq&lt;/code&gt;&#039;s &lt;code&gt;internal-reduce&lt;/code&gt;, &lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/clojure/clojure/blob/clojure-1.12.7/src/clj/clojure/gvec.clj#L79&quot;&gt;gvec.clj line 79&lt;/a&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(recur result (bit-and 0xffe0 (+ aidx 32)))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The mask is meant to round down to a multiple of 32, but &lt;code&gt;0xffe0&lt;/code&gt; also clears bit 16 and above. After the chunk that starts at index 65504, the next chunk start should be 65536, but the mask turns it into 0, so the reduce starts over from the beginning forever. A mask that only clears the low 5 bits, such as &lt;code&gt;(bit-not 0x1f)&lt;/code&gt;, keeps the index.&lt;/p&gt;
&lt;p&gt;The mask has been there since commit &lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/clojure/clojure/commit/5b281880&quot;&gt;5b281880&lt;/a&gt; (2010), which added &lt;code&gt;internal-reduce&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;This bug is reported at &lt;a rel=&quot;nofollow&quot; href=&quot;https://s.conao3.com/blog/2026/e4f733&quot;&gt;https://s.conao3.com/blog/2026/e4f733&lt;/a&gt;&lt;/p&gt;
</description>
<category>Sequences</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/15302/reduce-dropped-vector-never-terminates-with-65505-elements</guid>
<pubDate>Tue, 24 Nov 2026 08:24:42 +0000</pubDate>
</item>
</channel>
</rss>