Skip to content
This repository has been archived by the owner on Jul 25, 2018. It is now read-only.

Curry the combinators #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ module.exports = function (grunt) {
pkg: grunt.file.readJSON('package.json'),
jshint: {
all: [
'src/*.js'
'src/*.js',
'test/*.js'
]
},
nodeunit: {
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
"url": "https://github.com/fantasyland/fantasy-combinators/issues"
},
"dependencies": {
"fantasy-helpers": "0.x.x"
},
"devDependencies": {
"grunt-cli": "0.1.9",
"grunt": "0.4.x",
"grunt-contrib-jshint": "0.6.x",
"grunt-contrib-nodeunit": "0.2.0",
"fantasy-check": "0.0.8",
"fantasy-helpers": "0.0.x"
"fantasy-check": "0.0.8"
},
"scripts": {
"test": "grunt"
Expand Down
10 changes: 5 additions & 5 deletions src/apply.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// A combinator
function apply(f) {
return function(x) {
var helpers = require("fantasy-helpers"),

// A combinator
apply = helpers.curry(function(f, x) {
return f(x);
};
}
});

// Export
if(typeof module != 'undefined')
Expand Down
14 changes: 6 additions & 8 deletions src/compose.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
// B combinator
function compose(f) {
return function(g) {
return function(x) {
return f(g(x));
};
};
}
var helpers = require("fantasy-helpers"),

// B combinator
compose = helpers.curry(function(f, g, x) {
return f(g(x));
});

// Export
if(typeof module != 'undefined')
Expand Down
10 changes: 5 additions & 5 deletions src/constant.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// K combinator
function constant(a) {
return function(b) {
var helpers = require("fantasy-helpers"),

// K combinator
constant = helpers.curry(function(a, b) {
return a;
};
}
});

// Export
if(typeof module != 'undefined')
Expand Down
14 changes: 6 additions & 8 deletions src/flip.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
// C combinator
function flip(f) {
return function(a) {
return function(b) {
return f(b)(a);
};
};
}
var helpers = require("fantasy-helpers"),

// C combinator
flip = helpers.curry(function(f, a, b) {
return f(b)(a);
});

// Export
if(typeof module != 'undefined')
Expand Down
16 changes: 6 additions & 10 deletions src/psi.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
// Psi combinator
function psi(f) {
return function(g) {
return function(x) {
return function(y) {
return f(g(x))(g(y));
};
};
};
}
var helpers = require("fantasy-helpers"),

// Psi combinator
psi = helpers.curry(function(f, g, x, y) {
return f(g(x))(g(y));
});

// Export
if (typeof module != 'undefined')
Expand Down
14 changes: 6 additions & 8 deletions src/substitution.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
// S combinator
function substitution(f) {
return function(g) {
return function(x) {
return f(x)(g(x));
};
};
}
var helpers = require("fantasy-helpers"),

// S combinator
substitution = helpers.curry(function(f, g, x) {
return f(x)(g(x));
});

// Export
if(typeof module != 'undefined')
Expand Down
10 changes: 5 additions & 5 deletions src/thrush.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// T combinator
function thrush(x) {
return function(f) {
var helpers = require("fantasy-helpers"),

// T combinator
thrush = helpers.curry(function(x, f) {
return f(x);
};
}
});

// Export
if(typeof module != 'undefined')
Expand Down
6 changes: 3 additions & 3 deletions test/constant.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ var λ = require('fantasy-check/src/adapters/nodeunit'),

exports.constant = {
'when testing constant should return correct value': λ.check(
function(a) {
return constant(a)() === a;
function(a, b) {
return constant(a)(b) === a;
},
[λ.AnyVal]
[λ.AnyVal, λ.AnyVal]
)
};
19 changes: 9 additions & 10 deletions test/psi.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,39 @@ var λ = require('fantasy-check/src/adapters/nodeunit'),
psi = combinators.psi,
identity = combinators.identity,
flip = combinators.flip,
compose = combinators.compose
compose = combinators.compose;

function f(a) {
return a * 2
return a * 2;
}
function g(a) {
return a - 1
return a - 1;
}
function mul(a) {
return function(b) {
return a * b
}
return a * b;
};
}

exports.psi = {
'Application to identity should be equivalent to applying the original function': λ.check(
function(a, b) {
return psi(mul)(identity)(a)(b) === mul(a)(b)
return psi(mul)(identity)(a)(b) === mul(a)(b);
},
[Number, Number]
),

'((*) `psi` f) `psi` g is equivalent to (*) `psi` (f . g)': λ.check(
function(a, b) {
return psi(psi(mul)(f))(g)(a)(b) === psi(mul)(compose(f)(g))(a)(b)
return psi(psi(mul)(f))(g)(a)(b) === psi(mul)(compose(f)(g))(a)(b);
},
[Number, Number]
),

'flip psi f . flip psi g is equivalent to flip psi (g . f)': λ.check(
function(a, b) {
return compose(flip(psi)(f))(flip(psi)(g))(mul)(a)(b)
=== flip(psi)(compose(g)(f))(mul)(a)(b)
return compose(flip(psi)(f))(flip(psi)(g))(mul)(a)(b) === flip(psi)(compose(g)(f))(mul)(a)(b);
},
[Number, Number]
)
}
};