Skip to content

Instantly share code, notes, and snippets.

@Chouser
Chouser / externs_for_cljs.clj
Created June 17, 2013 13:44
Generate an externs.js file for arbitrary JS libraries for use in advanced Google Closure compilation, based on the ClojureScript code that uses the libraries.
(ns n01se.externs-for-cljs
(:require [clojure.java.io :as io]
[cljs.compiler :as comp]
[cljs.analyzer :as ana]))
(defn read-file [file]
(let [eof (Object.)]
(with-open [stream (clojure.lang.LineNumberingPushbackReader. (io/reader file))]
(vec (take-while #(not= % eof)
(repeatedly #(read stream false eof)))))))
@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)
(def ^:dynamic *err-info* nil)
(defmacro with-error-data [data & body]
`(let [old-data# *err-info*]
(when *err-info*
(set! *err-info* (merge *err-info* ~data)))
(let [result# (do ~@body)]
;; if no exception was thrown above, revert *err-info*:
(when *err-info*
(set! *err-info* old-data#))
https://4clojure.oxal.org/#/problem/117
; 227
#(loop [[[y x] & q] (for [[y r] (map list (range) %)
[x c] (map list (range) r)
:when (= \M c)]
[y x])
b (vec (map vec %))]
(if x
(if (= \C ((b y) x))
true
@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