Skip to content

Instantly share code, notes, and snippets.

@graue
graue / x_combinator.lua
Created January 14, 2013 21:45
X combinator (like the Y combinator) in Lua
-- traditional fixed-point operator from functional programming
Y = function (f)
return (function (x)
return x(x)
end)(function (x)
return f(function (v)
return (x(x))(v)
end)
end)
end
@graue
graue / juices.py
Last active December 11, 2015 04:59
itertools.chain() in Python
import itertools
fruits = ['apple', 'orange', 'grape']
veggies = ['cucumber', 'onion', 'potato']
juices = [thing + ' juice' for thing in itertools.chain(fruits, veggies)]
# >>> juices
# ['apple juice', 'orange juice', 'grape juice', 'cucumber juice', 'onion juice', 'potato juice']
@graue
graue / gist:4592307
Created January 22, 2013 05:24
a kinda cool thing (generative art) that I put into http://jelv.is/draw/
function (x, y, d, a) {
function surface(x, y) { return (x-sin(y))*(sin(x)-y)*(sin(x)-sin(y)); }
function safer(func) {
return function(x) {
if (x === 0) return 0;
else if (x < 0) return -func(-x);
else return func(x);
}
}
@graue
graue / gist:4622755
Created January 24, 2013 15:07
Potsfyi nginx proxying config
upstream potsfyi {
server 127.0.0.1:8000 fail_timeout=0;
}
server {
listen 4004;
server_name _;
keepalive_timeout 10;
location / {
1 bag frozen blueberries
4 bananas
unsweetened applesauce
egg replacer
firm tofu
vegetable broth
@graue
graue / histArray_spec.lua
Last active December 14, 2015 00:39
Specification (tests!) for not yet implemented Lua HistArray
local HistArray = require "histArray"
-- The HistArray module provides an array that automatically purges
-- old values. It lets you write code like:
--
-- y[n] = c1*x[n] + c2*x[n-1] + c3*x[n-2]
-- - c4*y[n-1] - c5*y[n-2]
--
-- If x and y are HistArrays created with history size 2, all samples from
-- x[n-3] and y[n-3] and below will be purged (accessing them is an error)
-- delay effect
local wrap = require "util.wrap"
local HistArray = require "util.histArray"
-- Convert a millisecond measurement to a whole number of samples.
local function msToSamples(ms)
@graue
graue / gist:5079734
Last active December 14, 2015 11:29
Weirdness with JS closures (Lua behaves differently).
NOTE: Scroll down for explanation (the difference turns out to be scoping, not closures per se).
$ cat juices.lua
local fruits = {"apple", "orange", "grape"}
local juicers = {}
for i,v in ipairs(fruits) do
local fruit = v
juicers[i] = function() return fruit .. " juice" end
@graue
graue / posts.atom
Created March 18, 2013 00:01
My Atom (i.e. "RSS") feed template for Jekyll
---
layout: nil
---
<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title>Divagations of a graphomane</title>
<link href="http://scott.mn/"/>
<link type="application/atom+xml" rel="self" href="http://scott.mn/posts.atom"/>
<updated>{{ site.time | date_to_xmlschema }}</updated>
@graue
graue / gist:5279439
Last active December 15, 2015 15:09
Using TJ Holowaychuk's "watch" with make

Using TJ Holowaychuk's "watch" with make

Note: I edited this and turned it into a blog post - read that instead.

While poking around some random projects by the prolific TJ Holowaychuk, I came upon his version of watch, which simply executes a given command periodically.

The idea is to pair it with make to run tests and automatically rebuild/recompile code. Since make tracks dependencies and looks at file modification times, it won't do anything if the relevant source files haven't changed.

There's a program called watch that comes with Linux, but it doesn't play as nicely with make. In particular, it clears the screen every time the command is run. For pairing this with make, we want new output to be printed whenever make actually does anything, and otherwise we want it to be silent.