Skip to content

Instantly share code, notes, and snippets.

@ChrisBlom
ChrisBlom / datomic-cdc.clj
Last active March 22, 2024 11:53
Datomic change data capture
(ns datomic-cdc.core
"proof-of-concept that shows how setup change-data-capture for datomic"
(:require [datomic.api :as d]))
(def processed-t- (atom nil))
(defn start-cdc-thread
"starts a new thread to processes all past transactions starting at start-t, then continues processing incoming transactions, using the provided `change-handler`
`change-handler` must be a function that takes a single map argument with
@ChrisBlom
ChrisBlom / Cached
Created September 29, 2023 09:30
how to wrap a function in Kotlin so that is invoked at most once in a duration for each argument
import com.github.benmanes.caffeine.cache.Caffeine
import com.github.benmanes.caffeine.cache.Ticker
import io.kotest.matchers.shouldBe
import org.junit.jupiter.api.Test
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.atomic.AtomicLong
import kotlin.time.Duration
import kotlin.time.Duration.Companion.seconds
import kotlin.time.toJavaDuration
@ChrisBlom
ChrisBlom / TimeGauge.kt
Created May 24, 2022 11:58
wrapper to create a easy to use timegauge
import io.micrometer.core.instrument.MeterRegistry
import io.micrometer.core.instrument.Tag
import java.time.Instant
import java.util.concurrent.TimeUnit
import java.util.concurrent.atomic.AtomicLong
import java.util.function.ToDoubleFunction
fun MeterRegistry.timeGaugeMillis(name: String, tags: Iterable<Tag>, initialValue: Long = 0): (Instant) -> Unit {
val a = AtomicLong(initialValue)
a.apply {
(let [l [0 0 1 1 1 14 5 13 6 3 ]
m [0.01 0.02 0.05 0.1 0.2 0.5 1 2 5 10 20 50 100]
subtotals (map * m l)
total (reduce + subtotals)]
(doseq [[munt totaal] (sort-by key > (zipmap m subtotals))]
(printf "%s\t%.2f%n" munt (float totaal)))
total)
(defun add-clj-format-before-save ()
(interactive)
(add-hook 'before-save-hook
'cider-format-buffer
t
t))
(add-hook 'clojure-mode-hook 'add-clj-format-before-save)
(defn invert
"inverts a graph represented as a map from source to target nodes"
[graph]
(reduce-kv (fn [acc src trgs]
(if (seq trgs)
(reduce (fn [acc- trg]
(update acc- trg (fnil conj #{}) src))
acc
trgs)
;; to preserve nodes without edges
@ChrisBlom
ChrisBlom / gist:a061a966eaf431f34dd6537c500920dd
Created October 10, 2016 14:44 — forked from sebsto/gist:19b99f1fa1f32cae5d00
Install Maven with Yum on Amazon Linux
sudo wget http://repos.fedorapeople.org/repos/dchen/apache-maven/epel-apache-maven.repo -O /etc/yum.repos.d/epel-apache-maven.repo
sudo sed -i s/\$releasever/6/g /etc/yum.repos.d/epel-apache-maven.repo
sudo yum install -y apache-maven
mvn --version
;; Toggle a counter that tracks how manny times a function is called
(defn instrument-fn [f]
(let [counter (atom 0)]
(-> (fn [& args] ;; NOTE could unroll for 1-n args for speed
(swap! counter inc)
(apply f args))
(with-meta
{:uninstrumented f
:counter counter}))))
(defmacro eval-with-bindings [bindings form]
(assert (even? (count bindings)) "an even number of forms in binding vector")
(let [binding-pairs (partition 2 bindings)
args (mapv first binding-pairs)
vals (map second binding-pairs)]
`((eval (fn ~args ~form))
~@vals)))
(let [foo inc]
(eval '(foo 1)))