Guest User

Untitled

a guest
May 25th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. Functional = function() {}
  2.  
  3. Functional.prototype = {
  4. map: function(funct, array) {
  5. var result = [];
  6. for(var n = 0; n < array.length; n++)
  7. result.push(funct(array[n]));
  8. return result;
  9. },
  10.  
  11. fold: function(funct, array, basis) {
  12. var result = basis;
  13. for(var n = 0; n < array.length; n++)
  14. result = funct(result, array[n]);
  15. return result;
  16. }
  17. }
  18.  
  19. suite.addTest(new Test("Functional.map can map 0 values",
  20. function() {
  21. var f = Object.create(new Functional());
  22. var mapped = f.map(function(value) { return value + 1; }, []);
  23. this.assertEqual(0, mapped.length);
  24. }));
  25.  
  26. suite.addTest(new Test("Functional.map can map 3 values",
  27. function() {
  28. var f = Object.create(new Functional());
  29. var mapped = f.map(
  30. function(value) { return value + 1; }, [1, 2, 3]);
  31. this.assertEqual(2, mapped[0]);
  32. this.assertEqual(3, mapped[1]);
  33. this.assertEqual(4, mapped[2]);
  34. }));
  35.  
  36. suite.addTest(new Test("Functional.fold can fold 0 values",
  37. function() {
  38. var f = Object.create(new Functional());
  39. var foldResult = f.fold(
  40. function(left,right) { return left + right; }, [], 0);
  41. this.assertEqual(0, foldResult);
  42. }));
  43.  
  44. suite.addTest(new Test("Functional.fold can fold 3 values",
  45. function() {
  46. var f = Object.create(new Functional());
  47. var foldResult = f.fold(
  48. function(left,right) { return left + right; }, [1, 2, 3], 0);
  49. this.assertEqual(6, foldResult);
  50. }));
Add Comment
Please, Sign In to add comment