Skip to content

Instantly share code, notes, and snippets.

@whostolebenfrog
Forked from cgrand/fcoll.clj
Created June 27, 2013 13:57
Show Gist options
  • Save whostolebenfrog/5876622 to your computer and use it in GitHub Desktop.
Save whostolebenfrog/5876622 to your computer and use it in GitHub Desktop.
;; Clojure 1.5.1
=> 1
1
=> (require '[clojure.core.reducers :as r])
nil
=> (use 'clojure.repl)
nil
=> (doc r/fold)
-------------------------
clojure.core.reducers/fold
([reducef coll] [combinef reducef coll] [n combinef reducef coll])
Reduces a collection using a (potentially parallel) reduce-combine
strategy. The collection is partitioned into groups of approximately
n (default 512), each of which is reduced with reducef (with a seed
value obtained by calling (combinef) with no arguments). The results
of these reductions are then reduced with combinef (default
reducef). combinef must be associative, and, when called with no
arguments, (combinef) must produce its identity element. These
operations may be performed in parallel, but the results will
preserve order.
nil
=> (defn freqs [coll]
(reduce (fn [fs x]
(assoc fs x (inc (fs x 0))))
{} coll))
#'user/freqs
=> (freqs "hello world")
{\d 1, \r 1, \w 1, \space 1, \o 2, \l 3, \e 1, \h 1}
=> [(freqs "hello ") (freqs "world")]
[{\space 1, \o 1, \l 2, \e 1, \h 1} {\d 1, \l 1, \r 1, \o 1, \w 1}]
=> (merge-with + (freqs "hello ") (freqs "world"))
{\w 1, \r 1, \d 1, \space 1, \o 2, \l 3, \e 1, \h 1}
=> (defn freqs [coll]
(r/fold
(fn
([] {})
([a b] (merge-with + a b)))
(fn [fs x]
(assoc fs x (inc (fs x 0))))
{} coll))
#'user/freqs
=> (freqs "hello world")
ArityException Wrong number of args (0) passed to: user$freqs$fn clojure.lang.AFn.throwArity (AFn.java:437)
=> (doc r/fold)
-------------------------
clojure.core.reducers/fold
([reducef coll] [combinef reducef coll] [n combinef reducef coll])
Reduces a collection using a (potentially parallel) reduce-combine
strategy. The collection is partitioned into groups of approximately
n (default 512), each of which is reduced with reducef (with a seed
value obtained by calling (combinef) with no arguments). The results
of these reductions are then reduced with combinef (default
reducef). combinef must be associative, and, when called with no
arguments, (combinef) must produce its identity element. These
operations may be performed in parallel, but the results will
preserve order.
nil
=> (defn freqs [coll]
(r/fold
(fn
([] {})
([a b] (merge-with + a b)))
(fn [fs x]
(assoc fs x (inc (fs x 0))))
coll))
#'user/freqs
=> (freqs "hello world")
{\d 1, \r 1, \w 1, \space 1, \o 2, \l 3, \e 1, \h 1}
=> (defn ffreqs [coll]
(r/fold
(fn
([] {})
([a b] (merge-with + a b)))
(fn [fs x]
(assoc fs x (inc (fs x 0))))
coll))
#'user/ffreqs
=> (defn freqs [coll]
(reduce (fn [fs x]
(assoc fs x (inc (fs x 0))))
{} coll))
#'user/freqs
=> (def v (vec (take 1e6 (cycle "hello nokia"))))
#'user/v
=> (freqs v)
{\a 90909, \i 90909, \k 90909, \n 90909, \space 90909, \o 181818, \l 181818, \e 90909, \h 90910}
=> (time (dotimes [_ 1000] (freqs v)))
;; Interrupting...
Expression was interrupted: null
=> (time (dotimes [_ 10] (freqs v)))
"Elapsed time: 1681.146 msecs"
nil
=> (dotimes [_ 5] (time (dotimes [_ 5] (freqs v))))
"Elapsed time: 883.888 msecs"
"Elapsed time: 863.669 msecs"
"Elapsed time: 841.742 msecs"
"Elapsed time: 838.673 msecs"
"Elapsed time: 830.127 msecs"
nil
=> (dotimes [_ 5] (time (dotimes [_ 5] (ffreqs v))))
"Elapsed time: 613.928 msecs"
"Elapsed time: 472.877 msecs"
"Elapsed time: 438.319 msecs"
"Elapsed time: 442.998 msecs"
"Elapsed time: 433.619 msecs"
nil
=> (dotimes [_ 5] (time (dotimes [_ 5] (frequencies v))))
"Elapsed time: 770.664 msecs"
"Elapsed time: 738.769 msecs"
"Elapsed time: 736.916 msecs"
"Elapsed time: 740.062 msecs"
"Elapsed time: 743.046 msecs"
nil
=> ; [1 2 3]
=> (defn rlist
([] (fn [f init] init))
([x & xs]
(let [rrest (apply rlist xs)]
(fn [f init]
(rrest f (f init x))))))
#'user/rlist
=> ((rlist) + 0)
0
=> ((rlist 1) + 0)
1
=> ((rlist 1 2 3) + 0)
6
=> ((rlist 1 2 3) str "")
"123"
=> (def empty (fn [f init] init))
WARNING: empty already refers to: #'clojure.core/empty in namespace: user, being replaced by: #'user/empty
#'user/empty
=> (defn fcons [x ftail]
(fn [f init]
(let [acc (f init x)]
(ftail f acc))))
#'user/fcons
=> (->> empty (fcons 3) (fcons 2) (fcons 1))
#<user$fcons$fn__2638 user$fcons$fn__2638@43a65198>
=> (*1 conj [])
[1 2 3]
=> (defn fnth [fcoll n]
(:just
(fcoll (fn [acc x]
(if-let [n (:to-go acc)]
(if (pos? n)
{:to-go (dec n)}
{:just x})
acc)) {:to-go n})))
#'user/fnth
=> (def l123(->> empty (fcons 3) (fcons 2) (fcons 1)))
#'user/l123
=> (fnth l123 0)
1
=> (fnth l123 1)
2
=> (fnth l123 2)
3
=> (fnth l123 3)
nil
=> (defn fnth [fcoll n]
(let [found (atom nil)]
(fcoll (fn [acc x]
(if (zero? n)
(reset! found x)
(dec n))) n)
@found))
#'user/fnth
=> (fnth l123 0)
3
=> (defn fnth [fcoll n]
(let [found (atom nil)]
(fcoll (fn [acc x]
(when (zero? n)
(reset! found x))
(dec n)) n)
@found))
#'user/fnth
=> (fnth l123 0)
3
=> (defn fnth [fcoll n]
(let [found (atom nil)]
(fcoll (fn [n x]
(when (zero? n)
(reset! found x))
(dec n)) n)
@found))
#'user/fnth
=> (fnth l123 0)
1
=> (fnth l123 1)
2
=> (fnth l123 2)
3
=> (fnth l123 3)
nil
=> (defn fmap [f fcoll]
(fn [g init]
(fcoll (fn [acc x]
(g acc (f x))) init)))
#'user/fmap
=> ((fmap inc l123))
ArityException Wrong number of args (0) passed to: user$fmap$fn clojure.lang.AFn.throwArity (AFn.java:437)
=> (fmap inc l123)
#<user$fmap$fn__3158 user$fmap$fn__3158@63d48a65>
=> (*1 conj [])
[2 3 4]
=> (defn fmap [f fcoll]
(fn [g init]
(fcoll (fn [acc x]
(g acc (f x))) init)))
#'user/fmap
=> (defn smap [f coll]
(if-let [[x & xs] (seq coll)]
(cons (f x) (smap f xs))
nil))
#'user/smap
=> (defn reducer [xf fcoll]
(fn [g init]
(fcoll (xf g) init)))
#'user/reducer
=> (defn fmap [f coll]
(reducer (fn [g]
(fn [acc x]
(g acc (f x))))
coll))
#'user/fmap
=> (fmap inc l123)
#<user$reducer$fn__3494 user$reducer$fn__3494@6e704a0>
=> (*1 conj [])
[2 3 4]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment