Skip to content

Instantly share code, notes, and snippets.

View ghadishayban's full-sized avatar

Ghadi Shayban ghadishayban

View GitHub Profile
@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
;; 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 / 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.
@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 / doseq.clj
Created April 24, 2016 05:19
doseq that doesn't blow up with too much bytecode, and w/o (fn []) thunks
(defmacro doseqs
[seq-exprs & body]
(let [stk (gensym "stack__")
level (gensym "level__")
seq-exprs (partition 2 seq-exprs)
bindings (vec (map first seq-exprs))
init-exprs (vec (map second seq-exprs))
nbinds (count init-exprs)
@ghadishayban
ghadishayban / docker_tag_paginate.clj
Last active December 21, 2016 03:48
unfold example
(ns docker-tag-paginate
(:require [clj-http.lite.client :as client]
[cheshire.core :as json]
[clojure.pprint :refer [print-table]]))
(def base-url "https://registry.hub.docker.com/v2")
;; (progression pos? dec 15)
(defn progression
@ghadishayban
ghadishayban / json.clj
Created October 1, 2015 19:30
Pex JSON grammar
(def JSON '{json [whitespace value EOI]
value (/ string number object array jtrue jfalse jnull)
object [(:ws "{")
(:join [string (:ws ":") value] ",")
(:ws "}")
(action capture-object)]
@ghadishayban
ghadishayban / poolboy.clj
Created March 15, 2015 17:23
Interruptible Channels (Needs JDK8)
(ns poolboy.chanfut
(:import [java.util.concurrent CompletableFuture Callable Future]
[java.util.function BiConsumer]
[java.util.concurrent Executor ExecutorService])
(:require [clojure.core.async :as async]
[clojure.core.async.impl.protocols :as async-impl]))
(def async-executor clojure.core.async.impl.exec.threadpool/the-executor)
(defprotocol Interruptible
@ghadishayban
ghadishayban / XFIterator.java
Created November 6, 2014 03:38
Transducers <3 Iterators
import clojure.lang.RT;
import clojure.lang.IFn;
import clojure.lang.AFn;
import java.util.Iterator;
import java.util.ArrayList;
import java.util.NoSuchElementException;
public class XFIterator implements Iterator {
@ghadishayban
ghadishayban / doseq.clj
Last active August 29, 2015 14:06
doseq expansion
(let*
[c__6072__auto__
(clojure.core.async/chan 1)
captured-bindings__6073__auto__
(clojure.lang.Var/getThreadBindingFrame)]
(clojure.core.async.impl.dispatch/run
(clojure.core/fn
[]
(clojure.core/let
[f__6074__auto__