Skip to content

Instantly share code, notes, and snippets.

(ns task02.network
(:use [task02 helpers query])
(:require [clojure.java.io :as io]
[clojure.string :as str])
(:import [java.net Socket ServerSocket InetAddress InetSocketAddress SocketException]))
(def inactive-timeout 20000)
;; Объявить переменную для синхронизации между потоками. Воспользуйтесь promise
(def ^{:private true :dynamic true} *should-be-finished* nil)
(defn opening? [sym]
(when (symbol? sym)
(.endsWith (name sym) ">")))
(defn closing? [sym]
(when (symbol? sym)
(.startsWith (name sym) "<")))
(defn split-closing [t exprs]
(split-with
@tonsky
tonsky / .profile.boot
Created March 2, 2015 20:14
boot-clj plugin to report build status to AnyBar
(defn- send-udp [s port]
(with-open [socket (java.net.DatagramSocket.)]
(let [group (java.net.InetAddress/getByName "localhost")
bytes (.getBytes s)
packet (java.net.DatagramPacket. bytes (count bytes) group port)]
(.send socket packet)
(.close socket))))
(deftask anybar [p port VAL int "AnyBar port"]
(let [port (or port 1738)]
@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);
}
@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 / 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"]
(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)))
(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)]
(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")]
(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))