Advertisement
Guest User

Untitled

a guest
May 1st, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.44 KB | None | 0 0
  1. const Case = cases => (
  2. pred => (
  3. (typeof cases[pred] !== 'undefined') ? cases[pred]() : cases['_']()
  4. )
  5. )
  6.  
  7. const count = Case({
  8. 0: () => 'zero',
  9. 1: () => 'one',
  10. _: () => 'many'
  11. })
  12.  
  13. count(0) // 'zero'
  14. count(1) // 'one'
  15. count(5) // 'many'
  16.  
  17. const cond = Case({
  18. true: () => 'yes!',
  19. false: () => 'no :('
  20. })
  21.  
  22. cond(true) // 'yes!'
  23. cond(false) // 'no :('
  24.  
  25. Case({
  26. 1: () => 'a single item',
  27. _: () => 'many items'
  28. })(2) // 'many items'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement