Skip to content

Instantly share code, notes, and snippets.

View ghadishayban's full-sized avatar

Ghadi Shayban ghadishayban

  • Charleston, SC
View GitHub Profile
@ghadishayban
ghadishayban / data_accessor.clj
Created September 18, 2016 16:44
better impl of #'clojure.core/bean
(deftype DataWrapper [prop-map obj]
clojure.lang.ILookup
(valAt [_ k]
(when-let [getter (prop-map k)]
(getter obj)))
(valAt [_ k not-found]
(if-let [getter (prop-map k)]
(getter obj)
not-found))
clojure.lang.IKVReduce
@ghadishayban
ghadishayban / productions.clj
Last active November 20, 2021 00:06
unified generators
;;
;; Example usages at the bottom of the file
;;
(defn productions
"Returns a sequence of values by repeatedly calling `produce!` until it
returns `fin`. The sequence can be used lazily/caching or reducible/non-caching.
The arity-2 variant's `produce!` takes no arguments and returns a value
or the terminator.
;; differences from scheme unfold
;; even initial value is lazy
;; predicate sense reversed
;; internal state == produced value, no special mapper-fn
;; no tail-gen
(defn series
"Produces a sequence of values.
`f` is a function that given a value, returns the next value.
`continue?` is a predicate that determines whether to produce
@ghadishayban
ghadishayban / clojure 1.8 bytecode
Last active April 29, 2017 20:53
clojure "map template" using invokedynamic
public static java.lang.Object invokeStatic(java.lang.Object, java.lang.Object, java.lang.Object);
Code:
0: bipush 10
2: anewarray #13 // class java/lang/Object
5: dup
6: iconst_0
7: getstatic #17 // Field const__0:Lclojure/lang/Keyword;
10: aastore
11: dup
12: iconst_1
@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;
@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 / 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 / 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 / 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 <[email protected]>
set -e
if [ -z ${1+x} ]
then
echo provide an asm git sha / ref
exit 1
fi
@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))])