-
Notifications
You must be signed in to change notification settings - Fork 39
/
simple.clj
29 lines (27 loc) · 1.06 KB
/
simple.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
(ns tesser.simple
"Drop-in replacement for `reducers/fold` and `core/reduce`, where order
doesn't matter."
(:refer-clojure :exclude [reduce])
(:require [tesser.core :as t]
[tesser.utils :refer :all]))
(defn fold
"Folds over a collection using a parallel reduce-combine strategy. Collection
is partitioned into chunks of approximately `n` (default 512), and folded
over with `t/tesser`. Unlike reducers `fold`, this function does not preserve
order."
([reducef coll]
(fold reducef reducef coll))
([combinef reducef coll]
(fold 512 combinef reducef coll))
([n combinef reducef coll]
(t/tesser (t/chunk n coll)
(t/fold {:reducer reducef
:combiner combinef}))))
(defn reduce
"Like `clojure.core/reduce, but parallel, using `t/tesser` over 512-element
chunks. Unlike `core/reduce`, does not preserve order, init must be an
identity element, f must be associative, etc."
([f init coll]
(t/tesser (t/chunk 512 coll)
(t/fold {:reducer f
:identity (constantly init)}))))