Skip to content

Instantly share code, notes, and snippets.

def gen_primes():
"""Generate an infinite sequence of prime numbers."""
D = {}
q = 2
S = 4
r = 2
while True:
if q not in D:
if q == S:
S = S + r
(defn echo [x] (do (prn x) x))
(defn readmap [fn fl]
(with-open [r (clojure.java.io/reader fl)] (doall (map fn (line-seq r)))))
(defn zip-county-frac [s] (let [l (clojure.string/split s #",")] (list (second l) (first l) (nth l 4))))
(defn cbsa-county [s] (list (first (clojure.string/split s #"," 2)) (clojure.string/replace (or (re-find #",\d\d,\d\d\d," s) "") #"," "")))
@scythe
scythe / sortedcitations.lua
Created March 31, 2021 17:41
Fix the dumb LaTeX bibliography
-- This script attempts to automatically sort the \bibitem{}s in the bibliography of a .tex file according to occurrences of \cite{}.
-- Pretty much everyone wants this, but LaTeX you to use extra files or sort by hand. This is annoying for small projects which only
-- need to use a single .tex file.
-- Assumptions: all \cite{} and \bibitem{} occurrences are in one file; only one \bibitem{} on each line; string.lower() works with
-- your citation handles.
tfile = [==[ PASTE .tex FILE HERE ]==] -- or use file i/o and io:lines() instead of the first call to gmatch()
local orders, count, lines = {}, 1, {}
@scythe
scythe / array.lua
Last active April 25, 2020 23:54
Array library for lua
-- A simple array-manipulation library for Lua.
-- The natural question is: why bother writing this?
-- After all, it's very short, and anyone reasonably familiar with
-- normal array-manipulation libraries could replicate it in a few
-- hours.
-- But despite this, such a library does not exist in the wild, and
-- the existing counterparts are bad. For instance, penlight.List
-- lacks foldr(), and includes a bunch of things that it shouldn't,
-- like t:put(x), which is equivalent to t:insert(1, x), but also
-- prevents anyone reading your code from knowing what it does.
@scythe
scythe / complex3.c
Created April 3, 2020 23:22
SIMD-friendly complex numbers with redundancy
/* example implementation -- not recommended for actual use, but it would work!
* (SIMD-friendly in principle; not shown)
* (probably not ideal for memory-bound workloads; uses 50% more space)
* based on two insights:
* - complex multiplication (a+bi)(c+di) with i^2 = -1 is equivalent to
* split-complex multiplication (a+bk)(c+dk) - 2*b*d with k^2 = +1
* - the split-complex numbers are isomorphic to the direct product R*R with the
* basis (1+k)/sqrt(2), (1-k)/sqrt(2), both idempotents, thus split-complex
* multiplication (a+bk)(c+dk) can be done by (a+b)(c+d) and (a-b)(c-d)
*/
@scythe
scythe / antiantivala.md
Created March 30, 2020 21:59
The anti-anti-Vala FAQ

The goal of this FAQ is to provide a humorous response to the inevitable question that comes up whenever people discuss a project that is written in Vala. Hopefully, it will save some time in the long run.

Why didn't you just use...

C#?

That was called Mono, and nobody liked it.

@scythe
scythe / gist:844a4977fa22b67e5c9e
Last active August 29, 2015 14:17
hofstadter_female_differences.lua
f = {[0] = 1}
m = {[0] = 0}
len = 100000000
for i = 1, len do
m[i] = i - f[m[i-1]]
f[i] = i - m[f[i-1]]
end
@scythe
scythe / va_stringify.h
Created January 21, 2015 19:27
Convert variadic macro arguments to strings
/* STR_VA_ARG(...) -- convert variadic arglist to strings
* Usage: STR_VA_ARG(phnglui, mglwnafh, cthulhu, rlyeh, wgahnagl, fhtagn)
* >> "phnglui", "mglwnafh", "cthulhu", "rlyeh", "wgahnagl", "fhtagn"
*/
#ifndef __VA_STRINGIFY_H
#define __VA_STRINGIFY_H
#define PP_NARG(...) \
PP_NARG_(__VA_ARGS__,PP_RSEQ_N())
@scythe
scythe / gist:d28c3f4933ff2f1e5c47
Last active August 29, 2015 14:06
transducers in lua
--Rich Hickey's infamous transducers, demonstrating reversed function composition.
--Look closely.
function transduce(tf, rf, init, iter)
for i in iter do
init = tf(rf)(init, i)
end
return init
end
@scythe
scythe / raj.lua
Last active August 29, 2015 14:05
A JSON parser in LPeg.re
re = require "re"
local function condense(keyvals)
local obj = {}
for i = 1, #keyvals do
obj[keyvals[i][1]] = keyvals[i][2]
end
return obj
end