Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use java.util.stream.Stream in Clojure?

(import java.nio.file.Files)
(import java.nio.file.Paths)
(import java.util.stream.Stream)

(def path 
  (Paths/get "." 
    (into-array ["data" "10000000.test.log"])))

(def stream 
  (Files/lines path))

This way I have:

stream
#object[java.util.stream.ReferencePipeline$Head 0x50129b8f
"java.util.stream.ReferencePipeline$Head@50129b8f"]

Is there a way to iterate over this without running out of memory? The suggestion on SO are not really helpful. The files is ~1G.

like image 292
Istvan Avatar asked Jun 13 '26 16:06

Istvan


2 Answers

Do the same thing you would do in java. Call interop methods on it, like (.map stream ...), or whatever else, and collect it to a list at the end, or reduce it, or whatever you need to do.

Clojure functions don't implement the interfaces that a stream expects, such as java.util.function.Function, so you'll need to use reify instead of a simple fn:

(.map stream (reify java.util.function.Function
               (apply [this x]
                 (foo x))))
like image 54
amalloy Avatar answered Jun 15 '26 05:06

amalloy


You may want to look into a library called ike.cljj for some interop niceties.

like image 41
dpassen Avatar answered Jun 15 '26 05:06

dpassen