plumbing.map

Common operations on maps (both Clojure immutable and mutable Java stuff)

collate

(collate flat-counts)
Take a seq of [k v] counts and sum them up into a HashMap on k.

deep-collate

(deep-collate nested-counts)
Take a seq of [kseq v] counts and sum them up into nested HashMaps

flatten

(flatten m)
Transform a nested map into a seq of [keyseq leaf-val] pairs

get!

macro

(get! m k default-expr)
Get the value in java.util.Map m under key k.  If the key is not present,
set the value to the result of default-expr and return it.  Useful for
constructing mutable nested structures on the fly.

(.add ^List (get! m :k (java.util.ArrayList.)) :foo)

inc-key!

(inc-key! m k d)
Increment the value in java.util.Map m under key k by double d.

inc-key-in!

(inc-key-in! m ks d)
Increment the value in java.util.Map m under key-seq ks by double d,
creating and storing HashMaps under missing keys on the path to this leaf.

keep-leaves

(keep-leaves f m)
Takes a nested map and returns a nested map with the same shape, where each
(non-map) leaf v is transformed to (f v), or removed if it returns nil.
Empty maps produced by this pruning are themselves pruned from the output.

keep-leaves-and-path

(keep-leaves-and-path f m)(keep-leaves-and-path f ks m)
Takes a nested map and returns a nested map with the same shape, where each
(non-map) leaf v is transformed to (f key-seq v), or removed if it returns nil.
key-seq is the sequence of keys to reach this leaf, starting at the root.
Empty maps produced by this pruning are themselves pruned from the output.

keyword-map

macro

(keyword-map & syms)
Expands to a map whose keys are keywords with the same name as the given
symbols, e.g.:

  (let [x 41, y (inc x)]
    (keyword-map x y))

  ;; => {:x 41, :y 42}

map-leaves

(map-leaves f m)
Takes a nested map and returns a nested map with the same shape, where each
(non-map) leaf v is transformed to (f v).

map-leaves-and-path

(map-leaves-and-path f m)(map-leaves-and-path f ks m)
Takes a nested map and returns a nested map with the same shape, where each
(non-map) leaf v is transformed to (f key-seq v).
key-seq is the sequence of keys to reach this leaf, starting at the root.

merge-disjoint

(merge-disjoint)(merge-disjoint m)(merge-disjoint m1 m2)(merge-disjoint m1 m2 & maps)
Like merge, but throws with any key overlap between maps

merge-with-key

(merge-with-key f & maps)
Like merge-with, but the merging function takes the key being merged
as the first argument

safe-select-keys

(safe-select-keys m ks)
Like select-keys, but asserts that all keys are present.

topological-sort

(topological-sort child-map & [include-leaves?])
Take an adjacency list representation of a graph (a map from node names to
sequences of child node names), and return a topological ordering of the node
names in linear time, or throw an error if the graph is cyclic.
If include-leaves? is false the ordering will only include keys from child-map,
and if true it will also include nodes only named as children in child-map.

unflatten

(unflatten s)
Transform a seq of [keyseq leaf-val] pairs into a nested map.
If one keyseq is a prefix of another, you're on your own.

update-key!

(update-key! m k f)(update-key! m k f & args)
Transform value in java.util.Map m under key k with fn f.