core.logic 查询字符串包含
core.logic query for string contains
我很好奇是否可能(如果可以,应该如何实现)类似
string-containso
在 core.logic.
示例用法类似于
(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")
提前致谢,
布吉
如果您知道要搜索的子字符串,可以使用 pred
,这对于将任何谓词提升为 core.logic
:
很方便
(defn string-containso
[string substring]
(pred string #(.contains % substring)))
(with-db database
(run* [q]
(person q)
(string-containso q "o")))
; => (John Bob)
如果您还希望能够搜索子字符串,那就更有趣了。您可以尝试枚举字符串的所有子字符串并检查是否包含给定的子字符串:
(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)))
现在,您仍然可以像以前一样使用它:
(with-db database
(run* [q]
(person q)
(string-containso q "o")))
; => (John Bob)
但您也可以使用它来获取所有子字符串:
(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)
我很好奇是否可能(如果可以,应该如何实现)类似
string-containso
在 core.logic.
示例用法类似于
(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")
提前致谢,
布吉
如果您知道要搜索的子字符串,可以使用 pred
,这对于将任何谓词提升为 core.logic
:
(defn string-containso
[string substring]
(pred string #(.contains % substring)))
(with-db database
(run* [q]
(person q)
(string-containso q "o")))
; => (John Bob)
如果您还希望能够搜索子字符串,那就更有趣了。您可以尝试枚举字符串的所有子字符串并检查是否包含给定的子字符串:
(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)))
现在,您仍然可以像以前一样使用它:
(with-db database
(run* [q]
(person q)
(string-containso q "o")))
; => (John Bob)
但您也可以使用它来获取所有子字符串:
(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)