Skip to content

Instantly share code, notes, and snippets.

View bedekelly's full-sized avatar

Bede Kelly bedekelly

View GitHub Profile
@bedekelly
bedekelly / ignores.py
Created December 9, 2015 01:37
implement an "ignores" list for OOE in IF
"""
A short example of my proposed object-oriented event framework for IF games.
Every event should implement a go() method, which calls super().go() before doing
anything else. After that, it should check whether its own go() method is
applicable by making a call to self.should_fire(<EventType>).
"""
class Event:
"""Generic Event type."""
def __init__(self, obj):
<head>
{{! Document Settings }}
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
{{! Page Meta }}
<title>{{meta_title}}</title>
<meta name="description" content="{{meta_description}}" />
<meta name="HandheldFriendly" content="True" />
@bedekelly
bedekelly / bounce.py
Created March 11, 2016 03:36
Tail-recursion optimization for Python! (kinda.)
def bounce(gen):
"""
This "decorator" (don't use it with the @syntax or you'll blow the stack!)
allows you to convert a tail-recursive function into an iteration.
To use it, replace each "return" in your function with "yield".
Then, "decorate" your new generator function like so:
new_name = bounce(old_name)
Make sure you're not using the same name, or you'll run into horrible errors.
You could also use this with the `decorate` decorator.
Thanks to http://www.usrsb.in/blog/blog/2012/08/12/bouncing-pythons-generators-with-a-trampoline/#comment-617720000
@bedekelly
bedekelly / naive_countdown.py
Created March 11, 2016 04:13
The original version of a tail-recursive countdown.
def countdown(a, b):
"""Count down from a to b, printing the numbers, and return b."""
print(a)
if a == b:
return b
else:
return countdown(a-1, b)
x = countdown(10000, 0)
@bedekelly
bedekelly / bouncetest.py
Last active March 11, 2016 04:22
Test a modified recursive generator that previously overflowed the stack
@decorate(bounce, globals(), "new_countdown")
def countdown(a, b):
"""Count down from a to b, printing the numbers, and return b."""
print(a)
if a == b:
yield b
else:
yield countdown(a-1, b)
x = new_countdown(10000, 0)
@bedekelly
bedekelly / coroutine.py
Created March 11, 2016 04:29
A simple decorator to advance a generator to the first "yield" without prompting.
def coroutine(generator):
"""Make sure we don't have to call `next` on a just-started coroutine."""
@wraps(generator)
def new_cr(*args, **kwargs):
g = generator(*args, **kwargs)
next(g)
return g
return new_cr
@bedekelly
bedekelly / decorate.py
Last active March 11, 2016 04:39
Meta-decorator for decorating functions with different names.
class decorate:
"""
Decorate a function with a decorator, specifying the new name it should have
in the given scope.
Usage:
@decorate(decorator, scope, name)
def f(): pass
"""
def __init__(self, decorator=None, scope=None, name=None):
@bedekelly
bedekelly / q2.ml
Created May 8, 2016 21:53
Question 2 a-c, FoCS paper 2015
(* 2a: defining a data type for boolean expressions. (3%) *)
type const = True | False;;
type uop = Not;;
type bop = And | Or;;
type exp =
| Const of const
| Uexp of (uop * exp)
| Bexp of (exp * bop * exp);;
(* 2d: adding a variable constructor to our type. *)
type exp =
| True | False
| Not of exp
| And of (exp * exp)
| Or of (exp * exp)
| Var of bool;;
(* 2e: check whether an expression has variables. *)
@bedekelly
bedekelly / keybase.md
Created August 5, 2016 08:56
Keybase Proof

Keybase proof

I hereby claim:

  • I am bedekelly on github.
  • I am bede (https://keybase.io/bede) on keybase.
  • I have a public key whose fingerprint is C2A0 F192 389D FDDD 006E E24E 04B1 289A A905 FA62

To claim this, I am signing this object: