-
Notifications
You must be signed in to change notification settings - Fork 377
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add Maybe type for testing Alternative laws and refactor tests
- move patches and test structures into `internal` - `Compose` and patching of `Array` form `laws/traversable` - `equality`, `Sum` and patching of `String` form `id_test` - `Id` from `id` - rename `id_test` to `test` as it's not just testing `Id` but `Maybe` and `Sum`
- Loading branch information
Showing
12 changed files
with
276 additions
and
152 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
'use strict'; | ||
|
||
const {tagged} = require('daggy'); | ||
|
||
const fl = require('..'); | ||
const {equality} = require('./func'); | ||
|
||
const Compose = module.exports = tagged('c'); | ||
Compose[fl.of] = Compose; | ||
Compose.prototype[fl.ap] = function(f) { | ||
return Compose(this.c[fl.ap](f.c[fl.map](u => y => y[fl.ap](u)))); | ||
}; | ||
Compose.prototype[fl.map] = function(f) { | ||
return Compose(this.c[fl.map](y => y[fl.map](f))); | ||
}; | ||
Compose.prototype[fl.equals] = function(x) { | ||
return equality(this.c, x.c); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
'use strict'; | ||
|
||
const fl = require('../'); | ||
const equality = (x, y) => typeof x[fl.equals] === 'function' ? x[fl.equals](y) : x === y; | ||
|
||
module.exports = { | ||
equality, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
'use strict'; | ||
|
||
const patch = require('./patch'); | ||
const Maybe = require('./maybe'); | ||
const Id = require('./id'); | ||
const Sum = require('./string_sum'); | ||
const Compose = require('./compose'); | ||
const {equality} = require('./func'); | ||
|
||
module.exports = { | ||
Maybe, | ||
Id, | ||
Sum, | ||
Compose, | ||
equality, | ||
patch, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
'use strict'; | ||
|
||
const {taggedSum} = require('daggy'); | ||
|
||
const fl = require('..'); | ||
const {equality} = require('./func'); | ||
|
||
const Maybe = module.exports = taggedSum({ | ||
Just: ['x'], | ||
Nothing: [], | ||
}); | ||
|
||
Maybe.prototype[fl.equals] = function(b) { | ||
return this.cata({ | ||
Just: x => this.cata({ | ||
Just: y => equality(x, y), | ||
Nothing: () => false, | ||
}), | ||
Nothing: () => this.cata({ | ||
Just: _ => false, | ||
Nothing: () => true, | ||
}), | ||
}); | ||
}; | ||
|
||
Maybe.prototype[fl.chain] = function(f) { | ||
return this.cata({ | ||
Just: x => f(x), | ||
Nothing: () => this, | ||
}); | ||
}; | ||
|
||
Maybe.prototype[fl.ap] = function(m) { | ||
return m[fl.chain](f => this[fl.map](f)); | ||
}; | ||
|
||
Maybe[fl.of] = Maybe.Just; | ||
|
||
Maybe.prototype[fl.map] = function(f) { | ||
return this[fl.chain](a => Maybe[fl.of](f(a))); | ||
}; | ||
|
||
Maybe[fl.zero] = () => Maybe.Nothing; | ||
|
||
Maybe.prototype[fl.alt] = function(b) { | ||
return this.cata({ | ||
Just: _ => this, | ||
Nothing: () => b, | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
'use strict'; | ||
|
||
const fl = require('..'); | ||
|
||
module.exports = () => { | ||
String.prototype[fl.concat] = String.prototype.concat; | ||
Array.prototype[fl.equals] = function(y) { | ||
return this.length === y.length && this.join('') === y.join(''); | ||
}; | ||
Array.prototype[fl.map] = Array.prototype.map; | ||
Array.prototype[fl.reduce] = Array.prototype.reduce; | ||
Array.prototype[fl.concat] = Array.prototype.concat; | ||
Array.prototype[fl.traverse] = function(f, p) { | ||
return this.map(f)[fl.reduce]( | ||
(ys, x) => ys[fl.ap](x[fl.map](y => z => z[fl.concat](y))), | ||
p([]) | ||
); | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
'use strict'; | ||
|
||
const {tagged} = require('daggy'); | ||
|
||
const fl = require('..'); | ||
const {equality} = require('./func'); | ||
|
||
// Special type of sum for the type of string. | ||
const Sum = module.exports = tagged('v'); | ||
Sum[fl.of] = Sum; | ||
Sum[fl.empty] = () => Sum(''); | ||
Sum.prototype[fl.map] = function(f) { | ||
return Sum(f(this.v)); | ||
}; | ||
Sum.prototype[fl.concat] = function(x) { | ||
return Sum(this.v + x.v); | ||
}; | ||
Sum.prototype[fl.equals] = function(x) { | ||
return equality(this.v, x.v); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,14 +33,15 @@ | |
"url": "https://github.com/fantasyland/fantasy-land.git" | ||
}, | ||
"files": [ | ||
"id.js", | ||
"index.js", | ||
"internal/*.js", | ||
"laws/*.js" | ||
], | ||
"main": "index.js", | ||
"scripts": { | ||
"lint": "eslint --config node_modules/sanctuary-style/eslint-es6.json --env es6 --env node --rule 'max-len: [off]' -- *.js laws/*.js", | ||
"test": "npm run-script lint && nodeunit id_test.js", | ||
"lint": "eslint --config node_modules/sanctuary-style/eslint-es6.json --env es6 --env node --rule 'max-len: [off]' -- *.js laws/*.js internal/*.js", | ||
"unit": "nodeunit test.js", | ||
"test": "npm run-script lint && npm run-script unit", | ||
"release-major": "xyz --repo [email protected]:fantasyland/fantasy-land.git --increment major", | ||
"release-minor": "xyz --repo [email protected]:fantasyland/fantasy-land.git --increment minor", | ||
"release-patch": "xyz --repo [email protected]:fantasyland/fantasy-land.git --increment patch" | ||
|
Oops, something went wrong.