Skip to content

Instantly share code, notes, and snippets.

View alexpw's full-sized avatar

Alex Walker alexpw

View GitHub Profile
@alexpw
alexpw / map-cast2.clj
Last active December 21, 2015 00:48 — forked from geoffeg/map-cast2.clj
(defmulti type-cast (fn [k v] k))
(defmethod type-cast :int [k v] (Integer/parseInt v))
(defmethod type-cast :float [k v] (Float/parseFloat v))
(defmethod type-cast :default [k v] v)
(def input {:int "1", :float ".2", :str "three" :blank ""})
(reduce (fn [xs [k v]]
(assoc xs k (type-cast k v)))
{}
@alexpw
alexpw / badmaps.clj
Created September 12, 2013 15:09 — forked from rbutler/badmaps.clj
(let [qparams {"cat" "meow"}
parammap {"cat" #(println "feline says" %), "dog" #(println "canine says" %)}
foundparam (into {} (filter #(contains? parammap (key %)) qparams))]
((get parammap (key (first foundparam))) (val (first foundparam))))
;> feline says meow
; Cleaner?
(let [queryparams {:cat "meow", :cheese "nonsense"}
functionmap {:cat #(println "feline says" %), :dog #(println "canine says" %)}
selectedkey (first (select-keys functionmap (keys queryparams)))]
(def notes {:C 0
:C# 1
:D 2
:D# 3
:E 4
:F 5
:F# 6
:G 7
:G# 8
:A 9
@alexpw
alexpw / gist:9399593
Created March 6, 2014 21:04
https://ochronus.com/brainfuck-in-clojure-in-40-lines/ - reformatted to be fewer lines (27) with no compromises in readability, and no more 1-line if statements.
(ns bf.core)
(defn bf-interpreter [program]
(letfn [(find-bracket [start-bracket end-bracket idx dir-fn]
(loop [i (dir-fn idx) opened 0]
(condp = (nth program i)
start-bracket (recur (dir-fn i) (inc opened))
end-bracket (if (zero? opened)
i
(recur (dir-fn i) (dec opened)))
(defn partition-by-frame
[coll]
(lazy-seq
(when-let [s (seq coll)]
(let [[x y z :as xyz] (take 3 s)]
(if (= 10 x)
(cons xyz (partition-by-frame (next s)))
(if (= 10 (+ x (or y 0)))
(cons xyz (partition-by-frame (drop 2 s)))
(cons [x y] (partition-by-frame (drop 2 s)))))))))
@alexpw
alexpw / gist:f20c7b3ac858003e07e2
Last active August 29, 2015 14:02
partition-by-seq: Search seqs in seq
(defn partition-by-seq
"Partition coll by splitting each time it encounters sub-seq. The sub-seq
can optionally contain fns, just like partition-by."
[sub-seq coll]
(let [cnt-seq (count sub-seq)
match? (if (some fn? sub-seq)
(let [partials (mapv (fn [f]
(if (fn? f)
#(f %)
#(= f %))) sub-seq)]
(mapcat (fn [[k coll]] (map vector (repeat k) coll))
[[:a [1 2]] [:b [3 4]]])
=> '([:a 1] [:a 2] [:b 3] [:b 4])
@alexpw
alexpw / gist:61619aa5924514d1180a
Last active August 29, 2015 14:05
Example implementation of partial function application and currying in Javascript.
/**
* A partial function applicator.
*/
function partial() {
var fn = arguments[0];
var slice = Array.prototype.slice;
var partialArgs = slice.call(arguments, 1);
return function () {
var args = slice.apply(arguments);
return fn.apply(null, partialArgs.concat(args));
@alexpw
alexpw / gist:c44ff885aadfef5c342e
Created August 28, 2014 05:51
xdebug segmentation fault 11; minimal test case
<?php
class F
{
public static $fns = [
'apply' => 'call_user_func_array',
'min' => 'min',
];
public static function __callStatic($method, $args)
(require '[clojure.string :as s])
(defn bool->int
[bool]
(if bool 1 0))
(defn single-char-days
"Translate two-char representation of certain day to one"
[days]
(-> days