Created
August 28, 2012 07:53
-
-
Save lost-theory/3495985 to your computer and use it in GitHub Desktop.
coffeescript advice method combinators a la raganwald, in python
This file contains hidden or 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
| #http://news.ycombinator.com/item?id=4439352 | |
| ''' | |
| before = (decoration) -> | |
| (base) -> | |
| -> | |
| decoration.apply(this, arguments) | |
| base.apply(this, arguments) | |
| after = (decoration) -> | |
| (base) -> | |
| -> | |
| __value__ = base.apply(this, arguments) | |
| decoration.apply(this, arguments) | |
| __value__ | |
| around = (decoration) -> | |
| (base) -> | |
| (argv...) -> | |
| callback = => base.apply(this, argv) | |
| decoration.apply(this, [callback].concat(argv)) | |
| provided = (condition) -> | |
| (base) -> | |
| -> | |
| if condition.apply(this, arguments) | |
| base.apply(this, arguments) | |
| ''' | |
| def before(decoration): | |
| def entangle(base): | |
| def inner(*a, **kw): | |
| decoration(*a, **kw) | |
| return base(*a, **kw) | |
| return inner | |
| return entangle | |
| def after(decoration): | |
| def entangle(base): | |
| def inner(*a, **kw): | |
| ret = base(*a, **kw) | |
| decoration(*a, **kw) | |
| return ret | |
| return inner | |
| return entangle | |
| def around(decoration): | |
| def entangle(base): | |
| def inner(*a, **kw): | |
| return decoration(base, *a, **kw) | |
| return inner | |
| return entangle | |
| def provided(condition): | |
| def entangle(base): | |
| def inner(*a, **kw): | |
| if condition(*a, **kw): | |
| return base(*a, **kw) | |
| return inner | |
| return entangle | |
| ## examples ################################################################### | |
| def say(s): | |
| print s | |
| def triggers(event): | |
| @after | |
| def inner(*a, **kw): | |
| print "triggering event %r.. (got args %r)" % (event, a) | |
| return inner | |
| def display_wait(): | |
| outer = {'wait_lvl': 0} #PEP3104 | |
| @around | |
| def inner(NEXT, *a, **kw): | |
| outer['wait_lvl'] += 1 | |
| if outer['wait_lvl'] > 0: | |
| print "showing element" | |
| ret = NEXT(*a, **kw) | |
| outer['wait_lvl'] -= 1 | |
| if outer['wait_lvl'] <= 0: | |
| print "hiding element btw the args were %r" % (a,) | |
| return ret | |
| return inner | |
| @triggers("cache:dirty") | |
| def set_heavyweight_prop(prop, val): | |
| print "set %r to %r" % (prop, val) | |
| @provided(lambda a,b: a > 0 and b > 0) | |
| @before(lambda a,b: say("warning! big calculation about to run")) | |
| @display_wait() | |
| @triggers("cache:dirty") | |
| def recalculate(a, b): | |
| print "recalculating...", a+b | |
| return a+b | |
| if __name__ == "__main__": | |
| print "\n== set_heavyweight_prop ==" | |
| set_heavyweight_prop("pi", 3.14159) | |
| print "\n== recalculate ==" | |
| recalculate(2, 2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment