Skip to content

Instantly share code, notes, and snippets.

View ghadishayban's full-sized avatar

Ghadi Shayban ghadishayban

View GitHub Profile
@ghadishayban
ghadishayban / resultset.clj
Created September 8, 2014 18:10
result sets
(defn result-set
[^ResultSet rs & {:keys [identifiers]
:or {identifiers str/lower-case}}]
(reify clojure.lang.IReduce
(reduce [this f]
(reduce [this f (f)]))
(reduce [this f init]
(let [rsmeta (.getMetaData rs)
idxs (range 1 (inc (.getColumnCount rsmeta)))
@ghadishayban
ghadishayban / weighted_rand.clj
Last active September 23, 2022 08:15
Vose's alias method for weighted randoms
(ns weighted-rand
(:import clojure.lang.PersistentQueue))
(defprotocol Rand
(nextr [_ rng]))
;; Vose's alias method
;; http://www.keithschwarz.com/darts-dice-coins/
(deftype Vose [n ^ints alias ^doubles prob]
@ghadishayban
ghadishayban / flow_control.clj
Last active August 29, 2015 14:04
"turnstile" for flow-control
(ns flow-control
(:require [clojure.core.async :as async :refer :all
:exclude (map merge partition-by partition unique take into reduce split)]))
;; flow control through a "turnstile"
;; kind of like an extensible circuit breaker
;; Overview --
;; Your program's processes are like people entering a subway,
;; but the central authority only allows certain tickets in.
docker@boot2docker:~$ sudo iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain FORWARD (policy ACCEPT)
target prot opt source destination
ACCEPT tcp -- anywhere 172.17.0.3 tcp dpt:smtp
ACCEPT tcp -- anywhere 172.17.0.2 tcp dpt:smtp
ACCEPT all -- anywhere anywhere ctstate RELATED,ESTABLISHED
ACCEPT all -- anywhere anywhere
@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
@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 / 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 / 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 / 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 / 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)