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
(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))) | |
{} |
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
(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)))] |
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 notes {:C 0 | |
:C# 1 | |
:D 2 | |
:D# 3 | |
:E 4 | |
:F 5 | |
:F# 6 | |
:G 7 | |
:G# 8 | |
:A 9 |
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 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))) |
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 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))))))))) |
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 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)] |
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
(mapcat (fn [[k coll]] (map vector (repeat k) coll)) | |
[[:a [1 2]] [:b [3 4]]]) | |
=> '([:a 1] [:a 2] [:b 3] [:b 4]) |
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
/** | |
* 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)); |
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
<?php | |
class F | |
{ | |
public static $fns = [ | |
'apply' => 'call_user_func_array', | |
'min' => 'min', | |
]; | |
public static function __callStatic($method, $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
(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 |