Skip to content

Instantly share code, notes, and snippets.

@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++) {
@piranha
piranha / пше
Last active March 25, 2023 07:41
пше гит, прошу пана
#!/usr/bin/env python
# -*- mode: python, coding: utf-8 -*-
#
# This incredible piece of code makes git a bit Polish, a bit Western Ukrainian,
# пше прошу пана
# Joke is based on fact that 'git' is 'пше' in qwerty/йцукен layouts
#
# (c) 2013 Alexander Solovyov under terms of WTFPL
import sys
@piranha
piranha / mixin-vs-dec.py
Created May 28, 2011 09:30
Mixin vs class decorator
import logging, timeit
class LoggedSetItemMixin(object):
def __setitem__(self, index, value):
logging.info('Setting %r to %r' % (index, value))
super(LoggedSetItemMixin, self).__setitem__(index,value)
class LoggingDict(LoggedSetItemMixin, dict):
pass
:headers = <<
Content-Type: application/json
#
# what indices are there
GET http://localhost:9200/_cat/indices
# delete product
DELETE http://localhost:9200/product
@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 / 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))