Extra transducers and reducing fns for Clojure(script)
Find a file
2015-09-03 20:51:37 +02:00
src/net/cgrand fix bug induced by change in api (pair fn instead of update fn) 2015-09-03 20:51:37 +02:00
project.clj init 2015-09-03 12:39:22 +02:00
README.md fix bug induced by change in api (pair fn instead of update fn) 2015-09-03 20:51:37 +02:00

xforms

More transducers and reducing functions for Clojure!

Usage

Add this dependency to your project:

[net.cgrand/xforms "0.1.0-SNAPSHOT"]
=> (require '[net.cgrand.xforms :as x])

str and str! are two reducing functions to build Strings and StringBuilders in linear time.

=> (quick-bench (reduce str (range 256)))
             Execution time mean : 58,714946 µs
=> (quick-bench (reduce x/str (range 256)))
             Execution time mean : 11,609631 µs

by-key and reduce are two new transducers. Here is an example usage:

;; reimplementing group-by
(defn my-group-by [kfn coll]
  (into {} (x/by-key kfn (x/reduce conj)) coll))

;; let's go transient!
(defn my-group-by [kfn coll]
  (into {} (x/by-key kfn (x/reduce (completing conj! persistent!))) coll))

=> (quick-bench (group-by odd? (range 256)))
             Execution time mean : 29,356531 µs
=> (quick-bench (my-group-by odd? (range 256)))
             Execution time mean : 20,604297 µs

avg is a reducing fn to compute the arithmetic mean. juxt is used to compute several reducing fns at once.

=> (into {} (x/by-key odd? (x/reduce (x/juxt + x/avg))) (range 256))
{false [16256 127], true [16384 128]}

License

Copyright © 2015 Christophe Grand

Distributed under the Eclipse Public License either version 1.0 or (at your option) any later version.