Skip to content

Instantly share code, notes, and snippets.

View ghadishayban's full-sized avatar

Ghadi Shayban ghadishayban

View GitHub Profile
@ghadishayban
ghadishayban / llp.server.cljs
Created December 17, 2011 22:06
HL7 LLP wire protocol receiver, ClojureScript on node.js. No ACKs yet
(ns llp.server
(:require [cljs.nodejs :as node]
[clojure.string :as str]))
(def net (node/require "net"))
(def events (node/require "events"))
(defn bufs->string [bufs]
(loop [acc ""
rst bufs]
@ghadishayban
ghadishayban / gist:2636413
Created May 8, 2012 15:37
Lazily distinct list
(defn lazily-distinct [coll]
"Returns a lazy distinct list of possibly infinite collection.
Does not realize the whole collection."
(letfn [(lazystep [seen l]
(lazy-seq
(when (seq l)
(loop [next-item (first l) lazy-list (rest l)]
(if (get seen next-item)
(recur (first lazy-list) (rest lazy-list))
(cons next-item (lazystep (conj seen next-item)
@ghadishayban
ghadishayban / Coder.scala
Created May 28, 2012 05:40 — forked from swannodette/Coder.scala
phone_code.clj
package demo
class Coder(words: List[String]) {
private val mnemonics = Map(
'2' -> "ABC", '3' -> "DEF", '4' -> "GHI", '5' -> "JKL",
'6' -> "MNO", '7' -> "PQRS", '8' -> "TUV", '9' -> "WXYZ")
private val charCode: Map[Char, Char] =
for ((digit, str) <- mnemonics; letter <- str) yield letter -> digit
@ghadishayban
ghadishayban / vhsrip.sh
Created November 5, 2012 14:43
Rip an old VHS using avconv and video4linux2
#!/bin/bash
# expects filename as first arg
# Using jack highly recommended over alsa
# Make sure you set up real time priorities
pasuspender -- qjackctl &
echo Before pressing play, go into the jack GUI and \
connect the source to the sink.
@ghadishayban
ghadishayban / coin.clj
Created May 1, 2013 21:07
Coin Kata for Paul DG
(ns cljdojo.coin
(:require [clojure.test :refer :all]))
(def coins (sorted-set-by > 50 25 10 5 1))
(defn min-coins [total]
(letfn [(step [[remaining tallies] coin]
(let [decrements (->> (iterate #(- % coin) remaining)
(take-while #(>= % coin))
count)
@ghadishayban
ghadishayban / fancie_queues.clj
Last active December 17, 2015 05:59
Experiments with a parallel ->> macro
(ns fancie-queues
(:import [java.util.concurrent BlockingQueue
LinkedBlockingQueue]))
;; see queue-chain at the bottom
;; (queue-chain (range 20) (map (partial * 5)) (map vector) (map pr-str))
;; (queue-chain (range 20) (remove odd?) (map pr-str))
;; for more fun:
;; (queue-chain (range 20) (remove odd?) (map (fn [i] (Thread/sleep 150) (pr-str i))))
;; throwing an exception in the middle will not be fun, fyi also queues don't take nil
@ghadishayban
ghadishayban / für donald.clj
Last active December 25, 2015 13:49
Destructure ugly JSON keys with underscores as symbols with dashes
(require '[clojure.string :as str])
(defn rename-underscore [sym]
[(symbol (str/replace (name sym) \_ \-))
(keyword sym)])
(defn udestructure [[lhs expr]]
(if (and (map? lhs)
(vector? (:_keys lhs)))
(let [msym (gensym)
@ghadishayban
ghadishayban / gist:7002262
Last active December 25, 2015 15:59
Regex split as Clojure reducer. Faster than java's Pattern.split(string)
;; Reimplementation of Java's (.split Pattern) to be zero-allocation, similar
;; to JDK 8 splitAsStream
;; Discards trailing ""
;; respects `clojure.core.reduced`
(defn split-string
[^CharSequence s ^java.util.regex.Pattern re]
(reify clojure.core.protocols/CollReduce
(coll-reduce [_ f init]
(let [m (re-matcher re s)
@ghadishayban
ghadishayban / foldtest.clj
Created April 1, 2014 20:18
pathological fold performance
(ns foldtest
(:require [clojure.core.reducers :as r]))
;; redefined to parameterize the threshold to r/fold
(defn foldcat [n coll]
(r/fold n r/cat r/append! coll))
(defn benchmark [n]
(let [x (into [] (range 1e8))]
(println "init vec")
@ghadishayban
ghadishayban / lb.clj
Last active August 29, 2015 13:58
LB 4/3 qry
-- these are for daily
-- generated by accompanying code
select * from delivery_stats where delivery_id in
(66124923, 66124925, 66124927, 66124929, 66124931, 66124933, 66124935, 66124937, 66124939, 66124941, 66124943, 66124945, 66124947, 66124949, 66124951, 66124953, 66124955, 66124957, 66124959, 66129249, 66129251, 66129253, 66129255, 66129257, 66129259, 66129261, 66129263, 66129265, 66129267, 66129269, 66129271, 66129273, 66129275, 66129277, 66129279, 66129281, 66129283, 66129285, 66129287, 66129289, 66129291, 66129293, 66129299, 66129301, 66129303, 66129305, 66129307, 66129311, 66124879, 66124883, 66124885, 66124887, 66124889, 66124891, 66124893, 66124895, 66124897, 66124899, 66124901, 66124903, 66124905, 66124907, 66124915, 66124919, 66124921, 66128365, 66128383, 66128255, 66128257, 66128259, 66128261, 66128263, 66128265, 66128269, 66128271, 66128273, 66128275, 66128281, 66128283, 66128285, 66128287, 66128289, 66128291, 66128293, 66128295, 66128297, 66128301, 66128305, 66128307, 66128309, 66128313, 661