A common transformation I run into is I have a sequence of rows that I want to convert into a
map. The most recent example of this I ran into I have a query to return all the "edge-types" which returns this:
In the context of my application this adds some messy indirection
between specifying the data I want, and the data I actually use
A better implementation is:
(defn index-by
"Given a function f and a seq coll,
returns a map mapping for each value x in coll
(f x) to x; presumes f is injective on coll."
[f coll]
(persistent!
(reduce
(fn [tm x]
(assoc! tm (f x) x))
(transient {}) coll)))
There exists the group-by function and set/index, but neither is very useful in practice (to me) because group-by creates sequences, and set/index creates maps as keys.
A common transformation I run into is I have a sequence of rows that I want to convert into a map. The most recent example of this I ran into I have a query to return all the "edge-types" which returns this:
But what I really want is:
Because my code uses edge type to look up properties:
So I often find myself writing:
In the context of my application this adds some messy indirection between specifying the data I want, and the data I actually use
A better implementation is:
There exists the
group-by
function andset/index
, but neither is very useful in practice (to me) becausegroup-by
creates sequences, andset/index
creates maps as keys.