Skip to content

Instantly share code, notes, and snippets.

@nathanmarz
Last active September 11, 2015 22:58
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 nathanmarz/3280f4bed63d19d6935e to your computer and use it in GitHub Desktop.
Save nathanmarz/3280f4bed63d19d6935e to your computer and use it in GitHub Desktop.
Late bound parameterization with Specter
(def DATA {"a" [{"b" 1} {"b" 2}] "b" 11})
;; Selectors that require parameters can be precompiled *without* the parameters.
;; The parameters are supplied later.
(def path (comp-paths keypath ALL keypath))
;; These two selections are equivalent
(select (path "a" "b") DATA)
(select [(keypath "a") ALL (keypath "b")] DATA)
;; This function will now execute extremely efficiently. Before, without late-bound parameterization
;; this would run much slower because of the need to compose the path during the function call. With
;; late-bound parameterization, it's done beforehand and the parameters supplied when they're available.
(defn foo [k1 k2 data]
(select (path k1 k2) data))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment