Skip to content

Instantly share code, notes, and snippets.

@gerritjvv
gerritjvv / mapvals.clj
Created July 27, 2020 13:46
Apply a function walking multiple levels of maps and vectors
(require '[clojure.walk :refer [postwalk]])
(defn mapvals [f m]
(let [v-f (fn [v]
(cond
(map? v) v
(coll? v) (into (empty v) (map f) v)
:else (f v)))
@gerritjvv
gerritjvv / in.clj
Created July 8, 2020 10:08
Clojure - finding something in a list
(defn in? [vs ls]
(boolean (some (partial (set vs)) ls)))
(in? [1 2] [3 4 2 5]) => false
(in? [1 2] [3 1 4 2 5]) => true
(defn in? [v ls]
(loop [[x & rs] ls]
(if-not x
@gerritjvv
gerritjvv / prod_config.sh
Created June 23, 2020 09:22
Update variables with a PROD Prefix in bash
# The aim is to support e.g
# URL="dburl" variables and then override at will when PROD_URL is available.
switch_prod.sh
for v in $(env | awk '/PROD_/ {gsub(/PROD_/,""); print $1}'); do
eval "export $v"
done
@gerritjvv
gerritjvv / aws_sqs.clj
Created September 23, 2019 13:03
clojure cognitect aws sqs
;; deps:
;; [com.cognitect.aws/api "0.8.352"]
;; [com.cognitect.aws/endpoints "1.1.11.632"]
;; [com.cognitect.aws/sqs "742.2.519.0"]
;; [mount "0.1.16"]
;; [cheshire "5.8.1"]
(require '[cognitect.aws.client.api :as aws])
(require '[mount.core :as mount)
=== RUN TestNaiveTimesEncryptGCM
Run 0. Did 1000000 iterations in 2.481178232s
Run 1. Did 1000000 iterations in 2.495653698s
Run 2. Did 1000000 iterations in 2.554544033s
Run 3. Did 1000000 iterations in 2.735187669s
Run 4. Did 1000000 iterations in 2.468231834s
Run 5. Did 1000000 iterations in 2.508428888s
Run 6. Did 1000000 iterations in 2.515230338s
Run 7. Did 1000000 iterations in 2.757054756s
Run 8. Did 1000000 iterations in 2.807702375s
@gerritjvv
gerritjvv / javaaesbenchmarks.md
Last active June 17, 2019 07:05
Java JVM AES CBC GCM Benchmarks

Vanilla Java JCE

Benchmark                       Mode  Cnt      Score      Error  Units
AesJce.enc_aes128CbcHmacSha256  thrpt  200  40722.671 ±  739.025  ops/s
AesJce.enc_aes256CbcHmacSha512  thrpt  200  39288.300 ±  979.722  ops/s
AesJce.enc_aes128GCM            thrpt  200  75599.381 ± 1197.385  ops/s

AesJce.dec_aes128CbcHmacSha256  thrpt  200  49830.138 ± 1040.007  ops/s
AesJce.dec_aes256CbcHmacSha512  thrpt  200  51758.272 ±  839.378  ops/s
AesJce.dec_aes128GCM            thrpt  200  76144.372 ± 2626.274  ops/s
@gerritjvv
gerritjvv / cliArgumentsAsMap.java
Created September 27, 2017 23:16
Convert CLI main arguments into a Map for quick no extra lib console java apps
public static Map<String, Object> argumentsAsMap(String[] args){
Map<String, Object> m = new HashMap<>();
for(int i = 0; i < args.length; i++){
String lbl = args[i].trim();
if(lbl.startsWith("-") && i+1 < args.length && !args[i+1].trim().startsWith("-")) {
m.put(removePrefixes(lbl), args[i+1].trim());
i++;
} else {
import posixpath
import argparse
import urllib
import os
from SimpleHTTPServer import SimpleHTTPRequestHandler
from BaseHTTPServer import HTTPServer
class RootedHTTPServer(HTTPServer):
@gerritjvv
gerritjvv / gist:2dc9322f5cd1d29a4505
Created December 9, 2014 12:19
ApplyClojureRecords
(defrecord MyItem [a b c])
(apply ->MyItem ["1" "2" "3"])
;; #user.MyItem{:a "1", :b "2", :c "3"}
(map #(apply ->MyItem %) [ [1 2 3] [4 5 6] [7 8 9]])
;;(#user.MyItem{:a 1, :b 2, :c 3} #user.MyItem{:a 4, :b 5, :c 6} #user.MyItem{:a 7, :b 8, :c 9})
@gerritjvv
gerritjvv / kafka-clj.pool-impl.clj
Created June 15, 2016 17:15
Performance of ConcurrentLinkedQueue + Semaphore vs atom + persistent_vector for queue implementation
(ns
^{:doc "Concurrent keyed pool implementation using ConcurrentHashMap and ConcurrentLinkedQueue"}
kafka-clj.pool-impl
(:use criterium.core)
(:import (java.util.concurrent ConcurrentLinkedQueue ConcurrentHashMap Semaphore ExecutorService Executors TimeUnit)))
;; add [criterium "0.4.4"] to you're project.clj file
;; then use run-test-cc and run-test-a
;;
;; Results for both ConcurrentLinkedQueue + Semaphore and for atom + vector access is the same