Skip to content

Instantly share code, notes, and snippets.

Chris Gill viperscape

Block or report user

Report or block viperscape

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
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]]]
@viperscape
viperscape / nasa-apod.py
Created Nov 6, 2014
pull down hi-res of nasa astronomical pic of the day
View nasa-apod.py
import urllib.request
address = 'http://apod.nasa.gov/apod/astropix.html'
file = urllib.request.urlopen(address) #read in the site and hunt for the image url
html = file.read()
data = html.decode('utf-8')
#print (data)
s = data.find("<a href=\"image")+14 #find the first instance of the image hyperlink
@viperscape
viperscape / init.el
Created Nov 12, 2014
toggle cleanmode (no menu,scroll,toolbars)
View init.el
(set 'cleanmode 1)
(defun toggle-cleanmode ()
"toggles clean mode"
(interactive)
(cond ((not cleanmode)
(menu-bar-mode -1)
(tool-bar-mode -1)
(scroll-bar-mode -1)
(set 'cleanmode 1))
((eq 1 cleanmode)
@viperscape
viperscape / init.el
Last active Aug 29, 2015
rust lang, cargo/rustc run hotkeys
View init.el
(defun rust-save-compile-and-run-this ()
(interactive)
(save-buffer)
(compile
(format "rustc %s && %s"
(buffer-file-name)
(file-name-sans-extension (buffer-file-name)))))
(defun rust-save-compile-and-run ()
(interactive)
@viperscape
viperscape / lazy-re.clj
Last active Aug 29, 2015
lazy regex matches
View lazy-re.clj
(defn lazy-re [re pp]
(let [re-m (re-matcher re pp)
re-cm #(re-find re-m)
call-lzre
(fn step []
(lazy-seq
(when-let [r (re-cm)]
(cons r
(step)))))]
(call-lzre)))
You can’t perform that action at this time.