Skip to content

Instantly share code, notes, and snippets.

@krukow
Created March 12, 2022 17:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krukow/070c51827fd3da25aa24a01ee7045011 to your computer and use it in GitHub Desktop.
Save krukow/070c51827fd3da25aa24a01ee7045011 to your computer and use it in GitHub Desktop.
(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