Created
January 4, 2013 17:53
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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