| Function::andThen = (f) -> => | ||
| r = @apply(@, Array.prototype.slice.call(arguments, 0)) | ||
| f.apply(f, if Array.isArray(r) then r else [r]) | ||
| Function::compose = (f) -> => | ||
| r = f.apply(f, Array.prototype.slice.call(arguments, 0)) | ||
| @apply(@, if Array.isArray(r) then r else [r]) |
| should = require('should') | ||
| describe('function1', -> | ||
| require('./function1') | ||
| add = (a, b) -> a + b | ||
| divide = (a, b) -> a / b | ||
| divideByFour = (a) -> a / 4 | ||
| divideByTwo = (a) -> a / 2 | ||
| multiply = (a, b) -> a * b | ||
| subtract = (a, b) -> a - b | ||
| square = (a) -> a * a | ||
| variadicAddTwo = -> y + 2 for y in arguments | ||
| variadicDivideByTwo = -> y / 2 for y in arguments | ||
| describe('andThen', -> | ||
| it('should work', -> | ||
| (square.andThen divideByTwo.andThen divideByFour)(10).should.be.equal(12.5) | ||
| (add.andThen square)(10, 5).should.be.equal(225) | ||
| (divide.andThen square)(100, 10).should.be.equal(100) | ||
| (multiply.andThen square)(5, 5).should.be.equal(625) | ||
| (subtract.andThen square)(0, 5).should.be.equal(25) | ||
| (variadicDivideByTwo.andThen variadicAddTwo)(10, 6)[0].should.be.equal(7) | ||
| (variadicAddTwo.andThen variadicDivideByTwo)(10, 6)[1].should.be.equal(4) | ||
| ) | ||
| ) | ||
| describe('compose', -> | ||
| it('should work', -> | ||
| (square.compose divideByTwo.compose divideByFour)(10).should.be.equal(1.5625) | ||
| (divideByFour.compose divideByTwo.compose square)(10).should.be.equal(12.5) | ||
| (square.compose add)(10, 5).should.be.equal(225) | ||
| (square.compose divide)(100, 10).should.be.equal(100) | ||
| (square.compose multiply)(5, 5).should.be.equal(625) | ||
| (square.compose subtract)(0, 5).should.be.equal(25) | ||
| (variadicDivideByTwo.compose variadicAddTwo)(10, 6)[0].should.be.equal(6) | ||
| (variadicAddTwo.compose variadicDivideByTwo)(10, 6)[1].should.be.equal(5) | ||
| ) | ||
| ) | ||
| ) |
@@ -7,3 +7,3 @@ (function() { | ||
| describe('function1', function() { | ||
| var add, divide, divideByFour, divideByTwo, multiply, square, subtract; | ||
| var add, divide, divideByFour, divideByTwo, multiply, square, subtract, variadicAddTwo, variadicDivideByTwo; | ||
| require('./function1'); | ||
@@ -31,2 +31,20 @@ add = function(a, b) { | ||
| }; | ||
| variadicAddTwo = function() { | ||
| var y, _i, _len, _results; | ||
| _results = []; | ||
| for (_i = 0, _len = arguments.length; _i < _len; _i++) { | ||
| y = arguments[_i]; | ||
| _results.push(y + 2); | ||
| } | ||
| return _results; | ||
| }; | ||
| variadicDivideByTwo = function() { | ||
| var y, _i, _len, _results; | ||
| _results = []; | ||
| for (_i = 0, _len = arguments.length; _i < _len; _i++) { | ||
| y = arguments[_i]; | ||
| _results.push(y / 2); | ||
| } | ||
| return _results; | ||
| }; | ||
| describe('andThen', function() { | ||
@@ -38,3 +56,5 @@ return it('should work', function() { | ||
| (multiply.andThen(square))(5, 5).should.be.equal(625); | ||
| return (subtract.andThen(square))(0, 5).should.be.equal(25); | ||
| (subtract.andThen(square))(0, 5).should.be.equal(25); | ||
| (variadicDivideByTwo.andThen(variadicAddTwo))(10, 6)[0].should.be.equal(7); | ||
| return (variadicAddTwo.andThen(variadicDivideByTwo))(10, 6)[1].should.be.equal(4); | ||
| }); | ||
@@ -49,3 +69,5 @@ }); | ||
| (square.compose(multiply))(5, 5).should.be.equal(625); | ||
| return (square.compose(subtract))(0, 5).should.be.equal(25); | ||
| (square.compose(subtract))(0, 5).should.be.equal(25); | ||
| (variadicDivideByTwo.compose(variadicAddTwo))(10, 6)[0].should.be.equal(6); | ||
| return (variadicAddTwo.compose(variadicDivideByTwo))(10, 6)[1].should.be.equal(5); | ||
| }); | ||
@@ -52,0 +74,0 @@ }); |
@@ -5,3 +5,5 @@ (function() { | ||
| return function() { | ||
| return f(_this.apply(_this, Array.prototype.slice.call(arguments, 0))); | ||
| var r; | ||
| r = _this.apply(_this, Array.prototype.slice.call(arguments, 0)); | ||
| return f.apply(f, Array.isArray(r) ? r : [r]); | ||
| }; | ||
@@ -13,3 +15,5 @@ }; | ||
| return function() { | ||
| return _this.call(_this, f.apply(_this, Array.prototype.slice.call(arguments, 0))); | ||
| var r; | ||
| r = f.apply(f, Array.prototype.slice.call(arguments, 0)); | ||
| return _this.apply(_this, Array.isArray(r) ? r : [r]); | ||
| }; | ||
@@ -16,0 +20,0 @@ }; |
+2
-2
@@ -7,3 +7,3 @@ module.exports = (grunt) -> | ||
| expand: true | ||
| cwd: 'source/main/coffeescript/' | ||
| cwd: 'src/main/coffeescript/' | ||
| src: ['**/*.coffee'] | ||
@@ -15,3 +15,3 @@ dest: 'build/' | ||
| expand: true | ||
| cwd: 'source/test/coffeescript/' | ||
| cwd: 'src/test/coffeescript/' | ||
| src: ['**/*.coffee'] | ||
@@ -18,0 +18,0 @@ dest: 'build/' |
+1
-1
@@ -24,3 +24,3 @@ { | ||
| }, | ||
| "version": "0.1.0" | ||
| "version": "0.1.1" | ||
| } |
+5
-0
@@ -26,2 +26,4 @@ #function1.js [](http://travis-ci.org/rockymadden/function1.js) [](http://badge.fury.io/js/function1) | ||
| square = (a) -> a * a | ||
| variadicAddTwo = -> y + 2 for y in arguments | ||
| variadicDivideByTwo = -> y / 2 for y in arguments | ||
| ``` | ||
@@ -47,2 +49,5 @@ | ||
| (square.compose subtract)(0, 5) # 25 | ||
| (variadicAddTwo.andThen variadicDivideByTwo)(10, 6) # [6, 4] | ||
| (variadicAddTwo.compose variadicDivideByTwo)(10, 6) # [7, 5] | ||
| ``` | ||
@@ -49,0 +54,0 @@ |
| Function::andThen = (f) -> => f(@apply(@, Array.prototype.slice.call(arguments, 0))) | ||
| Function::compose = (f) -> => @call(@, f.apply(@, Array.prototype.slice.call(arguments, 0))) |
| should = require('should') | ||
| describe('function1', -> | ||
| require('./function1') | ||
| add = (a, b) -> a + b | ||
| divide = (a, b) -> a / b | ||
| divideByFour = (a) -> a / 4 | ||
| divideByTwo = (a) -> a / 2 | ||
| multiply = (a, b) -> a * b | ||
| subtract = (a, b) -> a - b | ||
| square = (a) -> a * a | ||
| describe('andThen', -> | ||
| it('should work', -> | ||
| (square.andThen divideByTwo.andThen divideByFour)(10).should.be.equal(12.5) | ||
| (add.andThen square)(10, 5).should.be.equal(225) | ||
| (divide.andThen square)(100, 10).should.be.equal(100) | ||
| (multiply.andThen square)(5, 5).should.be.equal(625) | ||
| (subtract.andThen square)(0, 5).should.be.equal(25) | ||
| ) | ||
| ) | ||
| describe('compose', -> | ||
| it('should work', -> | ||
| (square.compose divideByTwo.compose divideByFour)(10).should.be.equal(1.5625) | ||
| (divideByFour.compose divideByTwo.compose square)(10).should.be.equal(12.5) | ||
| (square.compose add)(10, 5).should.be.equal(225) | ||
| (square.compose divide)(100, 10).should.be.equal(100) | ||
| (square.compose multiply)(5, 5).should.be.equal(625) | ||
| (square.compose subtract)(0, 5).should.be.equal(25) | ||
| ) | ||
| ) | ||
| ) |
9196
22.52%88
41.94%79
6.76%