Back
reduce-kv (clj)
(source)function
(reduce-kv f init coll)
Reduces an associative collection. f should be a function of 3
arguments. Returns the result of applying f to init, the first key
and the first value in coll, then applying f to that result and the
2nd key and value, etc. If coll contains no entries, returns init
and f is not called. Note that reduce-kv is supported on vectors,
where the keys will be the ordinals.
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")))
(deftest test-nil
(is (= {:k :v} (reduce-kv assoc {:k :v} nil)))
(is (= 0 (r/fold + nil))))
(defn reduced-at-probe
[m p]
(reduce-kv (fn [_ k v] (when (== p k) (reduced :foo))) nil m))
clojure/core.typed
(ns clojure.core.typed.test.cljs-core
(:require [cljs.core.typed :as t]
[cljs.core :as core]
[clojure.core.typed.test.cljs-utils :refer [is-tc-e tc-e]]
[clojure.test :refer :all]))
(add-test reduce-kv
[(t/fn [a :- t/CLJSInteger k :- Keyword v :- t/CLJSInteger] :- t/CLJSInteger (+ a v))
0 {:a 1 :b 2 :c 3}] t/CLJSInteger)
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)])
videlalvaro/clochure
[ns clojure.test-clojure.reducers
[:require (clojure.core.reducers :as r)]
[: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"]]]
[deftest test-nil
[is [= {:k :v} [reduce-kv assoc {:k :v} nil]]]
[is [= 0 [r/fold + nil]]]]
fluree/ledger
(ns fluree.db.ledger.general.todo-permissions
(:require [clojure.test :refer :all]
[clojure.core.async :as async]
[clojure.string :as str]
[fluree.db.test-helpers :as test]
[fluree.db.ledger.docs.getting-started.basic-schema :as basic]
[fluree.db.api :as fdb])
(:import (clojure.lang ExceptionInfo)))
(deftest query-auth
(testing "Verify auth records exist")
(let [id-list (-> (basic/get-db test/ledger-todo)
(fdb/query-async {:select ["*"] :from "_auth" :opts {:meta true}})
async/<!!
(as-> res (reduce-kv
(fn [z _ v]
(into z [(-> v (get "todo/auth") (get "_id"))]))
[]
res)))]
(is (= 5 (count id-list)))))
(deftest query-todo-root-auth
(testing "testing root auth sees all to-dos")
(let [exp-blk 5
res (async/<!! (-> test/ledger-todo
(basic/get-db {:auth ["_auth/id" (:auth sys-admin)]
:syncTo exp-blk})
(fdb/query-async {:select ["*", {:items ["*"]}]
:from "todo"})))
id-list (reduce-kv
(fn [z _ v]
(into z [(-> v (get "todo/auth") (get "_id"))]))
[]
res)]
(is (= 4 (count id-list)))))
(deftest query-own-todo
(testing "testing non-system admin users see only own to-do")
(let [db (fdb/db (:conn test/system) test/ledger-todo {:auth ["_auth/id" (:auth kevin)]})
res-graph @(fdb/query db {:select ["*", {:items ["*"]}] :from "todo"}) ;; wildcard with crawl(s)
res-analy @(fdb/query db {:select {"?s" ["*", {:items ["*"]}]}
:where [["?s" "rdf:type" "todo"]]})
res-graph2 @(fdb/query db {:select ["*"] :from "todo"}) ;; wildcard
id-list (reduce-kv
(fn [z _ v]
(into z [(-> v (get "todo/auth") (get "_id"))]))
[]
res-graph)]
gershwin/gershwin
(ns clojure.test-clojure.reducers
(:require [clojure.core.reducers :as r])
(: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")))
(deftest test-nil
(is (= {:k :v} (reduce-kv assoc {:k :v} nil)))
(is (= 0 (r/fold + nil))))