This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(* 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);; |