Skip to content

Instantly share code, notes, and snippets.

@mkremins
mkremins / zip.clj
Last active August 29, 2015 13:55
Clojure utility fns for working with zippers
(require '[clojure.zip :as z])
(defn forms-zip
"Returns a clojure.zip zipper over a tree of Clojure forms."
[form]
(z/zipper coll?
#(if (map? %) (interleave (keys %) (vals %)) (seq %))
#(cond
(map? %1) (apply hash-map %2)
(seq? %1) %2
@mkremins
mkremins / opts.clj
Created April 24, 2014 19:17
Clojure: parse sequential opts delimited by a known set of names
(defn parse-opts [ks aseq]
(let [ks (set ks)]
(loop [last-k nil
opts {}
frontier aseq]
(if-let [value (first frontier)]
(if (ks value)
(recur value opts (rest frontier))
(recur last-k (update-in opts [last-k] (fnil conj []) value) (rest frontier)))
opts))))
@mkremins
mkremins / partial_eval.clj
Last active August 29, 2015 14:02
Compile fn from expr containing unbound symbols
(ns expr-fn.core
(:require [clojure.walk :as walk]))
(defn forms-seq [form]
(tree-seq coll?
#(if (map? %) (interleave (keys %) (vals %)) %)
form))
(defn resolved? [form]
(if (coll? form)
@mkremins
mkremins / defntraced.clj
Created July 22, 2014 21:35
Define traced function
; http://www.reddit.com/r/Clojure/comments/2behsz/clojurescript_debugging_tips/cj4mvok
(defmacro defntraced
"Define a function with it's inputs and output logged to the console."
[sym & body]
(let [[_ _ [_ & specs]] (macroexpand `(defn ~sym ~@body))
new-specs
(map
(fn [[args body]]
(let [prns (for [arg args]
@mkremins
mkremins / weighted_choice.clj
Created October 8, 2014 20:04
Weighted random choice
(defn weighted-choice
"Given a map `choices {outcome weight}`, returns a randomly chosen `outcome`.
The likelihood that a particular `outcome` will be chosen is proportional to
its assigned `weight`."
[choices]
(->> choices
(mapcat (fn [[outcome weight]] (repeat weight outcome)))
rand-nth))
@mkremins
mkremins / core_defs.cljs
Created November 27, 2014 22:21
Inspect cljs.core defs in CLJS
(def replacements
[["_PLUS_" \+] ["_STAR_" \*] ["_SLASH_" \/] ["_QMARK_" \?] ["_BANG_" \!]
["_LT_" \<] ["_GT_" \>] ["_EQ_" \=] ["_DOTDOT_" ".."] ["_" \-]])
(defn unmunge [s]
(reduce (fn [s [before after]]
(clojure.string/replace s before after))
s replacements))
(defn alphabetical? [c]
@mkremins
mkremins / parse_arglist.clj
Created September 4, 2015 03:14
Parse arglists for fn-like forms
(def valid-name?
"Returns truthy if argument is a non-namespaced symbol."
(every-pred symbol? (complement namespace)))
(defn parse-arglist
"Given an `arglist` (a vector of parameter names for a function method),
returns a map of information about the method. May throw an exception if
`arglist` cannot be parsed."
[arglist]
(assert (every? valid-name? arglist)
@mkremins
mkremins / let_log.clj
Created September 9, 2015 16:27
Like clojure.core/let, but logs the value of every bound symbol
(defn indent [n s]
(let [spaces (clojure.string/join (repeat n " "))]
(->> (clojure.string/split-lines s)
(map (partial str spaces))
(clojure.string/join "\n"))))
(defn bound-syms [pat]
(condp #(%1 %2) pat
(some-fn map? vector?) (distinct (mapcat bound-syms pat))
symbol? [pat]
@mkremins
mkremins / complete.elm
Created September 29, 2015 17:19
Text completions with match highlighting
import Debug
import List
import String
type alias Match = {full: String, before: String, match: String, after: String}
score : Match -> Int
score {before} = String.length before
match : String -> String -> Maybe Match
@mkremins
mkremins / gist:5818692
Created June 19, 2013 22:27
Bash: pretty-print currently available color codes
#!/bin/bash
#
# This file echoes a bunch of color codes to the
# terminal to demonstrate what's available. Each
# line is the color code of one forground color,
# out of 17 (default + 16 escapes), followed by a
# test use of that color on all nine background
# colors (default + 8 escapes).
#
# Shamelessly stolen from: