brokvolli.core
Vars used from other namespaces, plus helper functions.
*keydex*
dynamic
Within a transducer stack modified by kv-ize, dynamically bound to the “current” key/index while being invoked with single/transduce-kv and multi/transduce-kv. Do not manually re-bind.
concatv
(concatv)(concatv v)(concatv v1 v2)Concatenates vectors v1 and v2 eagerly and efficiently.
See benchmarking and analysis.
Examples:
(concatv) ;; => []
(concatv [11 22 33]) ;; => [11 22 33]
(concatv [11 22 33] [44 55 66]) ;; => [11 22 33 44 55 66]
kv-ize
(kv-ize composition)Given a composition of transducing functions (a transducing stack) that only provides arities zero, one, and two, returns an equivalent transducer stack that also accepts result, key/index, and value, suitable for use with both transduce-kv variants, single/transduce-kv and multi/transduce-kv.
Intended for exploratory work or when the transducer stack is pre-composed. Otherwise, prefer composing transducer stacks with brokvolli.transducers-kv and brokvolli.stateful-transducers-kv.
The returned composition provides two additional capabilities.
- Diverts the key/index provided by
transduce-kv, passing only the accumulated value and the next element to the outer/top transducer, and - Establishes a binding context where the key/index is available by referencing *keydex* from any layer of the transducer stack.
Example, a transducer stack, out of our control:
(def xf-from-somewhere (comp (map inc)
(filter even?)
(take 3)))
(transduce-kv (kv-ize xf-from-somewhere) conj [11 22 33 44 55 66 77 88 99])
;; => [12 34 56]
Example, using clojure.core/filter instead of filter-kv, key/index accessible through dynamic var *keydex*:
(transduce-kv (kv-ize (filter (fn [_] (<= *keydex* 2)))) conj [11 22 33 44 55])
;; => [11 22 33]
tassoc
(tassoc)(tassoc x)(tassoc x y)(tassoc x y z)An assoc variant, useful as the reducing function in a transduce operation.
- With no args, returns
{}. - With one arg, returns arg.
- With two args, associates
yto *keydex* withinx. - With three args, associates
ytozwithinx.
See also tconj for a similar utility used in transducing over sequential collections.
Example:
(transduce-kv (map-kv (fn [keydex x] {:keydex (inc keydex) :value x})) tassoc [11 22 33])
;; => {0 {:keydex 1, :value 11}
;; 1 {:keydex 2, :value 22}
;; 2 {:keydex 3, :value 33}}
tconj
(tconj)(tconj x)(tconj x y)(tconj x _ z)A conj variant, useful as the reducing function in a transduce operation.
- With no args, returns
[]. - With one arg, returns arg.
- With two args, conjoins
yontox. - With three args, conjoins
zontox, ignoring the second arg (which is typically the key/index).
See also tassoc for a similar utility used in transducing over associative collections.
Example:
(transduce-kv (map-kv (fn [keydex x] [keydex x])) tconj [11 22 33])
;; => [[0 11] [1 22] [2 33]]