Skip to content

Instantly share code, notes, and snippets.

@shaunlebron
shaunlebron / css.cljc
Created November 19, 2026 20:10
css mounting
(defn mount-css
"Compile the given rules into a head-level <style> tag, if not yet mounted for `k`."
[k & rules]
#?(:cljs
(let [id (name k)
e (j/call js/document :getElementById id)] ;; NOTE: browsers make ID lookup very fast
(when-not e
(println "adding styles for" k)
(let [e (j/call js/document :createElement "style")]
(j/assoc! e :id id)
@shaunlebron
shaunlebron / clj-to-sh.md
Last active November 6, 2026 19:22
Clojure functions in the shell

Clojure functions in the shell

Tables by Anthropic's Claude Opus 5.5.

my goal is to learn shell commands that map to common clojure ones, since the sort | uniq -c surprised me that frequencies could be done in shell

In the examples, xs is a sequence of lines, s is a single string, and re is a regex.

Selecting lines

Clojure ->css

A small alternative to the Garden CSS compiler that maps to modern CSS:

  1. It compiles to native nested rules.
  2. [:a :b :c {…}] compiles to a b c {…} not a, b, c {…}.
  3. Its grammar is flexible enough to allow arbitrary @-rules.
(defmacro defn* [name & fdecl]
(let [[docs impl] (split-with (some-fn symbol? string? map?) fdecl)]
(letfn [(wrap [[bindings & body]]
(let [[condition-map & body] (if (and (map? (first body))
(> (count body) 1))
body
(cons nil body))]
`(~bindings
~condition-map
;; TODO: do what you need here if you want to wrap the body
(ns shaun.lib.head-retention
"Provides a `let-drain` macro for when we need to dangerously create a binding for a
giant sequence. If we tag the consumption point with ^:draining, at
compile-time it will catch any head retention caused by symbolic references.
Non-problematic references can be manually tagged with ^:releasing to make the
checker pass. It won't catch subtler cases that we likely won't see, but it
will at least communicate that we are trying to avoid OOM in critical areas."
(:require
[clojure.walk :refer [macroexpand-all]]
[schema.core :as s]))
@shaunlebron
shaunlebron / reagent-flash.cljs
Last active March 26, 2026 19:18
Flashing intermediate changes inside a render batch in Reagent
(ns example.reagent-flash
(:require
[reagent.core :as r]
[reagent.ratom :as ra]
))
(defn- resolve-ratom [r* path]
(cond
(instance? Atom r*) [r* path]
(instance? ra/RAtom r*) [r* path]
(ns example.touch-surface
(:require
[applied-science.js-interop :as j]
[reagent.core :as r]
[reagent.hooks :as hooks]))
(defn with-nonpassive-event-listeners
"TODO: remove this when React supports non-passive event listeners:
https://github.com/facebook/react/issues/22794
@shaunlebron
shaunlebron / safe-navigation-clojure.md
Last active November 5, 2025 21:31
Safe Navigation in Clojure

Safe Navigation in Clojure

safe-> is a safe navigation macro for Clojure. It extends some-> with additional guards:

  1. doesn’t call nil values
  2. doesn’t call missing methods (ClojureScript only)

#!/bin/bash
# Cheatsheet for remembering Bash rules for Parameter Substitution:
# https://tldp.org/LDP/abs/html/parameter-substitution.html#PSUB2
# Fallback for unset vars
# Bash #——> like JavaScript
${a+b} #——> a && b