-
-
Save tomjack/543a721f0f44cf48459d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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