Skip to content

Instantly share code, notes, and snippets.

@cgrand
Forked from anonymous/gist:4655172
Last active December 11, 2015 20:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cgrand/4655215 to your computer and use it in GitHub Desktop.
Save cgrand/4655215 to your computer and use it in GitHub Desktop.
cheap merge-with when you only need lookup
(defn cheap-merge-with
"Merges two maps-as-functions" [f a b]
(memoize (fn this
([k] (this k nil))
([k default]
(let [av (a k this)
bv (b k this)]
(if (identical? this av)
(if (identical? this bv)
default
bv)
(if (identical? this bv)
av
(f av bv))))))))
(defn fold-vec-maps-into-map [coll]
"Provided a reducer, concatenate into a vector.
Note: same as (into [] coll), but parallel."
(let [merge-fn (partial cheap-merge-with into)]
(r/fold (r/monoid merge-fn hash-map) merge-fn coll)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment