Advertisement
Guest User

Untitled

a guest
Jul 28th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.62 KB | None | 0 0
  1. var match = function (...vals) {
  2. return {
  3. when(...args) {
  4. var patterns = _(args).dropRight().take(vals.length);
  5. var values = _(vals).take(args.length-1);
  6.  
  7. var satisfy = patterns
  8. .zip(values.value())
  9. .every(([p,v]) => p(v));
  10.  
  11. if (satisfy) {
  12. this.result = _.last(args);
  13. this.when = function () { return this; }
  14. }
  15. return this;
  16. },
  17. done () {
  18. return this.result;
  19. },
  20. }
  21. }
  22.  
  23. var m = function(n) { return (x => x > n) };
  24. var e = function(n) { return (x => x == n) };
  25. var t = match(0,1,2)
  26. .when(m(0), e(1), e(2), 'a')
  27. .when(e(0), e(1), 'b')
  28. .when(e(0), e(1), e(2), 'c')
  29. .done()
  30. console.log(t);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement