Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I don't know of any existing ways of doing that, maybe it would be a good idea to browse clojure.contrib documentation to look if there's already something. In the meantime, I've looked at your example and cleared it up a little bit. </p> <pre><code>(defn report [cnt] (when (even? cnt) (println "Done" cnt))) (defn report-progress [] (let [aseq (range 10)] (doall (map report (take (count aseq) (iterate inc 1)))) aseq)) </code></pre> <p>You're heading in the right direction, even though this example is too simple. This gave me an idea about a more generalized version of your report-progress function. This function would take a map-like function, the function to be mapped, a report function and a set of collections (or a seed value and a collection for testing reduce).</p> <pre><code>(defn report-progress [m f r &amp; colls] (let [result (apply m (fn [&amp; args] (let [v (apply f args)] (apply r v args) v)) colls)] (if (seq? result) (doall result) result))) </code></pre> <p>The seq? part is there only for use with reduce which doesn't necessarily returns a sequence. With this function, we can rewrite your example like this:</p> <pre><code>user&gt; (report-progress map (fn [_ v] v) (fn [result cnt _] (when (even? cnt) (println "Done" cnt))) (iterate inc 1) (range 10)) Done 2 Done 4 Done 6 Done 8 Done 10 (0 1 2 3 4 5 6 7 8 9) </code></pre> <p>Test the filter function:</p> <pre><code>user&gt; (report-progress filter odd? (fn [result cnt] (when (even? cnt) (println "Done" cnt))) (range 10)) Done 0 Done 2 Done 4 Done 6 Done 8 (1 3 5 7 9) </code></pre> <p>And even the reduce function:</p> <pre><code>user&gt; (report-progress reduce + (fn [result s v] (when (even? s) (println "Done" s))) 2 (repeat 10 1)) Done 2 Done 4 Done 6 Done 8 Done 10 12 </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload