Skip to content

Instantly share code, notes, and snippets.

@piranha
piranha / sse.clj
Created March 27, 2025 11:51
http-kit sse
(ns something.core.sse
(:require [charred.api :as json]
[hiccup2.core :as hi]
[org.httpkit.server :as httpd]
[clojure.tools.logging :as log]))
(def ^:dynamic *report-ch* nil)
(declare sse!)
@piranha
piranha / co
Last active February 18, 2025 09:30
git branch checkout/delete via fzf
#!/bin/sh
set -eo errexit
sep=' '
# NOTE: $1 should not intersect with real branch names
case "$1" in
--LIST)
refname='%(refname:short)'
@piranha
piranha / standalone.clj
Last active January 20, 2025 12:28
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))