Skip to content

Instantly share code, notes, and snippets.

View llasram's full-sized avatar

Marshall Bockrath llasram

View GitHub Profile
(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 / 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))
@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 / 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 / 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 / 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 / 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 / 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 / case-expr.clj
Created October 14, 2011 14:15
Version of `case' allowing compile-time evaluated expressions
(defmacro case-expr
"Like case, but only supports individual test expressions, which are
evaluated at macro-expansion time."
[e & clauses]
`(case ~e
~@(concat
(mapcat (fn [[test result]]
[(eval `(let [test# ~test] test#)) result])
(partition 2 clauses))
(when (odd? (count clauses))
@llasram
llasram / nbody.clj
Created October 30, 2011 15:24
Alioth shootout n-body benchmark Clojure implementation
(ns nbody
(:gen-class))
;;
;; Convenience aliases for unchecked arithmetic operations
(defmacro uc+ [& args] `(unchecked-add ~@args))
(defmacro uc- [& args] `(unchecked-subtract ~@args))
(defmacro uc* [& args] `(unchecked-multiply ~@args))