Skip to content

Instantly share code, notes, and snippets.

@Chouser
Chouser / protocols.mal
Created February 21, 2015 18:20
A sketch of Clojure-like protocols, implemented in Mal
;; A sketch of Clojure-like protocols, implemented in Mal
;; Mal is https://github.com/kanaka/mal
(def! builtin-type (fn* [obj]
(cond
(list? obj) :mal/list
(vector? obj) :mal/vector
(map? obj) :mal/map
(symbol? obj) :mal/symbol
(keyword? obj) :mal/keyword
#!/usr/bin/env bash
# Use this share-session script in your tmux config to ease multi-user pairing,
# like this:
# set-option -g status-interval 5
# set-option -g status-right '#(/home/chouser/repos/tmux-multiuser/share-session go+rwx go+rw) | "#{pane_title}" #(date +"%H:%M %d-%b-%Y")'
# Set socket dir and socket permissions
chmod "$1" "${TMUX%/*}"
chmod "$2" "${TMUX%%,*}"
package mypkg;
public class Ugly {
public Ugly(){}
public String foo(boolean i) { return "bool: " + i; }
public String foo(Object o) { return "obj: " + o; }
}
@Chouser
Chouser / malli_play.clj
Created February 3, 2023 04:54
Playing around with specs in malli
(ns us.chouser.malli-play
(:require [malli.core :as m]
[malli.error :as me])
(:import (clojure.lang ExceptionInfo)))
;; {:deps {metosin/malli {:mvn/version "0.10.1"}}}
;; No refinement chaining yet
;; No clear separation of constraint violation from runtime error
(defn var-sym [v]
@Chouser
Chouser / filter-with-context.clj
Created January 29, 2023 23:19
A handful of implementations of "grep -C"
;; primitive lazy-seq
(defn filter-with-context-ls [pred n coll]
(let [step (fn step [buffer suffix coll]
(lazy-seq
(when-let [[v & more] (seq coll)]
(if (pred v)
(concat (take-last n buffer) [v] (step [] n more))
(if (pos? suffix)
(cons v (step [] (dec suffix) more))
(step (conj buffer v) 0 more))))))]
(ns us.chouser.merge-sort
"Experiments in parallel folding of vectors"
(:require [clojure.core.reducers :as r]
[criterium.core :as c]
[dom-top.core :refer [loopr]]))
(set! *warn-on-reflection* true)
(defn array-type
"Return a string representing the type of an array with dims
@Chouser
Chouser / experiment.clj
Created April 22, 2022 00:49
core async reseting experiment
(ns io.chouser.experiment
(:require [clojure.core.async :as async]))
(defn bang []
(future
(dotimes [i 10]
(async/go
(print (str "start " i "\n")) (flush)
(Thread/sleep 20000)
(prn :done i)))))
; STM history stress-test
(defn stress [hmin hmax]
(let [r (ref 0 :min-history hmin :max-history hmax)
slow-tries (atom 0)]
(future
(dosync
(swap! slow-tries inc)
(Thread/sleep 200)
@r)
@Chouser
Chouser / mapreduce.js
Created May 5, 2011 21:50
Making CouchDB map/reduce a little less nutty...
// This is the map function to give to CouchDB:
function(doc) {
function xemit(key, val) {
emit(key, {key: key, val: val});
}
// Your code goes here, working with the doc provided.
// Use xemit(key, value) instead of CouchDB's emit
// ...
@Chouser
Chouser / array_type.clj
Created September 22, 2015 14:27
Clojure array type hint
(defn array-type
"Return a string representing the type of an array with dims
dimentions and an element of type klass.
For primitives, use a klass like Integer/TYPE
Useful for type hints of the form: ^#=(array-type String) my-str-array"
([klass] (array-type klass 1))
([klass dims]
(.getName (class
(apply make-array
(if (symbol? klass) (eval klass) klass)