Skip to content

Instantly share code, notes, and snippets.

@tonsky
tonsky / uploader.cljs
Last active September 16, 2016 10:13
Rum file uploader
(ns uploader
(:require
[clojure.string :as str]
[goog.dom :as gdom]
[goog.userAgent :as ua]
[rum.core :as rum]))
(defonce supports-dragndrop?
(let [el (js/document.createElement "div")]
@tonsky
tonsky / user.keymap
Created June 4, 2014 12:04
LightTable keymap of Sublime shortcuts
{ :+
{ :editor
{ "pmeta-/" [:toggle-comment-selection]
"ctrl-shift-up" [:editor.sublime.selectLinesUpward]
"ctrl-shift-down" [:editor.sublime.selectLinesDownward]
"pmeta-d" [:editor.sublime.selectNextOccurrence]
"ctrl-m" [:editor.sublime.goToBracket]
"ctrl-shift-m" [:editor.sublime.selectBetweenBrackets]
"shift-pmeta-space" [:editor.sublime.selectScope]
"ctrl-pmeta-up" [:editor.sublime.swapLineUp]
@tonsky
tonsky / gist:0e90dde5b1cea02c2de0dbf1377bb41c
Created April 18, 2016 19:22
Rum 0.8.0 vs Hiccup 1.0.5
[~/Dropbox/ws/rum] lein perf
--- Testing page1.html (1 kB) ---
Evaluation count : 4656 in 6 samples of 776 calls.
Execution time mean : 127.518354 µs
Execution time std-deviation : 3.729988 µs
Execution time lower quantile : 124.490218 µs ( 2.5%)
Execution time upper quantile : 132.649851 µs (97.5%)
Overhead used : 2.051537 ns
(defn component [ref render-fn & {:keys [on-mount on-unmount]}]
(let [comp (React/createClass (clj->js
{ :render (fn [] (crate/html (render-fn @ref)))
:componentDidMount (or on-mount identity)
:componentWillUnmount (or on-unmount identity) }))]
(add-watch ref :react
(fn [_ _ old new]
(when (not= old new) (.forceUpdate comp))))
comp))
(require '[clojure.string :as str])
(defn parse-line [line]
(let [line (str/replace line #"[\\]\s*(\n\s*|$)" "")]
(when-not (str/blank? line)
(let [[k v] (str/split line #"=" 2)]
[(str/trim k) (when-not (str/blank? v) (str/trim v))]))))
(defn parse [text]
(let [lines (str/split text #"(?<![\s\\])\s*\n")]
(def ^:dynamic chunk-size 17)
(defn next-chunk [rdr]
(let [buf (char-array chunk-size)
s (.read rdr buf)]
(when (pos? s)
(java.nio.CharBuffer/wrap buf 0 s))))
(defn chunk-seq [rdr]
(when-let [chunk (next-chunk rdr)]
(ns ini
(require [clojure.string :as str]
[clojure.tools.logging :as log])
(use clojure.test))
(defn read [content]
(-> content
(str/replace #"\\\s*($|\n\s*)" "")
(str/split-lines)))
@tonsky
tonsky / user.clj
Last active September 16, 2015 14:08
Querying DataScript database for entities with 3+ tags
(require '[datascript :as d])
(defn count-vals [db e a]
(count (d/datoms db :eavt e a)))
(let [db (-> (d/empty-db {:link/tags {:db/cardinality :db.cardinality/many}})
(d/db-with [[:db/add 1 :link/tags "a"]
[:db/add 1 :link/tags "b"]
[:db/add 1 :link/tags "c"]
[:db/add 2 :link/tags "x"]
@tonsky
tonsky / gist:9da339f87113e1f1a395
Last active August 29, 2015 14:22
DataScript 0.11.4 perftests (clj/cljs comparison)

CLJ/CLJS perf comparison on DataScript 0.11.4

CLJ

test: q, method: d/q1, size: 100
{:unit μs, :min 61.923, :median 76.887, :max 161.738}

test: q, method: d/q2, size: 100
{:unit μs, :min 200.32, :median 213.523, :max 251.318}

@tonsky
tonsky / perf.html
Created May 24, 2015 15:02
Comparing perf and memory of mutable/immutable data structures
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/immutable/3.7.3/immutable.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mori/0.3.2/mori.js"></script>
<script type="text/javascript">
var hex_chars = "0123456789abcdef";
function rand_int(max) {
return Math.floor(Math.random() * max);
}