-
-
Save krukow/070c51827fd3da25aa24a01ee7045011 to your computer and use it in GitHub Desktop.
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 krukow.example | |
(:refer-clojure :exclude [==]) | |
(:use clojure.core.logic) | |
(:require [datomic.client.api :as d])) | |
(def mentors | |
{0 "fred" | |
1 "lucy" | |
2 "ethel"}) | |
(def mentees | |
{0 "bob" | |
1 "mary" | |
2 "ricky"}) | |
(def mentee-preferences | |
[{:mentee "bob" | |
:mentor-preferences ["lucy" "fred" "ethel"]} | |
{:mentee "mary" | |
:mentor-preferences ["lucy" "ethel"]} | |
{:mentee "ricky" | |
:mentor-preferences ["ethel" "fred"]} | |
]) | |
;; solutions | |
;; (([:bob :fred] [:mary :lucy] [:ricky :ethel]) | |
;; ([:bob :lucy] [:mary :ethel] [:ricky :fred]) | |
;; ([:bob :ethel] [:mary :lucy] [:ricky :fred])) | |
(def client (d/client {:server-type :dev-local | |
:storage-dir :mem | |
:system "dev"})) | |
(d/create-database client {:db-name "mentor-match"}) | |
(def conn (d/connect client {:db-name "mentor-match"})) | |
(def mentor-match-schema [{:db/ident :mentee/name | |
:db/valueType :db.type/string | |
:db/unique :db.unique/identity | |
:db/cardinality :db.cardinality/one | |
:db/doc "Name of a mentee"} | |
{:db/ident :mentor/handle | |
:db/valueType :db.type/string | |
:db/unique :db.unique/identity | |
:db/cardinality :db.cardinality/one | |
:db/doc "Handle of a mentor"} | |
{:db/ident :mentee/mentor-preference | |
:db/valueType :db.type/ref | |
:db/cardinality :db.cardinality/many}]) | |
(d/transact conn {:tx-data mentor-match-schema}) | |
(def mentors-data (mapcat | |
(fn [{:keys [mentor-preferences]}] | |
(map | |
#(hash-map :mentor/handle %) | |
mentor-preferences)) | |
mentee-preferences)) | |
(d/transact | |
conn | |
{:tx-data mentors-data}) | |
(def mentees-data | |
(map | |
(fn [x] | |
(let [{:keys [mentee mentor-preferences]} x] | |
{:mentee/name mentee | |
:mentee/mentor-preference | |
(map #(hash-map :mentor/handle %) | |
mentor-preferences)})) | |
mentee-preferences)) | |
(d/transact | |
conn | |
{:tx-data mentees-data}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment