Skip to content

Instantly share code, notes, and snippets.

@visibletrap
Last active May 7, 2018 08:29
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 visibletrap/2fdfdb7e039743f7a66f92eecb9e8152 to your computer and use it in GitHub Desktop.
Save visibletrap/2fdfdb7e039743f7a66f92eecb9e8152 to your computer and use it in GitHub Desktop.
Input: [{:province :a, :district :b, :amphoe :c}, {:province :a, :district :k, :amphoe :g}], Output: {:a {:c [:b], :g [:k]}}
(defn group-amphoes [locations]
(->> locations
(group-by :amphoe)
(map (juxt first (comp #(mapv :district %) second)))
(into {})))
(defn group-provinces [locations]
(->> locations
(group-by :province)
(map (juxt first (comp group-amphoes second)))
(into {})))
(group-provinces [{:province :a, :district :b, :amphoe :c}, {:province :a, :district :k, :amphoe :g}])
;=> {:a {:c [:b], :g [:k]}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment