This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns datomicdemo.core | |
(:require | |
[clojure.edn] | |
[clojure.string :as str] | |
[datomic.api :as d])) | |
(def url "datomic:free://localhost:4334/datomicdemo") | |
(d/create-database url) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; I have this | |
(def routes ["/" [["" :ui/index] | |
["api/query" :api/query] | |
["api/data-init" :api/data-init]]]) | |
;; And I want to make api/data-init only accesible through POST | |
;; I tried | |
(def routes ["/" [["" :ui/index] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(let [a 1, a 2, a 3] | |
(and (= a 1) (= a 2) (= a 3))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn bench-btset [] | |
(doseq [distinct [:distinct :duplicates] | |
size [20000] | |
[tn target] [["sorted-set" (sorted-set)] | |
["btset" (btset/btset)] | |
["vector" []]] | |
:let [range (if (= :distinct distinct) | |
(shuffle (range size)) | |
(repeatedly size #(rand-int size))) | |
shuffled-range (shuffle range) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn digits [x] | |
(loop [r x, digits []] | |
(if (== 0 r) | |
digits | |
(recur (quot r 10) (conj digits (rem r 10)))))) | |
(defn double-odd-digits [digits] | |
(map-indexed (fn [i d] (if (odd? i) (* 2 d) d)) digits)) | |
(defn valid? [x] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn quick-benchmark [fun] | |
(let [t0 (js/performance.now)] | |
(loop [] | |
(dotimes [_ 10] (fun)) | |
(when (< (- (js/performance.now) t0) 1000) (recur))) | |
(let [t0 (js/performance.now) | |
steps (loop [steps 0] | |
(dotimes [_ 10] (fun)) | |
(if (< (- (js/performance.now) t0) 5000) | |
(recur (+ steps 10)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn race | |
"Runs two fns in tight loops in two parallel threads for 1 second" | |
[publish-fn check-fn] | |
(println "---") | |
(let [*run? (volatile! true) | |
*failures (volatile! 0) | |
thread! #(future | |
(loop [i 0] | |
(% i) | |
(if @*run? (recur (inc i)) i))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(require '[criterium.core :as c]) | |
(defn format-time [estimate] | |
(let [mean (first estimate) | |
[factor unit] (c/scale-time mean)] | |
(c/format-value mean factor unit))) | |
(defmacro race [body1 body2] | |
`(let [_# (assert (= ~body1 ~body2)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(import '[java.net InetAddress DatagramPacket DatagramSocket]) | |
(defonce socket (DatagramSocket.)) | |
(defn send-message [text port] | |
(let [host (InetAddress/getLocalHost) | |
packet (DatagramPacket. (.getBytes text) (.length text) host port)] | |
(.send socket packet))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(require '[criterium.core :as c]) | |
(defn new_= | |
"Equality. Returns true if x equals y, false if not. Same as | |
Java x.equals(y) except it also works for nil, and compares | |
numbers and collections in a type-independent manner. Clojure's immutable data | |
structures define equals() (and thus =) as a value, not an identity, | |
comparison." | |
{:inline (fn [x y] `(. clojure.lang.Util equiv ~x ~y)) | |
:inline-arities #{2} |