Skip to content

Instantly share code, notes, and snippets.

@bsless
bsless / ex.clj
Created February 3, 2024 10:55
Compare and contrast unclebob/functor ur example and different implementation tradeoffs
;; Baseline
(defn encrypt-if-direct-message [content tags]
(if (re-find #"^D \#\[\d+\]" content)
(let [reference-digits (re-find #"\d+" content)
reference-index (Integer/parseInt reference-digits)
p-tag (get tags reference-index)]
(if (nil? p-tag)
[content 1]
(let [recipient-key (hex-string->num (second p-tag))
{:paths ["src" "resources"]
:deps {org.clojure/clojure {:mvn/version "1.11.1"}
metosin/reitit {:mvn/version "0.6.0"}
info.sunng/ring-jetty9-adapter {:mvn/version "0.30.0"}
http-kit/http-kit {:mvn/version "2.7.0"}}
:aliases
{:dev {:jvm-opts ["--enable-preview"]}
:run {:exec-fn server/go
:exec-args {}}}}
@bsless
bsless / README.md
Created August 31, 2023 12:44
Clojure app startup performance

Why

Simple experiment to test the effects of different techniques and options on application start up time

Class Data Sharing (CDS)

The goal of CDS is to reduce the startup time of the JVM by loading from a pre-processed archive of Java classes and JVM metadata that is used during the initialization process. https://dev.java/learn/jvm/cds-appcds/

Clojure compiler options

see

Meta elision

(require 'lsp)
(defcustom-lsp lsp-unison-lsp-port 5757
"The port of a running UCM language server. You can overwride thise by setting the UNISON_LSP_PORT environment variable when running UCM."
:type 'number
:group 'lsp-unison
:package-version '(lsp-mode . "8.0.1")
:lsp-path "unison.lspPort")
(defcustom-lsp lsp-unison-trace-server "off"
(defn qualify-definitions
[d prefix]
(let [p (str prefix ".")
p' (str "#/definitions/" p)]
(into
{}
(map (fn [[k v]]
[(str p k)
(walk/postwalk
(fn [o]
@bsless
bsless / build-modules.clj
Last active April 28, 2022 16:10
Build independent Clojure module as part of same repository
(ns build
(:refer-clojure :exclude [test])
(:require
[clojure.tools.build.api :as b]
[org.corfield.build :as bb]))
(def version (format "0.0.%s" (b/git-count-revs nil)))
(def class-dir "target/classes")
@bsless
bsless / thunk.clj
Created April 22, 2022 14:13
Make thunks of Clojure expressions, can be named
(defmacro $ [& body] `(fn ~'thunk [] ~@body))
;;; best effort to create a named function
(defmacro $
[& body]
(let [name (or (and (sequential? (first body))
(symbol? (ffirst body))
(-> body ffirst name (str "-thunk") symbol))
'thunk)]
`(fn ~name [] ~@body)))
@bsless
bsless / multi-what.clj
Last active November 16, 2021 19:20
What if multimethods were implemented with closures and not dispatch tables?
(defprotocol IDoubleDispatch
(-add [this k f])
(-remove [this k]))
(defmulti ->= type)
(defmethod ->= String [^String x] #(.equals x %))
(defmethod ->= clojure.lang.Keyword [^clojure.lang.Keyword x] #(.equals x %))
(defmethod ->= clojure.lang.Symbol [^clojure.lang.Symbol x] #(.equals x %))
(defmethod ->= Long [^long x]
#(if (int? %) (= x (unchecked-long %)) false))
@bsless
bsless / defrecord-star.clj
Last active November 13, 2021 17:52
defrecord which "proxies" protocols to its first field
(defmacro defrecord*
[-name fields & opts+specs]
(let [p (:proxy (meta -name))
extra (for [protocol p
:let [-ns (namespace protocol)
arg (first fields)
p' @(requiring-resolve protocol)]]
[protocol
(apply
concat
@bsless
bsless / slide-perf.clj
Created October 2, 2021 07:26
Idiomatically refactor Clojure code to improve performance
;; http://johnj.com/from-elegance-to-speed.html
(def times (iterate #(+ % (rand-int 1000)) 0))
(def times-v (into [] (take 1e6) (iterate #(+ % (rand-int 1000)) 0)))
(defn smt-8 [times]
(->> times
(partition 8 1)
(map (juxt identity