Hi - I was answering a question on StackOverflow, and after solving it
using functions from clojure.core, I thought it was the prefect candidate
for a little DataScript. Basically the user wants to do a "natural join"
on column :obs/A, then print out pairs of :obs/value from type :x and type
:y. The DataScript version removes a lot of tedious, manual indexing,
filtering, unification, etc.
(ns clj.core
(:require [tupelo.core :as t]
[datascript.core :as d]
[clojure.set :as set] ))
(t/refer-tupelo)
(def data
[ {:type :x :local/id 1, :obs/A "11", :obs/value 2.0, :obs/color
"yellow"}
{:type :x :local/id 2, :obs/A "12", :obs/value 4.0, :obs/color
"blue"}
{:type :x :local/id 3, :obs/A "13", :obs/value 3.0, :obs/color
"green"}
{:type :x :local/id 3, :obs/A "15", :obs/value 7.0, :obs/color
"red"}
{:type :y :local/id 2, :obs/A "11", :obs/value 7.0, :obs/shape
"square"}
{:type :y :local/id 2, :obs/A "13", :obs/value 4.0, :obs/shape
"circle"}
{:type :y :local/id 6, :obs/A "15", :obs/value 3.0, :obs/shape
"triangle"} ] )
(newline) (println "data") (pretty data)
(def conn (d/create-conn {}))
(d/transact! conn data)
(def labelled-result
(d/q '[:find ?a ?value1 ?value2
:where
[?ex :type :x] [?ex :obs/A ?a] [?ex :obs/value ?value1]
[?ey :type :y] [?ey :obs/A ?a] [?ey :obs/value ?value2]
] @conn ))
(newline) (println "labelled-result") (pretty labelled-result)
(defn -main [& args] )
With the result:
data
[{:type :x,
:local/id 1,
:obs/A "11",
:obs/value 2.0,
:obs/color "yellow"}
{:type :x,
:local/id 2,
:obs/A "12",
:obs/value 4.0,
:obs/color "blue"}
{:type :x,
:local/id 3,
:obs/A "13",
:obs/value 3.0,
:obs/color "green"}
{:type :x, :local/id 3, :obs/A "15", :obs/value 7.0, :obs/color "red"}
{:type :y,
:local/id 2,
:obs/A "11",
:obs/value 7.0,
:obs/shape "square"}
{:type :y,
:local/id 2,
:obs/A "13",
:obs/value 4.0,
:obs/shape "circle"}
{:type :y,
:local/id 6,
:obs/A "15",
:obs/value 3.0,
:obs/shape "triangle"}]
labelled-result
#{["13" 3.0 4.0] ["11" 2.0 7.0] ["15" 7.0 3.0]}
Cool stuff.
Alan
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.