You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
</code></pre><p>TBD: more realistic examples | <ahref="https://github.com/clojure-doc/clojure-doc.github.io#how-to-contribute">How to Contribute</a></p><h2id="clojure-functions-implement-runnable-and-callable">Clojure Functions Implement Runnable and Callable</h2><p>Note that Clojure functions implement <code>java.lang.Runnable</code> and
327
+
</code></pre><p>TBD: more realistic examples | <ahref="https://github.com/clojure-doc/clojure-doc.github.io#how-to-contribute">How to Contribute</a></p><h2id="consuming-java-streams">Consuming Java Streams</h2><p>The <ahref="https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/util/stream/Stream.html">Java Streams API</a>
328
+
introduced in Java 8 provided a way to work with collections of data in a
329
+
functional style: <code>filter</code>, <code>map</code>, <code>reduce</code>, <code>iterate</code>, and so on. We're used
330
+
to this in Clojure because <code>clojure.core</code> provides these operations across
331
+
sequences. In Clojure, that means we can apply those functions to anything
332
+
that can be coerced into a sequence, such as vectors, lists, sets, maps, and
Unfortunately, Java Streams are not <code>Iterable</code> so they were hard to consume
336
+
with Clojure until Clojure 1.12 added four functions that can consume them:</p><ul><li><code>(stream-seq! stream)</code> -- consume a Stream and produce a Clojure sequence,</li><li><code>(stream-reduce! f init stream)</code> -- reduce a Stream using <code>f</code> and the <code>init</code> value; <code>init</code> is optional,</li><li><code>(stream-transduce! xf f init stream)</code> -- transduce a Stream using <code>xf</code>, <code>f</code> and the <code>init</code> value; <code>init</code> is optional,</li><li><code>(stream-into! to-coll xf stream)</code> -- consume a Stream and add its elements to a collection <code>coll</code>; <code>xf</code> is optional.</li></ul><pre><codeclass="clojure">user=> (import '(java.util.stream Stream))
337
+
java.util.stream.Stream
338
+
user=> (Stream/iterate 0 inc) ; like Clojure's (iterate inc 0)
user=> (take 5 (stream-seq! *1)) ; consume the stream as a sequence
346
+
(0 1 2 3 4)
347
+
</code></pre><p>Several methods in the Java standard library produce Streams, for example:</p><pre><codeclass="clojure">user=> (import '(java.nio.file Files Path))
</code></pre><h2id="clojure-functions-implement-runnable-and-callable">Clojure Functions Implement Runnable and Callable</h2><p>Note that Clojure functions implement <code>java.lang.Runnable</code> and
328
355
<code>java.util.concurrent.Callable</code> directly so you can pass functions to
329
356
methods found in various classes from the <code>java.util.concurrent</code> package.</p><p>For example, to run a function in a new thread:</p><pre><codeclass="clojure">(let [t (Thread. (fn []
0 commit comments