Skip to content

Instantly share code, notes, and snippets.

@uwo
Last active July 9, 2018 19:09
Show Gist options
  • Save uwo/ea75d4fc11014df8776a3c23020c5b16 to your computer and use it in GitHub Desktop.
Save uwo/ea75d4fc11014df8776a3c23020c5b16 to your computer and use it in GitHub Desktop.
key selection
{:paths ["."]}
(ns key-selection
(:require [clojure.spec.alpha :as s]))
(defn select-keys-by-ns
[ns-re m]
(into {} (filter #(re-find ns-re (namespace (key %))) m)))
(defn select-keys-by-ns-exact
"A quick idiom for resolving autogenerated namespaces is
(namepsace ::_) or
(namespace ::namespace-alias/_)"
[ns m]
(into {} (filter #(= (namespace (key %)) ns) m)))
(s/fdef select-keys-by-ns-exact
:args (s/cat :namespace string?
:map (s/map-of keyword? any?))
:ret (s/map-of keyword? any?))
#_(select-keys-by-ns-exact (namespace ::_) {::test 1 ::two 21 :blah/asdf 3})
#_(select-keys-by-ns #"asdf" {:asdf/test "asdf" :qwer/test "qwer"})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment