Skip to content

Commit

Permalink
Add a bunch of libraries
Browse files Browse the repository at this point in the history
  • Loading branch information
Jimmy Miller committed Sep 2, 2018
1 parent e8192f7 commit 909409d
Show file tree
Hide file tree
Showing 24 changed files with 911 additions and 9 deletions.
87 changes: 87 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@

# Created by https://www.gitignore.io/api/macos

### macOS ###
# General
.DS_Store
.AppleDouble
.LSOverride

# Icon must end with two \r
Icon

# Thumbnails
._*

# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent

# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk


# End of https://www.gitignore.io/api/macos


# Created by https://www.gitignore.io/api/clojure

### Clojure ###
pom.xml
pom.xml.asc
*.jar
*.class
/lib/
/classes/
/target/
/checkouts/
.lein-deps-sum
.lein-repl-history
.lein-plugins/
.lein-failures
.nrepl-port
.cpcache/


# End of https://www.gitignore.io/api/clojure



# Created by https://www.gitignore.io/api/java

### Java ###
# Compiled class file
*.class

# Log file
*.log

# BlueJ files
*.ctxt

# Mobile Tools for Java (J2ME)
.mtj.tmp/

# Package Files #
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*


# End of https://www.gitignore.io/api/java
1 change: 1 addition & 0 deletions libraries/clojure/fizz-buzz-no-bools/deps.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{:deps {org.clojure/clojure {:mvn/version "1.10.0-alpha6"}}}
29 changes: 29 additions & 0 deletions libraries/clojure/fizz-buzz-no-bools/src/fizz_buzz/core.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
(ns fizz-buzz.core)

(def TRUE (fn [t f] t))
(def FALSE (fn [t f] f))
(def IF (fn [pred t f] (pred t f)))

(def AND
(fn [a b]
(IF a (IF b TRUE FALSE) FALSE)))

(defn is-zero? [n]
(nth (cons TRUE (repeat FALSE)) n))

(defn evenly-divisible? [n m]
(is-zero? (mod n m)))

(defn COND [p1 t1 p2 t2 p3 t3 _ e]
(IF p1 t1 (IF p2 t2 (IF p3 t3 e))))

(defn fizz [n]
(COND
(AND (evenly-divisible? n 3)
(evenly-divisible? n 5)) "FizzBuzz"
(evenly-divisible? n 3) "Fizz"
(evenly-divisible? n 5) "Buzz"
:else n))


(map fizz (range 1 101))
1 change: 1 addition & 0 deletions libraries/clojure/object-system/deps.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{:deps {org.clojure/clojure {:mvn/version "1.10.0-alpha6"}}}
68 changes: 68 additions & 0 deletions libraries/clojure/object-system/src/object_system/core.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
(ns object-system.core
(:require [clojure.string :as string]))

(defn dispatcher [obj message & args]
(apply (message obj)
(cons (partial dispatcher obj) args)))

(defn class [obj]
(partial dispatcher obj))

(defn get-methods [obj]
(obj constantly))

(defn method-names [obj]
(keys (get-methods obj)))

(defn extend-object [obj extension]
(let [methods (get-methods obj)]
(class (merge methods extension))))

(defn inherit [base-class my-class]
(extend-object base-class my-class))

