Skip to content

Instantly share code, notes, and snippets.

@trickster
Last active March 23, 2018 15:33
Show Gist options
  • Save trickster/430e9d14cb1a87932887615e9c845791 to your computer and use it in GitHub Desktop.
Save trickster/430e9d14cb1a87932887615e9c845791 to your computer and use it in GitHub Desktop.
Learning Clojure (core.logic too)
(println "Hello")
(let [x 1
y 2]
(print "foo")
(str 1 2
3 4 5))
(subvec (clojure.string/split-lines (slurp "https://www.google.com" :encoding "UTF-8")) 2 3)
(take 2 (clojure.string/split-lines (slurp "https://www.google.com" :encoding "UTF-8")))
(class (slurp "/etc/passwd"))
;; Consume the file lazily, only efficient when map, filter (transducering)
(with-open [rdr (clojure.java.io/reader "/etc/passwd")]
(count (line-seq rdr)))
(def sample-file (slurp "/etc/passwd"))
(take 5 sample-file)
;; Multiple arguments to map
; function
(defn my-inc [x y]
(+ x y))
; test
(my-inc 10 10)
; using anon function
(map #(my-inc 100 %) [1 2 3])
;; partial application
(map (partial my-inc 100) [1 2 3]) ;; partial apply a function
(defn sample-fn [x]
(Thread/sleep (* x 1000))
(println "This is done")
(str "after " x " seconds"))
(pvalues (sample-fn 10) (sample-fn 20))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Concurrency -
;; https://www.braveclojure.com/concurrency/
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(def sample (future (Thread/sleep 10000) (println "Printing after 10 sec") (+ 1 1)))
(println sample) ;#future[{:status :ready, :val 2}]
(println @sample) ; =>2
(realized? sample) ; => true
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; core.logic
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(use 'clojure.core.logic)
(run* [q]
(== q true))
(run* [q]
(membero q [1 2 3])
(membero q [2 3 4]))
(run* [q]
(== 1 q))
(run* [q]
(membero q [1 2 3])
(== 2 q))
(run* [q]
(fresh [a]
(membero a [1 2 3])
(membero q [3 4 5])
(== a q)))
(run* [q]
(conde
[succeed]
[succeed]))
(cons 1 [2 3 4]) ; result is returned as a separate sequence
(run* [q]
(conso 1 [2 3] q)) ; result should be passed along - q is lvar that can only take value 1 2 3
(run* [q]
(conso 1 q [1 2 3])) ; ((2 3)) is the result (== (list 1 q) (list 1 2 3))
(run* [q]
(resto [1 2 3 4] q))
;; beginning of zebra puzzle
(defn caro [p a]
(conso a [] p))
(defn cdro [p d]
(conso [] d p))
(defn pairo [p]
(conso [] [] p))
(defn eqo [x y]
(== x y))
(defn nullo [a]
(eqo a '()))
(defn memo [x l]
(fresh [d]
(conde
[(caro l x)]
[(cdro l d)
(memo x d)])))
(defn rmemo [x l]
(fresh [d]
(conde
[(cdro l d)
(memo x d)]
[(caro l x)])))
(defn reverseo [l r]
(rmemo r l))
;; appendo is already present in core.logic
;; refer to memq from Scheme
;; membero
;;
;; caro is firsto
;; cdro is resto
(run* [q]
(membero q [])
(pairo q))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; (defn init [vars hints]
;; (if (seq vars)
;; (let [hint (first hints)]
;; (all
;; (if-not (zero? hint)
;; (== (first vars) hint)
;; succeed)
;; (init (next vars) (next hints))))
;; succeed))
;; (defn fletfo [x y l]
;; (let [d (first l)]
;; (all
;; (firsto l x)
;; (resto l d)
;; (firsto d y))))
;; (defn rlefto [x y l]
;; (let [d (next l)]
;; (all
;; (if-not (zero? l)
;; (resto l d)
;; (rlefto x y d)))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defn fletfo [x y l]
(fresh [d]
(firsto l x)
(resto l d)
(firsto d y)))
(defn rlefto [x y l]
(let [d (next l)]
(all
(if-not (zero? l)
(resto l d)
(rlefto x y d)))))
(defn lefto [x y l]
(fresh [d]
(conde
[(rlefto x y d)]
[(fletfo x y l d)])))
(defn nexto [x y l]
(conde
[(lefto x y l)]
[(lefto y x l)]))
;; later
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Protocols
(defprotocol P
(foo [x])
(bar-me [x] [x y]))
(reify P)
(deftype Dog [a b c]
P
(foo [x] a)
(bar-me [x] b)
(bar-me [x y] (+ c y)))
(bar-me (Dog. 1 2 3) 42)
(bar-me (Foo. 1 2 3))
;; Using reify
(let [x 42]
(reify P
(foo [this] 17)
(bar-me [this] x)
(bar-me [this y] x)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment