Skip to content

Instantly share code, notes, and snippets.

View llasram's full-sized avatar

Marshall Bockrath llasram

View GitHub Profile
@llasram
llasram / p083.ijs
Last active August 29, 2015 14:23
J implementation of Dijkstra's algorithm on a square grid (Project Euler #83)
mask =: _~:]
moves =: 1 :'(0 1, 0 _1, 1 0,: _1 0) |.!.m"1 2 ]'
step =: [:<./ ], (* +./@:(0 moves)@:mask) +"2 (_ moves)@:]
init =: * $ $!._ 1:
costs =: step^:_ init
M =: ([:".(;._1)',',])(;._1)_1|.(1!:1)<'p083_matrix.txt'
] p83 =: {:{: costs M
@llasram
llasram / zwj.clj
Last active August 29, 2015 14:18 — forked from anonymous/zwj.clj
(let [f‍oo 1, foo 2] [f‍oo foo])
;; => [1 2]
@llasram
llasram / mapify.clj
Last active August 29, 2015 14:13 — forked from kindlychung/mapify.clj
(defn mapify
"Return a seq of maps like {:name \"Edward Cullen\" :glitter-index 10}"
[rows]
(let [headers (map (comp conversions headers->keywords) (first rows))]
(map (partial zipmap headers) (rest rows))))
@llasram
llasram / selmer-67-no-repro.txt
Created December 4, 2014 14:49
Demonstrating failure to reproduce yogthos/Selmer#67
∴ 0 seneca:~/ws/pedestal/samples/template-server git:master
$ git checkout -b aot-no-repro 2edb378
Switched to a new branch 'aot-no-repro'
∴ 0 seneca:~/ws/pedestal/samples/template-server git:aot-no-repro
$ grep selmer project.clj
[selmer "0.7.6"]
∴ 0 seneca:~/ws/pedestal/samples/template-server git:aot-no-repro
$ find target/ -type f -name '*.class'
@llasram
llasram / flat-json.clj
Created October 22, 2014 19:09
Parse giant JSON objects as reducer of key-value pairs.
(ns repubsub.flat-json
(:require [clojure.core.protocols :as ccp]
[clojure.java.io :as io]
[cheshire (core :as json) (parse :as parse) (factory :as factory)])
(:import [com.fasterxml.jackson.core
, JsonParser JsonFactory JsonFactory$Feature JsonGenerator$Feature
, JsonToken]
[java.io
, StringWriter StringReader BufferedReader BufferedWriter
, ByteArrayOutputStream PushbackReader]))
@llasram
llasram / permutron.core.clj
Created September 11, 2014 22:29
Permutations
(ns permutron.core
(:require [clojure.core.protocols :as ccp]
[clojure.core.reducers :as r])
(:import [clojure.lang Seqable Indexed Counted]))
;; Why are these private in clojure.core.reducers, but fjtask is public?
(def ^:private fjinvoke @#'r/fjinvoke)
(def ^:private fjfork @#'r/fjfork)
(def ^:private fjjoin @#'r/fjjoin)
@llasram
llasram / ffilter.clj
Created August 13, 2014 12:51
ffilter
(defn ffilter
"Returns the first item in `coll` for which `(pred item)` is true."
[pred coll] (reduce (fn [_ x] (if (pred x) (reduced x))) nil coll))
(defmacro assoc-keys
"The inverse of the {:keys [...]} binding form -- assoc the keyword form of
each symbol in syms with the bound value of that symbol."
[map & syms] `(assoc ~map ~@(mapcat (juxt keyword identity) syms)))
@llasram
llasram / signvert.clj
Created January 21, 2014 14:52
Signed <-> unsigned JVM type conversion
(def ^:private type-max
{'byte Byte/MAX_VALUE,
'short Short/MAX_VALUE,
'int Integer/MAX_VALUE})
(defn ^:private signed*
[type x]
(let [max (type-max type), roll (->> max long inc (* 2))]
`(~type (let [x# (long ~x)] (if (<= x# ~max) x# (- x# ~roll))))))
(defn parse-nx
"Parse & filter raw NX records. Truncate QNAMEs to E2LDs."
[[sensor rest]]
(ignore-errors
(am/domonad am/maybe-m
[:when (not (v-sensor? sensor))
[ts client domain :as fields] (str/split rest #"\t" 3)
:when (valid-fields? 3 fields)
ts (Long/parseLong ts 10)
domain (flat/domain-e2ld @etlds domain)