Skip to content

Instantly share code, notes, and snippets.

View terjesb's full-sized avatar

Terje Sten Bjerkseth terjesb

  • Outnordic
  • Norway
View GitHub Profile
@terjesb
terjesb / refsymbols.clj
Created July 18, 2012 07:34 — forked from claj/refsymbols.clj
Generate datom :db.type/ref s with fn
(use '[datomic.api :only [db q] :as d])
;;not supplied:
;;* connecting to a db named "conn"
;; (checkout awesome Jonas Edlunds gist
;; for inspiration: https://gist.github.com/3122363 )
;;* a schema that fit's the following person-datoms
;; Problem:
;;it's a bit quirky to generate references by functions to transact them to datoms
@terjesb
terjesb / accounts.clj
Created July 18, 2012 07:35 — forked from pelle/accounts.clj
Using database functions in Datomic transactions and annotating transaction history
(use '[datomic.api :only [q db] :as d])
(def uri "datomic:mem://accounts")
;; create database
(d/create-database uri)
;; connect to database
(def conn (d/connect uri))
@terjesb
terjesb / gist:3181000
Created July 26, 2012 08:34 — forked from stuarthalloway/gist:2645453
Datomic queries against Clojure collections
;; Datomic example code
(use '[datomic.api :only (db q) :as d])
;; ?answer binds a scalar
(q '[:find ?answer :in ?answer]
42)
;; of course you can bind more than one of anything
(q '[:find ?last ?first :in ?last ?first]
"Doe" "John")
@terjesb
terjesb / Datomic News Updates
Created July 26, 2012 08:35 — forked from stuarthalloway/Datomic News Updates
Datomic update examples against a social news database
;; Datomic example code
;; demonstrates various update scenarios, using a news database
;; that contains stories, users, and upvotes
;; grab an in memory database
(use '[datomic.api :only (q db) :as d])
(def uri "datomic:mem://foo")
@terjesb
terjesb / logicrels-fred.clj
Created July 26, 2012 08:39 — forked from jsmorph/logicrels-fred.clj
St. Louis Fed FRED relation for clojure.core.logic
(ns logicrels.fred
(:require [clojure.xml :as xml]
[clojure.core.logic :as logic]))
;; (config :fred-key (do "See http://api.stlouisfed.org/api_key.html" nil))
;; (clojure.pprint/pprint (sort-by :date (fred-test "DGS10" "2012-07-02")))
;; ({:target "1.61", :date "2012-05-31", :value "1.59"}
;; {:target "1.61", :date "2012-06-01", :value "1.47"}
@terjesb
terjesb / logicrels-lucene.clj
Created July 26, 2012 08:39 — forked from jsmorph/logicrels-lucene.clj
Lucenalog: Datalog interface to Lucene in 10 lines
(ns lucenalog.core
"Lucenalog = Datalog interface to Lucene in 10 lines.
Simple but powerful.
Use
(db/add (index) {:a \"foo\" :b \"bar\"})
to index a map with Lucene. Then you can use the relation
@terjesb
terjesb / gist:3181096
Created July 26, 2012 08:55 — forked from jonase/gist:3175654
query performance predictability
(use '[datomic.api :only [db q] :as d])
(def schema
[{:db/id #db/id[:db.part/db]
:db/ident :num
:db/valueType :db.type/long
:db/cardinality :db.cardinality/one
:db.install/_attribute :db.part/db}
{:db/id #db/id[:db.part/db]
@terjesb
terjesb / gist:3181098
Created July 26, 2012 08:56 — forked from stuarthalloway/gist:3068749
datomic-clojure-relational-algebra-2012-07-07
;; Datomic example code
;; Demonstrates using datalog with Clojure defrecords
(use '[datomic.api :only [q db] :as d])
;;; http://www.lshift.net/blog/2010/08/21/some-relational-algebra-with-datatypes-in-clojure-12
(defrecord Supplier [number name status city])
(defrecord Part [number name colour weight city])
(defrecord Shipment [supplier part quantity])
;; sample data
@terjesb
terjesb / gist:3206009
Created July 30, 2012 10:14 — forked from stuarthalloway/gist:2560986
Datomic rule to find schema attributes whose name matches a prefix
;; The Datomic schema is made up of Datomic data, so you manipulate it
;; with ordinary queries and transactions -- and with ordinary code
;; in Java or Clojure.
;; query rule that finds attributes in schema whose names start with ?prefix
;; note the Java interop call to .startsWith
;; you could create a similar rule for namespace prefixes
(def rules '[[[attr-starts-with ?prefix ?attr]
[?e :db/valueType]
[?e :db/ident ?attr]
@terjesb
terjesb / gist:3206011
Created July 30, 2012 10:15 — forked from stuarthalloway/gist:2321773
Datomic schema query, constrained by attribute namespace
;; Datomic sample code
;; schema query for attribute types in specified namespaces
(q '[:find ?attr
:in $ [?include-ns ...] ;; bind ?include-ns once for each item in collection
:where
[?e :db/valueType] ;; all schema types (must have a valueType)
[?e :db/ident ?attr] ;; schema type name
[(datomic.Util/namespace ?attr) ?ns] ;; namespace of name
[(= ?ns ?include-ns)]] ;; must match one of the ?include-ns