Skip to content

Instantly share code, notes, and snippets.

@tomjack
Created August 28, 2012 05:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomjack/543a721f0f44cf48459d to your computer and use it in GitHub Desktop.
Save tomjack/543a721f0f44cf48459d to your computer and use it in GitHub Desktop.
(ns scratch
(:require [clojure.core.reducers :as r]
[clojure.core.protocols :as rp])
(:refer-clojure :exclude [reductions]))
(defn reductions
;; curried, identity init
([reducef]
(fn [coll] (reductions reducef coll)))
;; curried, arbitrary init
;; ...
;; uncurried, identity init
([reducef coll]
(reductions reducef (reducef) coll))
;; uncurried, arbitrary init
([reducef val coll]
(reify
rp/CollReduce
(coll-reduce [this f1]
(rp/coll-reduce this f1 (f1)))
(coll-reduce [_ f1 init]
(let [acc (atom init)]
(rp/coll-reduce coll
(fn [ret v]
(let [vret (reducef ret v)]
(swap! acc f1 vret)
vret))
val)
@acc)))))
(comment
(= (into [] (reductions + [1 2 3 4]))
[1 3 6 10])
true
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment