Back
reductions (clj)
(source)function
(reductions f coll)
(reductions f init coll)
Returns a lazy seq of the intermediate values of the reduction (as
per reduce) of coll by f, starting with init.
Examples
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))))
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)])
ReactiveX/RxClojure
(ns rx.lang.clojure.core-test
(:require [rx.lang.clojure.core :as rx]
[rx.lang.clojure.blocking :as b]
[rx.lang.clojure.future :as f]
[clojure.test :refer [deftest is testing are]]))
(deftest test-reductions
(is (= (into [] (reductions + 0 (range 4)))
(b/into [] (rx/reductions + 0 (rx/seq->o (range 4)))))))
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 [])))))
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}})
findmyway/plotly-clj
;; @@
(ns horizontal-bar
(:require [clojure.core.matrix :as m]
[clojure.core.matrix.dataset :as d]
[clojure.core.matrix.random :as rnd]
[clojure.data.csv :as csv])
(:use [plotly-clj.core] :reload-all))
;; @@
;; =>
;;; {"type":"html","content":"<span class='clj-nil'>nil</span>","value":"nil"}
;; <=
;; @@
(let [top-labels ["Strongly<br>agree" "Agree" "Neutral" "Disagree" "Strongly<br>disagree"]
colors ["rgba(38 24 74 0.8)" "rgba(71 58 131 0.8)" "rgba(122 120 168 0.8)"
"rgba(164 163 204 0.85)" "rgba(190 192 213 1)"]
x-data [[21 30 21 16 12] [24 31 19 15 11] [27 26 23 11 13] [29 24 15 18 14]]
y-data ["The course was effectively<br>organized"
"The course developed my<br>abilities and skills for<br>the subject"
"The course developed my<br>ability to think critically about<br>the subject"
"I would recommend this<br>course to a friend"]
get-mid (fn [xs]
(let [s (butlast (reductions + 0 xs))
halves (m/div xs 2)]
(m/add s halves)))
xs-mid (map get-mid x-data)]
(-> (plotly)
(plot-seq
(for [[x c] (map vector (m/transpose x-data) colors)]
#(add-bar %
:x x
:y y-data
:orientation "h"
:marker {:color c
:line {:color "rgb(248, 248, 249)" :width 1}})))
(set-layout :xaxis {:showgrid false
:showline false
:showticklabels false
:zeroline false
:domain [0.15 1]}
:yaxis {:showgrid false
:showline false
:showticklabels false
:zeroline false}
:barmode "stack"
:showlegend false
:paper_bgcolor "rgb(248, 248, 255)"
:plot_bgcolor "rgb(248, 248, 255)"
:margin {:l 120 :r 10 :t 120 :b 80})
(add-annotations
(for [[x y] (map vector x-data y-data)]
{:xref "paper" :yref "y" :x 0.14 :y y :xanchor "right" :text y :showarrow false :align "right"}))
(add-annotations
(for [idy (range (count y-data)) idx (range (count (first xs-mid)))]
{:xref "x"
:yref "y"
:x (nth (nth xs-mid idy) idx)
:y (nth y-data idy)
:text (str (nth (nth x-data idy) idx) "%")
:showarrow false
:font {:color "rgb(248, 248, 255)"
:family "Arial"
:size 14}}))
(add-annotations
(for [[x label] (map vector (last xs-mid) top-labels)]
{:xref "x" :yref "paper"
:x x :y 1.1 :text label :showarrow false}))
(plot "Color Palette for Bar Chart" :fileopt "overwrite")
embed-url))
;; @@
;; =>
;;; {"type":"html","content":"<iframe height=\"600\" src=\"//plot.ly/~findmyway/78.embed\" width=\"800\"></iframe>","value":"pr'ed value"}
;; <=