Skip to content

Instantly share code, notes, and snippets.

View alexpw's full-sized avatar

Alex Walker alexpw

View GitHub Profile
@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)]
(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: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)))
(def notes {:C 0
:C# 1
:D 2
:D# 3
:E 4
:F 5
:F# 6
:G 7
:G# 8
:A 9
@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)))]
@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 / gist:6125634
Last active December 20, 2015 11:48
parse metar xml
(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
@alexpw
alexpw / gist:6125499
Last active December 20, 2015 11:39
metar data -- pprint
[{: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",
@alexpw
alexpw / gist:4350075
Created December 21, 2012 01:24
Merged implementation.
(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)))
(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)))))