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:
""" | |
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): |
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 |
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) |
@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) |
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 |
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): |
(* 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. *) |
I hereby claim:
To claim this, I am signing this object: