Public Vars

Back

eduction (clj)

(source)

function

(eduction xform* coll)
Returns a reducible/iterable application of the transducers to the items in coll. Transducers are applied in order as if combined with comp. Note that these applications will be performed every time reduce/iterator is called.

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-sorted-maps
  (let [m (into (sorted-map)
                '{1 a, 2 b, 3 c, 4 d})]
    (is (= "1a2b3c4d" (reduce-kv str "" m))
        "Sorted maps should reduce-kv in sorted order")
    (is (= 1 (reduce-kv (fn [acc k v]
                          (reduced (+ acc k)))
                        0 m))
        "Sorted maps should stop reduction when asked")))
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)))

(deftest plan-misuse
  (let [s (pr-str (jdbc/plan (ds) ["select * from fruit"]))]
    (is (re-find #"missing reduction" s)))
  (let [s (pr-str (into [] (jdbc/plan (ds) ["select * from fruit"])))]
    (is (re-find #"missing `map` or `reduce`" s)))
  ;; this may succeed or not, depending on how the driver handles things
  ;; most drivers will error because the ResultSet was closed before pr-str
  ;; is invoked (which will attempt to print each row)
  (let [s (pr-str (into [] (take 3) (jdbc/plan (ds) ["select * from fruit"]
                                               (default-options))))]
    (is (or (re-find #"missing `map` or `reduce`" s)
            (re-find #"(?i)^\[#:fruit\{.*:id.*\}\]$" s))))
  (is (every? #(re-find #"(?i)^#:fruit\{.*:id.*\}$" %)
              (into [] (map str) (jdbc/plan (ds) ["select * from fruit"]
                                            (default-options)))))
  (is (every? #(re-find #"(?i)^#:fruit\{.*:id.*\}$" %)
              (into [] (map pr-str) (jdbc/plan (ds) ["select * from fruit"]
                                               (default-options)))))
  (is (thrown? IllegalArgumentException
               (doall (take 3 (jdbc/plan (ds) ["select * from fruit"]))))))
typedclojure/typedclojure
(ns ^:no-doc typed.ann.clojure
  "Type annotations for the base Clojure distribution."
  #?(:cljs (:require-macros [typed.ann-macros.clojure :as macros]))
  (:require [clojure.core :as cc]
            [typed.clojure :as t]
            #?(:clj [typed.ann-macros.clojure :as macros])
            #?(:clj typed.ann.clojure.jvm) ;; jvm annotations
            #?(:clj clojure.core.typed))
  #?(:clj
     (:import (clojure.lang PersistentHashSet PersistentList
                            APersistentMap #_IPersistentCollection
                            #_ITransientSet
                            IRef)
              (java.util Comparator Collection))))

#?@(:clj [
; (t/All [x ...] [(t/Transducer x y) (t/Seqable x) :-> (Iterable y)])
cc/eduction (t/All [x y z a b] (t/IFn [(t/Seqable x) :-> (Iterable x)]
                                      [(t/Transducer x y) (t/Seqable x) :-> (Iterable y)]
                                      [(t/Transducer x y) (t/Transducer y z) (t/Seqable x) :-> (Iterable z)]
                                      [(t/Transducer x y) (t/Transducer y z) (t/Transducer z a) (t/Seqable x) :-> (Iterable a)]
                                      [(t/Transducer x y) (t/Transducer y z) (t/Transducer z a) (t/Transducer a b) (t/Seqable x) :-> (Iterable b)]))
]) 

cc/reduce (t/All [a c] (t/IFn 
                         ;Without accumulator
                         ; default
                         ; (reduce + my-coll)
                         [[a a :-> (t/U (t/Reduced a) a)] (t/NonEmptySeqable a) :-> a]
                         [(t/IFn [a a :-> (t/U (t/Reduced a) a)] [:-> a]) (t/Seqable a) :-> a]
                         ; default
                         ; (reduce + 3 my-coll)
                         ; (reduce (fn [a b] a) (reduced 1) nil) 
                         ; ;=> (reduced 1)
                         [[a c :-> (t/U (t/Reduced a) a)] a (t/Seqable c) :-> a]))
cc/transduce (t/All [a b c] (t/IFn [(t/Transducer a a) (t/Reducer a a) (t/Seqable a) :-> a]
                                   [(t/Transducer b c) (t/IFn [c :-> c] [c a :-> (t/U c (t/Reduced c))]) a (t/Seqable b) :-> a]))
cc/reduce-kv (t/All [a k v] [[a k v :-> (t/U (t/Reduced a) a)] a (t/Option (t/Associative k v)) :-> a])
cc/reductions (t/All [a b] (t/IFn [[a a :-> (t/U (t/Reduced a) a)] (t/NonEmptySeqable a) :-> (t/NonEmptyASeq a)]
                                  [(t/IFn [:-> a] [a a :-> (t/U (t/Reduced a) a)]) (t/Seqable a) :-> (t/NonEmptyASeq a)]
                                  [[a b :-> (t/U (t/Reduced a) a)] a (t/Seqable b) :-> (t/NonEmptyASeq a)]))
cc/reduced (t/All [x] [x :-> (t/Reduced x)])
cc/unreduced (t/All [x] (t/IFn [(t/Reduced x) :-> x]
                               [(t/U x (t/Reduced x)) :-> x]))
cc/ensure-reduced (t/All [x] [(t/U x (t/Reduced x)) :-> (t/Reduced x)])
cc/completing (t/All [a b] [(t/IFn [:-> b] [b a :-> (t/U b (t/Reduced b))])
                            [b :-> b]
                            :-> (t/Reducer a b)])
damballa/parkour
(ns parkour.reducers-test
  (:require [clojure.core.reducers :as r]
            [parkour (reducers :as pr)]
            [clojure.test :refer :all]))

(deftest test-reductions
  (is (= [[] [0] [0 1] [0 1 2] [0 1 2 3]]
         (->> (range 4)
              (pr/reductions conj [])
              (into [])))))
reborg/parallel
(require '[parallel.core :as p])
(require '[criterium.core :refer [bench quick-bench]])
(require '[clojure.core.reducers :as r])

(quick-bench (frequencies (eduction (keep :samplevalue) (map int) small-overlapping)))
;; 238 ms
(quick-bench (p/frequencies small-overlapping (keep :samplevalue) (map int)))
;; 91 ms
(binding [p/*mutable* true] (quick-bench (p/frequencies small-overlapping (keep :samplevalue) (map int))))
;; 50 ms
bsless/more.async
(ns more.async.dataflow.node
  (:require
   [clojure.spec.alpha :as s]
   [clojure.core.async :as a]
   [more.async :as ma]
   [clojure.data]))

(s/def ::reductions
  (s/keys :req [::from ::to ::rf ::init-fn]
          :opt [::async?]))

(defmethod -type ::reductions [_]
  (s/keys :req [::name ::type ::reductions]))

(defmethod -compile ::reductions
  [{{from ::from
     to ::to
     rf ::rf
     init ::rf
     async? ::async?} ::reductions} env]
  (let [from (env from)
        to (env to)]
    (if async?
      (ma/reductions! rf init from to)
      (a/thread
        (ma/reductions!! rf init from to)))))

(defmethod ports ::reductions
  [{{to ::to from ::from} ::reductions}]
  #{{::name from ::direction ::in}
    {::name to ::direction ::out}})