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
(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
(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
(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
(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
(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
(ns metar.core | |
(:require [clojure.zip :as z] | |
[clojure.xml :as x] | |
[cheshire.core :as j])) | |
(defn metar-zipper | |
[url] | |
(-> url x/parse z/xml-zip z/children)) | |
(defn tag-kv |
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
[{:metar_type "METAR", | |
:longitude "-155.05", | |
:elevation_m "12.0", | |
:wind_dir_degrees "40", | |
:raw_text | |
"PHTO 311953Z 04008KT 10SM SCT029 BKN040 BKN049 27/19 A3016 RMK AO2 SLP210 T02670194", | |
:quality_control_flags {:auto_station "TRUE"}, | |
:latitude "19.72", | |
:observation_time "2013-07-31T19:53:00Z", | |
:flight_category "VFR", |
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 x-group-by | |
([f g coll] | |
(persistent! | |
(reduce | |
(fn [ret x] | |
(let [k (f x)] | |
(assoc! ret k (g (get ret k) x)))) | |
(transient {}) coll))) | |
([f coll] | |
(x-group-by f #(conj (or % []) %2) coll))) |
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 animals [{:name "Betsy" :type :cow} | |
{:name "Murmur" :type :cat} | |
{:name "Lessie" :type :dog} | |
{:name "Dingo" :type :dog} | |
{:name "Rosie" :type :cat} | |
{:name "Rex" :type :dog} | |
{:name "Alf" :type :cat}]) | |
(def group-by-uc-name (group-by-with #(conj (or % []) (.toUpperCase (:name %2))))) |