<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Clojure Q&amp;A - Recent questions tagged reducers</title>
<link>https://ask.clojure.org/index.php/tag/reducers</link>
<description>Powered by Question2Answer</description>
<item>
<title>Why do r/foldcat return types give inconsistent results when passed directly to `vec`?</title>
<link>https://ask.clojure.org/index.php/12984/foldcat-return-types-inconsistent-results-passed-directly</link>
<description>&lt;p&gt;When using reducers, I will occasionally use foldcat to &quot;realize&quot; the transformed collection through the reducer chain. For downstream code, its preferable to have a fully realized standard clojure collection like &lt;code&gt;vec&lt;/code&gt;. This is often the case when updating core collection code to use reducers for performance reasons:&lt;/p&gt;
&lt;p&gt;Note the following works just fine:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(-&amp;gt;&amp;gt; (repeat 2 3) (vec) (r/map inc) (r/foldcat) (vec))
=&amp;gt; [4 4]
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If the input gets large enough however, this triggers the parallel behavior which changes the return type from Array to Cat and causes the last &lt;code&gt;vec&lt;/code&gt; to choke:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(-&amp;gt;&amp;gt; (repeat 513 3) (vec) (r/map inc) (r/foldcat) (vec))
Execution error at user/eval19976 (form-init6687476487684153971.clj:1).
Unable to convert: class clojure.core.reducers.Cat to Object[]
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I surmise that the &quot;correct&quot; way to do this is to use &lt;code&gt;(into [])&lt;/code&gt; instead of &lt;code&gt;vec&lt;/code&gt; in the last position which works just fine.&lt;/p&gt;
&lt;p&gt;That said, this came as fairly surprising behavior given that &lt;code&gt;seq&lt;/code&gt; and &lt;code&gt;into&lt;/code&gt; work just fine on both &lt;code&gt;Array&lt;/code&gt; and &lt;code&gt;Cat&lt;/code&gt;. Its also quite dangerous because such code can function quietly in production until the input data grows passed a certain threshold. Is there pragmatic reason that &lt;code&gt;seq&lt;/code&gt; works for &lt;code&gt;Cat&lt;/code&gt; but &lt;code&gt;vec&lt;/code&gt; doesn&#039;t?&lt;/p&gt;
</description>
<category>Sequences</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/12984/foldcat-return-types-inconsistent-results-passed-directly</guid>
<pubDate>Tue, 30 May 2023 17:28:26 +0000</pubDate>
</item>
<item>
<title>Why is r/fold not parallelized across all available cpus?</title>
<link>https://ask.clojure.org/index.php/9475/why-is-r-fold-not-parallelized-across-all-available-cpus</link>
<description>&lt;p&gt;I would have expected the below to be parallelized across all available cpus (in this case 12) but it only runs on one. What am I doing wrong?&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(ns csv2summap.core
  (:require [clojure.data.csv :as csv]
            [clojure.java.io :as io]
            [clojure.core.reducers :as r])))

(with-open [writer (io/writer &quot;numbers.csv&quot;)]
  (csv/write-csv
   writer
   (take 10000000
         (repeatedly #(vector (char (+ 65 (rand-int 26))) (rand-int 1000))))))

(defn sum-vals
([] {})
([m [k v]]
 (update m k (fnil + 0) (Integer/parseInt v))))

(defn merge-sums
([] {})
([&amp;amp; m] (apply merge-with + m)))

(time
(with-open [reader (io/reader &quot;numbers.csv&quot;)]
  (doall
   (r/fold
    (/ 10000000 12)
    merge-sums
    sum-vals
    (csv/read-csv reader)))))
&lt;/code&gt;&lt;/pre&gt;
&lt;blockquote&gt;&lt;pre&gt;&lt;code&gt;(def n-cpu (.availableProcessors (Runtime/getRuntime)))
=&amp;gt;12
&lt;/code&gt;&lt;/pre&gt;
&lt;/blockquote&gt;
&lt;pre&gt;&lt;code&gt;(ns csv2summap.core
  (:require [clojure.data.csv :as csv]
            [clojure.java.io :as io]
            [clojure.core.reducers :as r])))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt; -&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(defproject csv2summap &quot;0.1.0-SNAPSHOT&quot;
  :description &quot;FIXME: write description&quot;
  :url &quot;http://example.com/FIXME&quot;
  :license {:name &quot;EPL-2.0 OR GPL-2.0-or-later WITH Classpath-exception-2.0&quot;
            :url &quot;https://www.eclipse.org/legal/epl-2.0/&quot;}
  :dependencies [[org.clojure/clojure &quot;1.10.1&quot;]
                 [org.clojure/data.csv &quot;1.0.0&quot;]]
  :repl-options {:init-ns csv2summap.core})
&lt;/code&gt;&lt;/pre&gt;
</description>
<category>Sequences</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/9475/why-is-r-fold-not-parallelized-across-all-available-cpus</guid>
<pubDate>Sun, 19 Jul 2020 11:14:29 +0000</pubDate>
</item>
</channel>
</rss>