Skip to content

Instantly share code, notes, and snippets.

@leonoel
leonoel / primassign.clj
Last active December 7, 2017 15:31
primitive field assignment
(import (clojure.asm Type ClassWriter Opcodes)
(clojure.asm.commons Method GeneratorAdapter)
(clojure.lang DynamicClassLoader Compiler$HostExpr))
(require '[clojure.string :as s])
(defn- struct-bytecode [cname slots]
(let [objtype (Type/getType ^Class Object)
cv (new ClassWriter (. ClassWriter COMPUTE_MAXS))]
(. cv (visit (. Opcodes V1_5) (+ (. Opcodes ACC_PUBLIC) (. Opcodes ACC_FINAL))
@leonoel
leonoel / asyncawait.clj
Created December 6, 2018 20:17
cloroutine-based async/await
(ns asyncawait
(:refer-clojure :exclude [await])
(:require [cloroutine.core :refer [cr]]))
(defmacro async [& body]
`(js/Promise. (fn [s# f#]
(spawn (cr {await thunk}
(try (s# (do ~@body))
(catch :default e# (f# e#))))))))
@leonoel
leonoel / deps.edn
Created February 18, 2019 20:17
happy eyeballs
{:paths ["."]
:deps {missionary {:mvn/version "a.3"}}}
@leonoel
leonoel / deps.edn
Created February 28, 2019 11:47
Injure implementation of the toy stores & factories example from https://docs.racket-lang.org/guide/units.html
{:paths ["."]
:deps {injure {:mvn/version "1"}}}
@leonoel
leonoel / goto.clj
Last active March 15, 2019 09:57
CL's tagbody in clojure
(ns goto)
(defmacro tagbody [& forms]
(let [start (gensym)]
(loop [binds []
point start
forms forms]
(let [[body [jump & forms]] (split-with (complement symbol?) forms)
binds (conj binds `(~point [] ~@body ~jump))]
(if jump
@leonoel
leonoel / pump.clj
Last active July 6, 2023 17:13
functional reactive petroleum
;; missionary solution to petrol pump example by Stephen Blackheath and Anthony Jones, ISBN 978-1633430105
;; huanhulan's demo : [live](https://huanhulan.github.io/petrol_pump), [code](https://github.com/huanhulan/petrol_pump)
(ns pump
(:refer-clojure :exclude [first])
(:require [missionary.core :as m])
(:import missionary.Cancelled))
(defn rising
"A transducer that outputs `nil` when the input switches from logical false to logical true."
@leonoel
leonoel / amb.clj
Last active September 15, 2021 06:48
SICP amb
;; missionary implementation of SICP's [amb](http://sarabander.github.io/sicp/html/4_002e3.xhtml#g_t4_002e3)
(ns amb
(:refer-clojure :exclude [require eval])
(:require [missionary.core :as m]))
(deftype FlowIterator [^:unsynchronized-mutable iterator
^:unsynchronized-mutable pending?]
clojure.lang.IFn
(invoke [this]
@leonoel
leonoel / complex_business_process_example_missionary.clj
Last active October 12, 2023 11:16
An alternative solution to didibus' business process using missionary.
(ns complex-business-process-example-missionary
"A stupid example of a more complex business process to implement as a flowchart."
(:require [missionary.core :as m])
(:import missionary.Cancelled))
;;;; Config
(def config
"When set to :test will trigger the not-boosted branch which won't write to db.
When set to :prod will trigger the boosted branch which will try to write to the db."
@leonoel
leonoel / parallel_processing.clj
Last active October 30, 2022 01:25
Parallel processing
;; Experiment - parallel processing using missionary primitives.
;; Inspired by Rx's parallel 'rails' :
;; http://reactivex.io/RxJava/3.x/javadoc/io/reactivex/rxjava3/parallel/ParallelFlowable.html
;; https://dzone.com/articles/rxjava-idiomatic-concurrency-flatmap-vs-parallel
(ns parallel-processing
(:require [missionary.core :as m]))
(defn map-task [f >x]
(m/ap (m/? (f (m/?> >x)))))
@leonoel
leonoel / cloroutine-perf.clj
Created April 16, 2022 19:10
cloroutine performance
(ns ^{:doc "
Performance measurements for various generator implementations, cloroutine v10 https://github.com/leonoel/cloroutine
Based on https://clojureverse.org/t/how-to-transform-nested-map-into-flat-sequence-of-path-value-pairs/8801/22
"} cloroutine-perf
(:require [cloroutine.core :refer [cr]]))
(def ^:dynamic *tail*)
(defn gen-seq-dv [gen]
(lazy-seq (binding [*tail* (gen-seq-dv gen)] (gen))))