(comment

(def Animal
(class {:isAnimal (fn [this] true)}))

(def Dog
(inherit Animal {:speaks (fn [this] "Bark")}))

(def Insert)
(def Union)

(def Empty
(class {:isEmpty (fn [this] true)
:contains (fn [this i] false)
:insert (fn [this i] (Insert this i))
:union (fn [this s] s)}))

(defn Insert [s n]
(if (s :contains n)
s
(class
{:isEmpty (fn [this] false)
:contains (fn [this i] (or (= i n) (s :contains i)))
:insert (fn [this i] (Insert this i))
:union (fn [this s] (Union this s))})))

(defn Counter [initial-count]
(let [count (atom initial-count)]
(class {:increment (fn [this] (swap! count inc))
:get-count (fn [this] @count)})))

(def counter (Counter 12))

(def super-counter
(extend-object
counter
{:inc-3 (fn [this]
(this :increment)
(this :increment)
(this :increment))}))

(counter :get-count)

(super-counter :inc-3)
)
2 changes: 2 additions & 0 deletions libraries/clojure/pattern-matcher/deps.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{:deps {org.clojure/clojure {:mvn/version "1.10.0-alpha6"}
unifier {:local/root "../unifier"}}}
65 changes: 65 additions & 0 deletions libraries/clojure/pattern-matcher/src/pattern_matcher/core.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
(ns pattern-matcher.core
(:require [unifier.core :as unifier]))

(defn matcher [value]
(fn [pattern]
(unifier/unify {} value pattern)))

(defn first-match [value patterns]
(->> (partition 2 patterns)
(map (juxt (comp (matcher value) first) second))
(filter (comp (complement unifier/failed?) first))
(first)))

(defn match* [value patterns]
(unifier/substitute (first-match value patterns)))

(defmacro match [value & patterns]
`(eval (match* ~value (quote ~patterns))))

(defmacro defmatch [name & patterns]
`(defn ~name [& args#]
(match args# ~@patterns)))




(comment
(match true
true false
false true)

(match :name
:otherName :otherThing
:name :thing)


(match [1 2 3]
[x] {:x x}
[x y] {:x x :y y}
[x y z] {:x x :y y :z z})


(match [1 2 1]
[x y x] {:x x :y y}
[x y z] {:x x :y y :z z})


(defmatch fib
[0] 0
[1] 1
[n] (+ (fib (- n 1))
(fib (- n 2))))


(fib 5)
(fib 10)

(defmatch get-x
[x] x
[x y] x
[x y z] x)

(get-x 1)
(get-x 1 2)
(get-x 1 2 3))
2 changes: 2 additions & 0 deletions libraries/clojure/query-engine/deps.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{:deps {org.clojure/clojure {:mvn/version "1.10.0-alpha6"}
unifier {:local/root "../unifier"}}}
73 changes: 73 additions & 0 deletions libraries/clojure/query-engine/src/query_engine/core.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
(ns query-engine.core
(:require [unifier.core :as unifier]))

(defn match-clause [clause facts env]
(->> facts
(map (partial unifier/unify env clause))
(filter (complement unifier/failed?))))

(defn match-all [clause facts envs]
(mapcat (partial match-clause clause facts) envs))

(defn process-query [clauses facts envs]
(if (empty? clauses)
envs
(recur (rest clauses)
facts
(match-all (first clauses) facts envs))))

(defn q* [{:keys [find where]} db]
(let [envs (process-query where db [{}])]
(map unifier/substitute (map vector envs (repeat find)))))

(defmacro q [query db]
`(q* (quote ~query) db))


(comment

(def db
[[1 :age 26]
[1 :name "jimmy"]
[2 :age 26]
[2 :name "steve"]
[3 :age 24]
[3 :name "bob"]
[4 :address 1]
[4 :address-line-1 "123 street st"]
[4 :city "Indianapolis"]])



(q {:find {:name name}
:where [[_ :name name]]}
db)


(q {:find {:name name
:age age}
:where [[e :name name]
[e :age age]]}
db)




(q {:find {:name1 name1
:name2 name2}
:where [[e1 :name name1]
[e2 :name name2]
[e1 :age age]
[e2 :age age]]}
db)



(q {:find {:name name
:address-line-1 address-line-1
:city city}
:where [[e :name name]
[a :address e]
[a :address-line-1 address-line-1]
[a :city city]]}
db))
1 change: 1 addition & 0 deletions libraries/clojure/unifier/deps.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{:deps {org.clojure/clojure {:mvn/version "1.10.0-alpha6"}}}
Loading

0 comments on commit 909409d

Please sign in to comment.