Skip to content

Instantly share code, notes, and snippets.

View ghadishayban's full-sized avatar

Ghadi Shayban ghadishayban

View GitHub Profile
(ns day3
(:require [util]))
(defn adjacent-range
"returns [(dec lo), (inc hi)], lo clamped by 0, hi clamped by arg"
[lo hi hi-clamp]
[(max (dec lo) 0)
(min (inc hi) hi-clamp)])
(defn scan-adjacent
@ghadishayban
ghadishayban / iteration.clj
Created January 12, 2022 21:43
iteration async
;; needs a refer-clojure exclude on iteration
(defn iteration
"returns a channel of items given step!, a function of some (opaque continuation data) k
step! calls can get bufsize ahead of consumption (asynchronously)
step! - fn of k/nil to chan yielding (opaque) 'ret'
:bufsize - buffer this many step! calls, default 1
:some? - fn of ret -> truthy, indicating there is a value
@ghadishayban
ghadishayban / aproto3.ebnf
Last active August 7, 2020 04:00
instaparse is a super power
proto = <syntax> { import | package | option | message | enum | service | emptyStatement } <ws>
(* Header *)
syntax = ws "syntax" ws "=" ws ( "'proto3'" | '"proto3"' ) ws ";"
import = <ws "import" ws> [ "weak" | "public" ] <ws> strLit <ws ";">
package = <ws "package" ws> fullIdent <ws ";">
option = <ws "option" ws> optionName <ws "=" ws > constant <ws ";">
@ghadishayban
ghadishayban / retry.clj
Last active May 1, 2019 21:41
retry with completablefuture
(ns retry
(:import [java.util.function Supplier]
[java.util.concurrent CompletableFuture TimeUnit]))
(defn with-retry
"given an op wanting retries, and a strategy for backoff,
returns a CompletableFuture that can be waited on
op takes no args
A backoff strategy is a function of an exception, returning nil or a number of milliseconds to backoff"
@ghadishayban
ghadishayban / functional_interfaces.clj
Last active February 4, 2019 17:52
SAM inference helpers
(import '[java.lang.reflect Method Modifier])
(set! *warn-on-reflection* true)
(defn- samsig
"Given a SAM interface, returns the j.l.reflect.Method of the abstract method"
[sym]
(let [kls (resolve sym)]
(if (and (class? kls) (.isInterface ^Class kls))
(let [mid (fn [^Method m] [(.getName m) (vec (.getParameterTypes m))])
@ghadishayban
ghadishayban / vendor_asm.sh
Created February 12, 2018 01:46
a script that vendors asm and writes itself as git commit 'transaction data'
#!/bin/bash
# author Ghadi Shayban <gshayban@gmail.com>
set -e
if [ -z ${1+x} ]
then
echo provide an asm git sha / ref
exit 1
fi
@ghadishayban
ghadishayban / keybase.md
Created November 12, 2017 18:33
keybase.md

Keybase proof

I hereby claim:

  • I am ghadishayban on github.
  • I am ghadi (https://keybase.io/ghadi) on keybase.
  • I have a public key ASBXbzSnNB9Tcf5AU7PXtLsWEiBhS8jeuA3ZQe9kHQHN0Qo

To claim this, I am signing this object:

@ghadishayban
ghadishayban / fuse.clj
Last active January 26, 2019 04:13
"stream fusion" example using transducers
;; lots of garbage
;;
(->> coll ;; original collection
(map #(assoc % :foo :bar)) ;; intermediate Chunked collection 1
(filter :baz) ;; intermediate Chunked collection 2
(map :feature) ;; intermediate Chunked collection 3
(into [])) ;; reduction using #'conj over Chunked collection #3
;; -- direct reduction using transducers --
@ghadishayban
ghadishayban / sawtooth_signing.clj
Created July 19, 2017 20:33
sawtooth / bitcoin compact signature generation
(ns sawtooth-signing
(:import org.bitcoinj.core.ECKey
org.bitcoinj.core.DumpedPrivateKey
org.bitcoinj.core.Sha256Hash
org.bitcoinj.core.Utils
org.bitcoinj.params.MainNetParams))
(def ^{:doc "generates an in-memory priv key representation from the .wif"}
wif->key
(let [MAINNET (MainNetParams/get)]
@ghadishayban
ghadishayban / map-templates.diff
Created April 29, 2017 21:48
map template code
diff --git a/src/jvm/clojure/lang/BootstrapMethods.java b/src/jvm/clojure/lang/BootstrapMethods.java
index 6eb04ee8..7b163bf3 100644
--- a/src/jvm/clojure/lang/BootstrapMethods.java
+++ b/src/jvm/clojure/lang/BootstrapMethods.java
@@ -1,56 +1,113 @@
package clojure.lang;
import java.util.Arrays;
import java.lang.invoke.CallSite;
import java.lang.invoke.ConstantCallSite;