3
votes

I'm curios if it is possible (and if so how should one implement) something like

string-containso

in core.logic.

an example usage would be something like

(db-rel person x)

(def database
  (db
    [person "John"]
    [person "Jane"]
    [person "Jim"]
    [person "Dan"]
    [person "Bob"]))

(with-db database
  (run* [q]
    (string-containso q "o")))

;would return ("John" "Bob")

Thanks in advance,

Boogie

1

1 Answers

4
votes

If you know the substring you're searching for, you can use pred, which is handy for lifting any predicate into core.logic:

(defn string-containso
  [string substring]
  (pred string #(.contains % substring)))

(with-db database
  (run* [q]
    (person q)
    (string-containso q "o")))
; => (John Bob)

It's a little more interesting if you also want to be able to search for substrings. You could try enumerating all of a string's substrings and checking whether that includes a given one:

(defn substrings
  [s]
  (for [start (range (inc .length s))
        end (range (inc start) (inc (.length s)))]
    (.substring s start end)))

(defn string-containso
  [string substring]
  (fresh [all-substrings]
    (is all-substrings string substrings)
    (membero substring all-substrings)))

Now, you can still use this the way you did before:

(with-db database
  (run* [q]
    (person q)
    (string-containso q "o")))
; => (John Bob)

But you could also use it to get all of the substrings:

(with-db database
  (run* [q]
    (fresh [p]
      (person p)
      (string-containso p q))))
; => (J Jo J Joh John Ji o J oh Jim ohn h i hn Ja n im D m Jan B Jane Da a Bo an Dan ane Bob n a ne o e an ob n b)