Skip to content

Instantly share code, notes, and snippets.

@shesek
Created April 20, 2012 03:29
Show Gist options
  • Save shesek/2425683 to your computer and use it in GitHub Desktop.
Save shesek/2425683 to your computer and use it in GitHub Desktop.
CoffeeScript: tiny DSL for switch-like function based control flow
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