Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Functional = function() {}
- Functional.prototype = {
- map: function(funct, array) {
- var result = [];
- for(var n = 0; n < array.length; n++)
- result.push(funct(array[n]));
- return result;
- },
- fold: function(funct, array, basis) {
- var result = basis;
- for(var n = 0; n < array.length; n++)
- result = funct(result, array[n]);
- return result;
- }
- }
- suite.addTest(new Test("Functional.map can map 0 values",
- function() {
- var f = Object.create(new Functional());
- var mapped = f.map(function(value) { return value + 1; }, []);
- this.assertEqual(0, mapped.length);
- }));
- suite.addTest(new Test("Functional.map can map 3 values",
- function() {
- var f = Object.create(new Functional());
- var mapped = f.map(
- function(value) { return value + 1; }, [1, 2, 3]);
- this.assertEqual(2, mapped[0]);
- this.assertEqual(3, mapped[1]);
- this.assertEqual(4, mapped[2]);
- }));
- suite.addTest(new Test("Functional.fold can fold 0 values",
- function() {
- var f = Object.create(new Functional());
- var foldResult = f.fold(
- function(left,right) { return left + right; }, [], 0);
- this.assertEqual(0, foldResult);
- }));
- suite.addTest(new Test("Functional.fold can fold 3 values",
- function() {
- var f = Object.create(new Functional());
- var foldResult = f.fold(
- function(left,right) { return left + right; }, [1, 2, 3], 0);
- this.assertEqual(6, foldResult);
- }));
Add Comment
Please, Sign In to add comment