This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn smearfle | |
"\"Smearing shuffle\": shuffle a list while semi-preserving its original order. | |
Slide a window of size `window-size` (default 3) across the list from left to right | |
in increments of `step-size` (default 1), shuffling the contents of the window on each step. | |
The end result: some items are carried a random distance left or right of their | |
original starting point, but disruption of the list's order is largely local, | |
so it's unlikely that any given item is (for instance) carried all the way | |
from the beginning to the end of the list. | |
You can use this to add some variation to the order in which a sorted list is traversed, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn scale | |
"Converts the number `x` from the scale `[old-min old-max]` to the scale | |
`[new-min new-max]`." | |
[x [old-min old-max] [new-min new-max]] | |
(let [old-range (- old-max old-min) | |
new-range (- new-max new-min)] | |
(+ (/ (* (- x old-min) new-range) old-range) new-min))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; 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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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)))) |
NewerOlder