fork download
  1. function sequence(start, step) {
  2. var x = 0;
  3. return function () {
  4. start = start || 0;
  5. step = step || 1;
  6. var y = start + (step * x)
  7. x++;
  8. return y;
  9. }
  10. }
  11.  
  12.  
  13. var gen = sequence(1, 1);
  14.  
  15.  
  16. function square(x) { return x * x };
  17.  
  18.  
  19. function fmap(mixin, fn) {
  20. return function() {
  21. return mixin(fn.apply(null, arguments));
  22. }
  23. }
  24.  
  25.  
  26.  
  27. var squareGen = fmap(square, gen);
  28.  
  29. console.log(squareGen()); // 1
  30. console.log(squareGen()); // 4
  31. console.log(squareGen()); // 9
  32. console.log(squareGen()); // 16
  33.  
  34.  
  35.  
  36. function add(a, b) {
  37. return a + b;
  38. }
  39.  
  40.  
  41. var squareAdd = fmap(square, add);
  42. console.log(squareAdd(2, 3)); // 25 = (2 + 3) ^ 2
  43. console.log(squareAdd(5, 7)); // 144 = (5 + 7) ^ 2
Runtime error #stdin #stdout #stderr 0.44s 321920KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
js: uncaught JavaScript runtime exception: ReferenceError: "console" is not defined.