Skip to content

Instantly share code, notes, and snippets.

@timvisher
Created December 2, 2013 19:34
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 timvisher/7756042 to your computer and use it in GitHub Desktop.
Save timvisher/7756042 to your computer and use it in GitHub Desktop.

I’d like to be able to do something like the following:

(filter (like? {:a-key :*
                :b-key [1 2 :*]
                :c-key #{:*}
                :e-key #{"an-item" :*}
                :*     :*})
        seqable)

The idea being that you query data with example data.

src_clojure{:*} is a wildcard, indicating that the query engine is able to match anything. It’s meaning also changes with the context.

Actually, I think wildcards are undesirable. The whole point of this is to make it so you can take a sparse example data structure and filter using it over more-populated data structures. The wildcards sort of defeat this purpose.

Actually, they could serve a purpose still. If I wanted some portions of the comparison to be strict, but others to be relaxed, I could do that with the wildcard keyword, but not without it.

(filter (like? (wildcard {:a [1 2 3] :b (wildcard #{:b})})) data-structure)

In this instance, I can use the wildcard function to add a wildcard to the subsequent data structure but not any of the data structures contained therein. I don’t know if that’s any nicer than just doing that in data. I’m inclined to say no.

What bothers me about the wildcard is how I often I imagine myself using it. In the case where I want an entirely sparse example data structure to be matched, I’d have to put it everywhere.

Maybe the solution at that point is to have a src_clojure{wildcard} function that does a deep walk of the entire contained data structure, adding wildcards at each point. All that src_clojure{wildcard} should do, I think, would be to annotate the data structure with the appropriate wildcard data, so that you could also represent that same thing in data if you so chose.

The following example shows how this might be used to query an in-memory datastructure.

(def data-structure
  [{:a "some sort of string"
    :b [1 2 3 4]
    :c #{:a :b :c}
    :d :doesnt-matter}
   {:a "of string"
    :b [1 2 3 7 8 9]
    :c #{:b :d :e :f}
    :d :also-doesnt-matter}
   {:a "a string"
    :b [7 8 9 10]
    :c #{:d :e :f}}])

(filter (like? {:a "of string"
                :b [1 2 3]
                :c #{:b}})
        data-structure)

;;=>
;; [{:a "some sort of string"
;;   :b [1 2 3 4]
;;   :c #{:a :b :c}
;;   :d :doesnt-matter}
;;  {:a "of string"
;;   :b [1 2 3 7 8 9]
;;   :c #{:b :d :e :f}
;;   :d :also-doesnt-matter}]

;; (filter (like? (wildcard {:a "of string"
;;                           :b [1 2 3]
;;                           :c #{:b}}))
;;         data-structure)

;; ;;=>
;; ;; [{:a "some sort of string"
;; ;;   :b [1 2 3 4]
;; ;;   :c #{:a :b :c}
;; ;;   :d :doesnt-matter}
;; ;;  {:a "of string"
;; ;;   :b [1 2 3 7 8 9]
;; ;;   :c #{:b :d :e :f}
;; ;;   :d :also-doesnt-matter}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment