Skip to content

Instantly share code, notes, and snippets.

@piranha
piranha / standalone.clj
Last active December 5, 2024 10:04
Single-file Clojure script with dependencies
#!/usr/bin/env clojure -M
;;; setup
;; this classloader stuff is needed so that `add-lib` sees the DynamicClassLoader that's used by `require`
(.setContextClassLoader (Thread/currentThread) (clojure.lang.RT/baseLoader))
(require '[clojure.repl.deps :as deps])
(binding [clojure.core/*repl* true]
(deps/add-lib 'http-kit {:mvn/version "2.8.0"}))
;;; actual script
@piranha
piranha / cond+.clj
Last active April 4, 2024 16:18
cond+
(defmacro cond+ [& clauses]
(when-some [[test expr & rest] clauses]
(condp = test
:do `(do ~expr (cond+ ~@rest))
:let `(let ~expr (cond+ ~@rest))
:some `(or ~expr (cond+ ~@rest))
`(if ~test ~expr (cond+ ~@rest)))))

What

The world needs another utility lib for Clojure, since the existing ones are seldom accepting new stuff in. Ideally it would have an understandable rules how to add more stuff in, so that it's utility will grow over time.

What's not to replace

  • hashp is almost perfect (?), only needs (defmacro p [form] (p* form)) to call it in threading pipelines

Ideas to bring in

@piranha
piranha / ghost.restclient
Last active June 23, 2023 08:52
Making requests to Ghost from restclient.el (or generating JWT in elisp, whatever)
# use with https://github.com/pashky/restclient.el
:admin := <<
(let* ((key "649484dc581803f494a07f40:dbee81202550e7afe5a022405d8b5b10da9f24d18883c92d5331bace31f6796d")
(bits (split-string key ":"))
(jwt-header (base64url-encode-string
(json-serialize (list :alg "HS256"
:typ "JWT"
:kid (car bits)))))
(payload (base64url-encode-string
@piranha
piranha / recdescent.js
Created June 11, 2023 16:48
Simple recursive descent in JavaScript
const test = require('node:test');
const assert = require('node:assert').strict;
let RE = /[\s,()]/;
function tokenize(s) {
var tokens = []
let j = 0;
for (var i = 0; i < s.length; i++) {
:headers = <<
Content-Type: application/json
#
# what indices are there
GET http://localhost:9200/_cat/indices
# delete product
DELETE http://localhost:9200/product
@piranha
piranha / encrypt.clj
Last active March 11, 2020 12:46
Simple encryption/decryption in Clojure
(ns encrypt
(:import [javax.crypto Cipher]
[javax.crypto.spec SecretKeySpec]
[java.security MessageDigest]
[java.util Base64 Base64$Encoder Base64$Decoder]))
(def SECRET (or (System/getenv "SECRET")
(binding [*out* *err*]
(print "\nWARNING: set 'SECRET' env variable to be secure\n\n")
@piranha
piranha / hex.clj
Created March 11, 2020 12:33
hex and unhex in clojure
(defn hex [ba]
(->> (map #(format "%02x" %) ba)
(apply str)))
(defn unhex [s]
(->> (partition 2 s)
(map #(Integer/parseInt (apply str %) 16))
byte-array))
@piranha
piranha / async-profiler-mw.clj
Created February 5, 2019 10:38
Async Profile Middleware
(defn make-profile-transform [^String thread-name]
(fn [^String s]
(when (> (.indexOf s thread-name) -1)
(-> s
(str/replace #"com.fasterxml.jackson.+" "JACKSON...")
(str/replace #"org.elasticsearch.client.RestClient.+" "ES request...")
(str/replace #"clojure.tools.logging/.+" "LOG...")))))
(defn profiler-mw [handler]
@piranha
piranha / cx.clj
Last active February 2, 2018 17:51
(defmacro cx [& classes]
"This macro compiles this:
(cx :one true :two true :three false :four (pos? 1))
Into this:
[ \"one two\", 1 > 0 ? \"four\" : null ].join(\" \")
"
(let [class-map# (partition 2 classes)
groups# (group-by (fn [[k v]]