Skip to content

Instantly share code, notes, and snippets.

@leonoel
leonoel / promise.clj
Created October 23, 2023 07:17
missionary promise interop with abort
(ns promise)
(defmacro with-abort "
Returns a task evaluating `body` with symbol `a` bound to a fresh `AbortSignal` that is aborted when the task is
cancelled. The task completes with the result of returned promise.
" [a & body]
`(fn [s# f#]
(let [c# (js/AbortController.)
~a (.-signal c#)]
(try (.then (do ~@body) s# f#)
@leonoel
leonoel / animation.clj
Created October 8, 2023 13:16
animation in missionary
(ns animation
(:refer-clojure :exclude [cycle])
(:require [missionary.core :as m]))
;; A signal giving the current time in milliseconds.
;; On the browser, you may want to use an RAF-based clock instead.
(def <time
(->> (m/ap
(loop []
(m/amb nil
@leonoel
leonoel / prepl.clj
Last active February 21, 2024 16:47
Example of clojure prepl usage with missionary
(ns prepl
(:require [clojure.core.server :as s]
[missionary.core :as m])
(:import (java.io PipedReader PipedWriter)
(java.util.concurrent Executors)))
(defn remote "
Returns a function taking a `java.io.PipedWriter` and returning a flow connecting to a remote prepl on given `host` and
`port`, sending the content of the pipe to the remote peer and emitting received evaluation results. The pipe is closed
on flow cancellation.
@leonoel
leonoel / deps.edn
Created December 30, 2022 10:30
Missionary implementation of kafka consumer with backpressure and manual offset commits
{:deps {org.clojure/clojure {:mvn/version "1.11.1"}
org.apache.kafka/kafka-clients {:mvn/version "3.3.1"}
missionary/missionary {:mvn/version "b.26"}}}
@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))))
@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 / 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 / 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 / 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 / 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