Skip to content

Instantly share code, notes, and snippets.

@viperscape
viperscape / maps defentity's to the table listing in sql
Last active Dec 20, 2015
information schema to defentity in korma
View maps defentity's to the table listing in sql
(defentity INFORMATION_SCHEMA.tables)
(def tables (map #(get % :TABLE_NAME)
(select 'INFORMATION_SCHEMA.tables (fields :TABLE_NAME)
(where {:TABLE_TYPE "BASE TABLE"})))) ;notice the quote
(prn tables)
;(map #(def %) tables) ;additional concept
(map #(defentity %) tables) ;not sure this is required anymore
(select (first tables))
@viperscape
viperscape / matrix operations for 3d graphics.clj
Last active Dec 25, 2015
using core.matrix, perform some 3d graphics-related functions lookat, perspective projection, and building translation and rotation matrices; 'left' in the function name specifies left-handed coordinates
View matrix operations for 3d graphics.clj
;; (ns somenamespace
;; (:refer-clojure :exclude [* - + == /])
;; (:use clojure.core.matrix)
;; (:use clojure.core.matrix.operators)
;; (:gen-class))
(defn inv [n] "returns the inverse, (* -1 n)" (* -1 n))
(defn projection [l r b t, n f, aspect fov]
@viperscape
viperscape / peek-drain.clj
Last active Dec 28, 2015
Quickly look at a core.async channel with only momentary blocking, pulls the data if it exists and thus alters the channel.Drain! will repeatedly call peek! until a maximum number of elements is pulled or until the channel is empty.
View peek-drain.clj
;;uses core.async and refers chan, >!, go, alts!! and timeout functions
(def c (chan))
(go (>! c {:msg "hi"}))
(defn peek! [ch & args]
"quickly looks at a channel for new incoming data, pulls it if it exists, quits otherwise;
returns seq if specifying max elements to pull"
(if-not (nil? args)
@viperscape
viperscape / handler_clients.clj
Created Jan 20, 2014
http-kit connection timeout handling, client tracking. ping-clients should probably be named differently. assumes traffic is json.
View handler_clients.clj
(def clients (atom {}))
(defn remove-client [ch] (close ch)(swap! clients dissoc ch))
(defn purge-clients! []
(doseq [c @clients]
(if (> (- (.getTime (java.util.Date.)) (.getTime (:last-seen (val c)))) 60000)
(remove-client (key c)))))
View websocket-decode-encode.clj
(defn get-chunk [conn buf]
"returns map of bytes and size of buffer for data recieved in stream (buffered input stream);
buf would be something like: (make-array Byte/TYPE 4096)"
(let [bufsize (.read conn buf)] ;;blocking
{:size bufsize :data buf}))
(defn- ws-decode [frame]
"decodes websocket frame"
(let [data (:data frame)
@viperscape
viperscape / bits-lit.clj
Last active Aug 29, 2015
returns map of which bits where flagged
View bits-lit.clj
(defn- bits-lit [b]
"cerates seq of bits that are flagged"
(map #(bit-and b (bit-shift-left 1 %)) (range 8)))
(bits-lit 23) ;;(1 2 4 0 16 0 0 0)
(map pos? (bits-lit 23)) ;; (true true true false true false false false)
@viperscape
viperscape / int-to-bytes.clj
Created Feb 22, 2014
int32 to 4 bytes, which could be used to send size of a following datagram, like a bytes header
View int-to-bytes.clj
(let [i 128900, b (map #(unchecked-byte(bit-shift-right i (* % 8))) (range 0 4))]
{i (reduce +(map #(bit-shift-left (bit-and %1 255) (* %2 8)) b (range 0 4)))})
@viperscape
viperscape / many-to-many-queues.clj
Last active Aug 29, 2015
A small comparison of many to many thread-safe queues in Clojure
View many-to-many-queues.clj
(import 'java.util.concurrent.LinkedBlockingQueue)
(use 'criterium.core)
(defn lbq-test []
(let [q ^LinkedBlockingQueue (LinkedBlockingQueue.)]
(.start(Thread. #(dotimes [n 50000] (.take q))))
(.start(Thread. #(dotimes [n 50000] (.take q))))
(.start(Thread. #(dotimes [n 50000] (.take q))))
(.start(Thread. #(dotimes [n 1000000] (.add q n))))
@viperscape
viperscape / broadcast-stream.clj
Last active Aug 29, 2015
many-to-many broadcast stream
View broadcast-stream.clj
(defn v-> [s v]
"pushes value onto stack, if possible, returns new head/tail"
(let [p (promise)]
(if (deliver s (cons v (list p)))
p)))
(defn v<- [s]
"returns lazy sequence of actual values, without head/tail"
(when (realized? s)
(cons (first @s)
@viperscape
viperscape / gridgraph.clj
Last active Aug 29, 2015
square grid to relationship graph for loom
View gridgraph.clj
(defn gen-graph [s]
(graph ;;generate loom graph
(reduce merge {} ;; format...
(apply concat ;; for loom
(for [r (range s)] ;;for each row
(for [c (range s)] ;;for each column in the row
(let [nl [[r (+ 1 c)]
[(- r 1) c]
[r (- c 1)]
[(+ 1 r) c]]]
You can’t perform that action at this time.