Back
fold (clj)
(source)function
(fold reducef coll)
(fold combinef reducef coll)
(fold 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.
Examples
clojure
(ns clojure.test-clojure.reducers
(:require [clojure.core.reducers :as r]
[clojure.test.generative :refer (defspec)]
[clojure.data.generators :as gen])
(:use clojure.test))
(deftest test-nil
(is (= {:k :v} (reduce-kv assoc {:k :v} nil)))
(is (= 0 (r/fold + nil))))
(deftest test-fold-runtime-exception
(is (thrown? IndexOutOfBoundsException
(let [test-map-count 1234
k-fail (rand-int test-map-count)]
(r/fold (fn ([])
([ret [k v]])
([ret k v] (when (= k k-fail)
(throw (IndexOutOfBoundsException.)))))
(zipmap (range test-map-count) (repeat :dummy)))))))
clojure/clojurescript
(ns cljs.reducers-test
(:require [cljs.test :refer-macros [deftest is]]
[clojure.core.reducers :as r]))
(deftest test-builtin-impls
(is (= 0 (r/fold + nil)))
(is (= [1 2 3 4] (seq (r/reduce r/append! (r/cat) [1 2 3 4]))))
(is (= 10 (r/reduce + (array 1 2 3 4))))
(is (= 11 (r/reduce + 1 (array 1 2 3 4))))
(is (= 10 (r/reduce + (list 1 2 3 4))))
(is (= 11 (r/reduce + 1 (list 1 2 3 4))))
(is (= (r/fold + + [1 2 3])
(r/fold + [1 2 3])
(r/reduce + [1 2 3])
6))
(is (= (r/fold + + (vec (range 2048)))
(r/reduce + (vec (range 2048)))))
(letfn [(f [[ks vs] k v]
[(conj ks k) (conj vs v)])
(g ([] [#{} #{}])
([[ks1 vs1] [ks2 vs2]]
[(into ks1 ks2) (into vs1 vs2)]))]
(is (= (r/reduce f (g) {:a 1 :b 2 :c 3})
(r/fold g f {:a 1 :b 2 :c 3})
[#{:a :b :c} #{1 2 3}]))
(let [m (into {} (for [x (range 2048)] [x (- x)]))]
(is (= (r/reduce f (g) m) (r/fold g f m)))))
;; CLJS-792
(is (= (into [] (r/map identity {})) [])))
jonase/eastwood
(ns testcases.unusednss3
(:require [clojure.core.protocols :as protocols]
[clojure.core.reducers :as reducers]
[clojure.data :as data]
[clojure.java.io :as io]
[clojure.reflect :as reflect]))
(extend-protocol reducers/CollFold
String
(coll-fold [coll n combinef reducef] nil))
seancorfield/next-jdbc
(ns next.jdbc-test
"Basic tests for the primary API of `next.jdbc`."
(:require [clojure.core.reducers :as r]
[clojure.string :as str]
[clojure.test :refer [deftest is testing use-fixtures]]
[next.jdbc :as jdbc]
[next.jdbc.connection :as c]
[next.jdbc.test-fixtures
:refer [with-test-db db ds column
default-options stored-proc?
derby? hsqldb? jtds? mssql? mysql? postgres? sqlite?]]
[next.jdbc.prepare :as prep]
[next.jdbc.result-set :as rs]
[next.jdbc.specs :as specs]
[next.jdbc.types :as types])
(:import (com.zaxxer.hikari HikariDataSource)
(com.mchange.v2.c3p0 ComboPooledDataSource PooledDataSource)
(java.sql ResultSet ResultSetMetaData)))
;; around each test because of the folding tests using 1,000 rows
(use-fixtures :each with-test-db)
(deftest folding-test
(jdbc/execute-one! (ds) ["delete from fruit"])
(with-open [con (jdbc/get-connection (ds))
ps (jdbc/prepare con ["insert into fruit(name) values (?)"])]
(jdbc/execute-batch! ps (mapv #(vector (str "Fruit-" %)) (range 1 1001))))
(testing "foldable result set"
(testing "from a Connection"
(let [result
(with-open [con (jdbc/get-connection (ds))]
(r/foldcat
(r/map (column :FRUIT/NAME)
(jdbc/plan con ["select * from fruit order by id"]
(default-options)))))]
(is (= 1000 (count result)))
(is (= "Fruit-1" (first result)))
(is (= "Fruit-1000" (last result)))))
(testing "from a DataSource"
(doseq [n [2 3 4 5 100 300 500 700 900 1000 1100]]
(testing (str "folding with n = " n)
(let [result
(try
(r/fold n r/cat r/append!
(r/map (column :FRUIT/NAME)
(jdbc/plan (ds) ["select * from fruit order by id"]
(default-options))))
(catch java.util.concurrent.RejectedExecutionException _
[]))]
(is (= 1000 (count result)))
(is (= "Fruit-1" (first result)))
(is (= "Fruit-1000" (last result)))))))
(testing "from a PreparedStatement"
(let [result
(with-open [con (jdbc/get-connection (ds))
stmt (jdbc/prepare con
["select * from fruit order by id"]
(default-options))]
(r/foldcat
(r/map (column :FRUIT/NAME)
(jdbc/plan stmt nil (default-options)))))]
(is (= 1000 (count result)))
(is (= "Fruit-1" (first result)))
(is (= "Fruit-1000" (last result)))))
(testing "from a Statement"
(let [result
(with-open [con (jdbc/get-connection (ds))
stmt (prep/statement con (default-options))]
(r/foldcat
(r/map (column :FRUIT/NAME)
(jdbc/plan stmt ["select * from fruit order by id"]
(default-options)))))]
(is (= 1000 (count result)))
(is (= "Fruit-1" (first result)))
(is (= "Fruit-1000" (last result)))))))
dco-dev/interval-tree
(ns com.dean.interval-tree.tree.interval-set
(:require [clojure.core.reducers :as r :refer [coll-fold]]
[clojure.set]
[com.dean.interval-tree.tree.interval :as interval]
[com.dean.interval-tree.tree.node :as node]
[com.dean.interval-tree.tree.order :as order]
[com.dean.interval-tree.tree.protocol :as proto]
[com.dean.interval-tree.tree.root]
[com.dean.interval-tree.tree.tree :as tree])
(:import [clojure.lang RT]
[com.dean.interval_tree.tree.protocol PExtensibleSet]
[com.dean.interval_tree.tree.root INodeCollection
IBalancedCollection
IOrderedCollection
IIntervalCollection]))
clojure.core.reducers.CollFold
(coll-fold [this n combinef reducef]
(with-interval-set this
(tree/node-chunked-fold n root combinef
(fn [acc node] (reducef acc (node/-k node)))))))
reborg/clojure-essential-reference
(require '[criterium.core :refer [quick-bench]])
(require '[clojure.core.reducers :as r])
(let [not-so-big-data (into [] (range 1000))]
(quick-bench (r/fold + not-so-big-data)))
;; Execution time mean : 32.683242 µs