Make 3+-arg arities of edn-out and lines-out to be transducing contexts.

This commit is contained in:
Christophe Grand 2017-10-05 10:31:53 +02:00
parent ea7a3e699d
commit 89d384ce74
3 changed files with 20 additions and 7 deletions

View file

@ -17,7 +17,10 @@ More transducers and reducing functions for Clojure(script)!
* in `net.cgrand.xforms.rfs`: `min`, `minimum`, `max`, `maximum`, `str`, `str!`, `avg`, `sd`, `last` and `some`. * in `net.cgrand.xforms.rfs`: `min`, `minimum`, `max`, `maximum`, `str`, `str!`, `avg`, `sd`, `last` and `some`.
* in `net.cgrand.xforms.io`: `line-out` and `edn-out`. * in `net.cgrand.xforms.io`: `line-out` and `edn-out`.
*Transducing contexts*: `transjuxt` (for performing several transductions in a single pass), `into`, `count` and `some`. *Transducing contexts*:
* in `net.cgrand.xforms`: `transjuxt` (for performing several transductions in a single pass), `into`, `count` and `some`.
* in `net.cgrand.xforms.io`: `line-out` (3+ args) and `edn-out` (3+ args).
*Reducible views* (in `net.cgrand.xforms.io`): `lines-in` and `edn-in`. *Reducible views* (in `net.cgrand.xforms.io`): `lines-in` and `edn-in`.
@ -26,7 +29,7 @@ More transducers and reducing functions for Clojure(script)!
Add this dependency to your project: Add this dependency to your project:
```clj ```clj
[net.cgrand /xforms "0.10.1"] [net.cgrand /xforms "0.10.2"]
``` ```
```clj ```clj

View file

@ -1,4 +1,4 @@
(defproject net.cgrand/xforms "0.10.1" (defproject net.cgrand/xforms "0.10.2"
:description "Extra transducers for Clojure" :description "Extra transducers for Clojure"
:url "https://github.com/cgrand/xforms" :url "https://github.com/cgrand/xforms"
:license {:name "Eclipse Public License" :license {:name "Eclipse Public License"

View file

@ -33,13 +33,18 @@
state)))))))) state))))))))
(defn lines-out (defn lines-out
"Reducing function that writes values serialized to its accumulator (a java.io.Writer). "1-2 args: reducing function that writes values serialized to its accumulator (a java.io.Writer).
3+ args: transducing context that writes transformed values to the specified output. The output is
coerced to a Writer by passing out and opts to clojure.java.io/writer. The output is automatically closed.
Returns the writer." Returns the writer."
([w] w) ([w] w)
([^java.io.Writer w line] ([^java.io.Writer w line]
(doto w (doto w
(.write (str line)) (.write (str line))
(.newLine)))) (.newLine)))
([out xform coll & opts]
(with-open [w (apply io/writer out opts)]
(transduce xform lines-out w coll))))
(defn edn-in (defn edn-in
"Returns a reducible view over the provided input. "Returns a reducible view over the provided input.
@ -72,7 +77,9 @@
(recur state)))))))))))) (recur state))))))))))))
(defn edn-out (defn edn-out
"Reducing function that writes values serialized as EDN to its accumulator (a java.io.Writer). "1-2 args: reducing function that writes values serialized as EDN to its accumulator (a java.io.Writer).
3+ args: transducing context that writes transformed values to the specified output. The output is
coerced to a Writer by passing out and opts to clojure.java.io/writer. The output is automatically closed.
Returns the writer." Returns the writer."
([w] w) ([w] w)
([^java.io.Writer w x] ([^java.io.Writer w x]
@ -83,4 +90,7 @@
*print-meta* false *print-meta* false
*print-readably* true] *print-readably* true]
(prn x) (prn x)
w))) w))
([out xform coll & opts]
(with-open [w (apply io/writer out opts)]
(transduce xform edn-out w coll))))