Created
April 20, 2012 03:29
-
-
Save shesek/2425683 to your computer and use it in GitHub Desktop.
CoffeeScript: tiny DSL for switch-like function based control flow
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
switchx = (value, fns...) -> return ret for fn in fns when ret=fn value | |
whenx = (pred, fn) -> (val) -> (fn val) or true if pred val | |
defaultx = (fn) -> (val) -> fn val | |
# example | |
jump = (p) -> p.takeEnergy JUMP_ENERGY | |
canJump = (p) -> p.getEnergy() > JUMP_ENERGY | |
# ... | |
switchx player, | |
whenx canJump, jump | |
whenx hasWater, compose drink, jump | |
defaultx -> throw new Exception 'Cant jump!' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment