Skip to content

Instantly share code, notes, and snippets.

@m0smith
Created January 4, 2013 17:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save m0smith/4454548 to your computer and use it in GitHub Desktop.
Save m0smith/4454548 to your computer and use it in GitHub Desktop.
An example of Clojure core.logic that finds map that match a certain structure. Both functions are equilivent but one uses list comprehension and the other uses core.logic
(ns logicfun.core
;(:refer-clojure :exclude [==])
(:use [clojure.core.logic :rename {== ?==}]))
(def data [
{:id 1 :type :t1 :name "one" :other1 :o2}
{:id 2 :type :t2 :name "two" :other2 :o1 :another :ao2}
{:id 3 :type :t3 :name "three" :other2 :o1 :another :ao1}
])
(defn map-test-logic []
(run* [q]
(fresh [?id ?type ?name ?pm]
(membero ?type [:t1 :t2])
(?== q [?id ?name])
(featurec ?pm {:id ?id :type ?type :name ?name} )
(membero ?pm data))))
(defn map-test []
(for [{:keys [id type name]} data :when (#{:t1 :t2} type)]
[id name]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment