xio/sh produces reducible collections

This commit is contained in:
Christophe Grand 2018-04-16 23:07:18 +02:00
parent 00e19651ed
commit f2165ba932
3 changed files with 88 additions and 78 deletions

View file

@ -15,7 +15,7 @@ In `net.cgrand.xforms`:
* aggregators: `reduce`, `into`, `without`, `transjuxt`, `last`, `count`, `avg`, `sd`, `min`, `minimum`, `max`, `maximum`, `str`
In `net.cgrand.xforms.io`:
* `sh` to use any process as a transducer
* `sh` to use any process as a reducible collection (of stdout lines) or as a transducers (input as stdin lines, stdout lines as output).
*Reducing functions*

View file

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

View file

@ -101,7 +101,8 @@
(cond (map? x) x (string? x) {:enc x} (keyword? x) {:mode x})))
(defn sh
"Transducer. Spawns a process (program cmd with optional arguments arg1 ... argN) and pipes data through it.
"Transducer or reducible view (in this case assumes empty stdin).
Spawns a process (program cmd with optional arguments arg1 ... argN) and pipes data through it.
Options may be:
* :env, an environment variables map, it will be merged with clojure.java.shell/*sh-env* and JVM environment (in decreasing precedence order),
* :dir, the current dir (defaults to clojure.java.shell/*sh-dir* or JVM current dir),
@ -113,7 +114,16 @@
In :text mode, values are strings."
{:arglists '([cmd arg1 ... argN & opts])}
[& args]
(fn [rf]
(reify
clojure.lang.IReduce
(reduce [self rf]
(reduce rf (eduction self nil))) ; quick way to handle no init
(reduce [self rf init]
(let [xf (self rf)]
(xf init)))
clojure.lang.Fn
clojure.lang.IFn
(invoke [_ rf]
(let [[cmd [& {:as opts :keys [env in out dir] :or {dir sh/*sh-dir*}}]] (split-with string? args)
env (into (or sh/*sh-env* {}) env)
env (into {} (for [[k v] env] [(name k) (str v)]))
@ -188,4 +198,4 @@
(write! x))
acc
(catch java.io.IOException e
(ensure-reduced acc)))))))))
(ensure-reduced acc))))))))))