Skip to content

Instantly share code, notes, and snippets.

View deps.edn
{: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
View README.md
View lsp-unison.el
(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"
View json_schema.clj
(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
View build-modules.clj
(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
View thunk.clj
(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?
View multi-what.clj
(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
View defrecord-star.clj
(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
View slide-perf.clj
;; 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
View async-async-middleware.clj
(defn wrap-async
([af on-success on-failure]
(wrap-async af identity on-success on-failure))
([af before on-success on-failure]
(fn -wrap [-next]
(fn -async [ctx respond raise]
(let [ctx (before ctx)]
(letfn [(-on-success [result] (-next (on-success ctx result)) respond raise)
(-on-failure [?error] (raise (on-failure ctx ?error)))]
(af ctx -on-success -on-failure)))))))