A bridge from I/O to transducers, and back!
This library provides two functions to bridge I/O with transducers. decoder
creates a function
that can produce data from input for use with transducers and encoder
makes a function that takes
data, possibly through a transducer, and writes it to output.
decoder
is analogous to a source collection in a transduction pipeline. encoder
is analogous to
into
, it is a transduction context that takes a transducer and an input collection, but writes to
output instead of building a collection.
decoder
takes four arguments: open, decode, done?, close. It returns a function that takes a
source and returns a reducible. This reducible calls open with the source, then decode with that
result, and eventually, when done? returns true, the reduction completes and it calls close.
For example, if you want to decode Clojure data:
(def clojure-decoder
(decoder (comp PushbackReader/new io/reader)
#(read % false ::eof)
#{::eof}
AutoCloseable/.close))
(into [] (clojure-decoder (StringReader. "1 2 3")))
;;=> [1 2 3]
encoder
takes three arguments: open, encode!, close. It returns a function that acts like into
.
Like into
it has two arities. The first takes a sink and a source and reduces the source into the
sink. It calls open with the sink, then encode! with that result and each value from the source,
and eventually, when it exhausts the source, the reduction completes and it calls close.
The second arity takes a sink, a transduction, and a source. The process is the same as above, but the data flows from the source, through the transduction, then into the sink.
For example, if you want to encode Clojure data:
(def clojure-encoder
(encoder io/writer
(fn [^Writer out value] (.write out (prn-str value)))
AutoCloseable/.close))
(clojure-encoder "/tmp/foo.clj" (map inc) [1 2 3])
(slurp "/tmp/foo.clj")
;;=> "2\n3\n4\n"
Copyright © technosophist
This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0.
Can you improve this documentation?Edit on GitHub
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close