Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

number: flip S.sub argument order, and remove S.dec and S.inc #391

Merged
merged 1 commit into from
May 13, 2017
Merged
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
96 changes: 48 additions & 48 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,23 +167,25 @@
//. silent failures due to type coercion (at worst). For example:
//.
//. ```javascript
//. S.inc('XXX');
//. S.add(2, true);
//. // ! TypeError: Invalid value
//. //
//. // inc :: FiniteNumber -> FiniteNumber
//. // ^^^^^^^^^^^^
//. // 1
//. // add :: FiniteNumber -> FiniteNumber -> FiniteNumber
//. // ^^^^^^^^^^^^
//. // 1
//. //
//. // 1) "XXX" :: String
//. // 1) true :: Boolean
//. //
//. // The value at position 1 is not a member of ‘FiniteNumber’.
//. //
//. // See https://github.com/sanctuary-js/sanctuary-def/tree/v0.11.0#FiniteNumber for information about the sanctuary-def/FiniteNumber type.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like having to remember to update this each time we upgrade the sanctuary-def dependency.

//. ```
//.
//. Compare this to the behaviour of Ramda's unchecked equivalent:
//.
//. ```javascript
//. R.inc('XXX');
//. // => NaN
//. R.add(2, true);
//. // => 3
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this example even more. 🤣

//. ```
//.
//. There is a performance cost to run-time type checking. One may wish to
Expand Down Expand Up @@ -236,6 +238,15 @@
// Thunk :: Type -> Type
function Thunk(x) { return $.Function([x]); }

// flip$ :: ((a, b) -> c) -> b -> a -> c
function flip$(f) {
return function(x) {
return function(y) {
return f(y, x);
};
};
}

// typeEq :: String -> a -> Boolean
function typeEq(typeIdent) {
return function(x) {
Expand Down Expand Up @@ -628,7 +639,7 @@
//. (b -> c) -> (a -> b) -> (a -> c)
//.
//. ```javascript
//. > S.map(Math.sqrt, S.inc)(99)
//. > S.map(Math.sqrt, S.add(1))(99)
//. 10
//. ```
S.map = def('map', {f: [Z.Functor]}, [Fn(a, b), f(a), f(b)], Z.map);
Expand All @@ -655,7 +666,7 @@
//. Curried version of [`Z.promap`][].
//.
//. ```javascript
//. > S.promap(Math.abs, S.inc, Math.sqrt)(-100)
//. > S.promap(Math.abs, S.add(1), Math.sqrt)(-100)
//. 11
//. ```
S.promap =
Expand Down Expand Up @@ -785,7 +796,7 @@
//. > S.ap([Math.sqrt, x => x * x], [1, 4, 9, 16, 25])
//. [1, 2, 3, 4, 5, 1, 16, 81, 256, 625]
//.
//. > S.ap({x: Math.sqrt, y: S.inc, z: S.dec}, {w: 4, x: 4, y: 4})
//. > S.ap({x: Math.sqrt, y: S.add(1), z: S.sub(1)}, {w: 4, x: 4, y: 4})
//. {x: 2, y: 5}
//.
//. > S.ap(S.Just(Math.sqrt), S.Just(64))
Expand Down Expand Up @@ -1154,10 +1165,10 @@
//. function.
//.
//. ```javascript
//. > S.A(S.inc, 42)
//. > S.A(S.add(1), 42)
//. 43
//.
//. > S.map(S.A(S.__, 100), [S.inc, Math.sqrt])
//. > S.map(S.A(S.__, 100), [S.add(1), Math.sqrt])
//. [101, 10]
//. ```
function A(f, x) {
Expand All @@ -1172,10 +1183,10 @@
//. `(&)` function.
//.
//. ```javascript
//. > S.T(42, S.inc)
//. > S.T(42, S.add(1))
//. 43
//.
//. > S.map(S.T(100), [S.inc, Math.sqrt])
//. > S.map(S.T(100), [S.add(1), Math.sqrt])
//. [101, 10]
//. ```
function T(x, f) {
Expand Down Expand Up @@ -1320,7 +1331,7 @@
//. See also [`pipe`](#pipe).
//.
//. ```javascript
//. > S.compose(Math.sqrt, S.inc)(99)
//. > S.compose(Math.sqrt, S.add(1), 99)
//. 10
//. ```
function compose(f, g, x) {
Expand All @@ -1338,7 +1349,7 @@
//. of functions. `pipe([f, g, h], x)` is equivalent to `h(g(f(x)))`.
//.
//. ```javascript
//. > S.pipe([S.inc, Math.sqrt, S.dec])(99)
//. > S.pipe([S.add(1), Math.sqrt, S.sub(1)], 99)
//. 9
//. ```
function pipe(fs, x) {
Expand Down Expand Up @@ -2219,10 +2230,10 @@
//. left side as well as the right side.
//.
//. ```javascript
//. > S.bimap(S.toUpper, S.inc, S.Left('abc'))
//. > S.bimap(S.toUpper, S.add(1), S.Left('abc'))
//. Left('ABC')
//.
//. > S.bimap(S.toUpper, S.inc, S.Right(42))
//. > S.bimap(S.toUpper, S.add(1), S.Right(42))
//. Right(43)
//. ```
Either.prototype['fantasy-land/bimap'] = function(f, g) {
Expand Down Expand Up @@ -2498,9 +2509,6 @@
//
//. > S.tagBy(S.odd, 1)
//. Right(1)
//
//. > S.compose(S.bimap(S.dec, S.inc), S.tagBy(S.odd))(5)
//. Right(6)
//. ```
function tagBy(pred, a) {
return pred(a) ? Right(a) : Left(a);
Expand Down Expand Up @@ -3527,45 +3535,37 @@
S.sum =
def('sum', {f: [Z.Foldable]}, [f($.FiniteNumber), $.FiniteNumber], sum);

//# sub :: FiniteNumber -> FiniteNumber -> FiniteNumber
//# sub :: FiniteNumber -> (FiniteNumber -> FiniteNumber)
//.
//. Returns the difference between two (finite) numbers.
//. Takes a finite number `n` and returns the _subtract `n`_ function.
//.
//. See also [`sub_`](#sub_).
//.
//. ```javascript
//. > S.sub(4, 2)
//. 2
//. > S.map(S.sub(1), [1, 2, 3])
//. [0, 1, 2]
//. ```
function sub(x, y) {
return x - y;
}
S.sub =
def('sub', {}, [$.FiniteNumber, $.FiniteNumber, $.FiniteNumber], sub);
def('sub',
{},
[$.FiniteNumber, Fn($.FiniteNumber, $.FiniteNumber)],
flip$(sub_));

//# inc :: FiniteNumber -> FiniteNumber
//.
//. Increments a (finite) number by one.
//# sub_ :: FiniteNumber -> FiniteNumber -> FiniteNumber
//.
//. ```javascript
//. > S.inc(1)
//. 2
//. ```
function inc(x) {
return x + 1;
}
S.inc = def('inc', {}, [$.FiniteNumber, $.FiniteNumber], inc);

//# dec :: FiniteNumber -> FiniteNumber
//. Returns the difference between two (finite) numbers.
//.
//. Decrements a (finite) number by one.
//. See also [`sub`](#sub).
//.
//. ```javascript
//. > S.dec(2)
//. 1
//. > S.sub_(4, 2)
//. 2
//. ```
function dec(x) {
return x - 1;
function sub_(x, y) {
return x - y;
}
S.dec = def('dec', {}, [$.FiniteNumber, $.FiniteNumber], dec);
S.sub_ =
def('sub_', {}, [$.FiniteNumber, $.FiniteNumber, $.FiniteNumber], sub_);

//# mult :: FiniteNumber -> FiniteNumber -> FiniteNumber
//.
Expand Down
4 changes: 2 additions & 2 deletions test/A.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ test('A', function() {
eq(S.A.length, 2);
eq(S.A.toString(), 'A :: (a -> b) -> a -> b');

eq(S.A(S.inc, 1), 2);
eq(map(S.A(S.__, 100))([S.inc, Math.sqrt]), [101, 10]);
eq(S.A(S.add(1), 1), 2);
eq(map(S.A(S.__, 100))([S.add(1), Math.sqrt]), [101, 10]);

});
4 changes: 2 additions & 2 deletions test/Either/Left.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ suite('Left', function() {
test('"fantasy-land/ap" method', function() {
eq(S.Left('abc')[FL.ap].length, 1);
eq(S.Left('abc')[FL.ap](S.Left('xyz')), S.Left('xyz'));
eq(S.Left('abc')[FL.ap](S.Right(S.inc)), S.Left('abc'));
eq(S.Left('abc')[FL.ap](S.Right(S.add(1))), S.Left('abc'));
});

test('"fantasy-land/bimap" method', function() {
eq(S.Left('abc')[FL.bimap].length, 2);
eq(S.Left('abc')[FL.bimap](S.toUpper, S.inc), S.Left('ABC'));
eq(S.Left('abc')[FL.bimap](S.toUpper, S.add(1)), S.Left('ABC'));
});

test('"fantasy-land/chain" method', function() {
Expand Down
4 changes: 2 additions & 2 deletions test/Either/Right.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ suite('Right', function() {
test('"fantasy-land/ap" method', function() {
eq(S.Right(42)[FL.ap].length, 1);
eq(S.Right(42)[FL.ap](S.Left('abc')), S.Left('abc'));
eq(S.Right(42)[FL.ap](S.Right(S.inc)), S.Right(43));
eq(S.Right(42)[FL.ap](S.Right(S.add(1))), S.Right(43));
});

test('"fantasy-land/bimap" method', function() {
eq(S.Right(42)[FL.bimap].length, 2);
eq(S.Right(42)[FL.bimap](S.toUpper, S.inc), S.Right(43));
eq(S.Right(42)[FL.bimap](S.toUpper, S.add(1)), S.Right(43));
});

test('"fantasy-land/chain" method', function() {
Expand Down
2 changes: 1 addition & 1 deletion test/Maybe/Just.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ suite('Just', function() {
test('"fantasy-land/ap" method', function() {
eq(S.Just(42)[FL.ap].length, 1);
eq(S.Just(42)[FL.ap](S.Nothing), S.Nothing);
eq(S.Just(42)[FL.ap](S.Just(S.inc)), S.Just(43));
eq(S.Just(42)[FL.ap](S.Just(S.add(1))), S.Just(43));
});

test('"fantasy-land/chain" method', function() {
Expand Down
2 changes: 1 addition & 1 deletion test/Maybe/Nothing.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ suite('Nothing', function() {
test('"fantasy-land/ap" method', function() {
eq(S.Nothing[FL.ap].length, 1);
eq(S.Nothing[FL.ap](S.Nothing), S.Nothing);
eq(S.Nothing[FL.ap](S.Just(S.inc)), S.Nothing);
eq(S.Nothing[FL.ap](S.Just(S.add(1))), S.Nothing);
});

test('"fantasy-land/chain" method', function() {
Expand Down
4 changes: 2 additions & 2 deletions test/T.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ test('T', function() {
eq(S.T.length, 2);
eq(S.T.toString(), 'T :: a -> (a -> b) -> b');

eq(S.T(42, S.inc), 43);
eq(map(S.T(100))([S.inc, Math.sqrt]), [101, 10]);
eq(S.T(42, S.add(1)), 43);
eq(map(S.T(100))([S.add(1), Math.sqrt]), [101, 10]);

});
12 changes: 6 additions & 6 deletions test/ap.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ test('ap', function() {

eq(S.ap([], []), []);
eq(S.ap([], [1, 2, 3]), []);
eq(S.ap([S.inc], []), []);
eq(S.ap([S.inc], [1, 2, 3]), [2, 3, 4]);
eq(S.ap([S.dec, Math.sqrt], [1, 4, 9]), [0, 3, 8, 1, 2, 3]);
eq(S.ap([S.add(1)], []), []);
eq(S.ap([S.add(1)], [1, 2, 3]), [2, 3, 4]);
eq(S.ap([S.sub(1), Math.sqrt], [1, 4, 9]), [0, 3, 8, 1, 2, 3]);
eq(S.ap({}, {}), {});
eq(S.ap({}, {x: 1, y: 2, z: 3}), {});
eq(S.ap({x: S.inc}, {}), {});
eq(S.ap({x: S.inc}, {x: 1}), {x: 2});
eq(S.ap({x: S.inc, y: S.dec, z: Math.sqrt}, {w: 0, x: 1, y: 2}), {x: 2, y: 1});
eq(S.ap({x: S.add(1)}, {}), {});
eq(S.ap({x: S.add(1)}, {x: 1}), {x: 2});
eq(S.ap({x: S.add(1), y: S.sub(1), z: Math.sqrt}, {w: 0, x: 1, y: 2}), {x: 2, y: 1});
eq(S.ap(S.Nothing, S.Nothing), S.Nothing);
eq(S.ap(S.Nothing, S.Just(9)), S.Nothing);
eq(S.ap(S.Just(Math.sqrt), S.Nothing), S.Nothing);
Expand Down
4 changes: 2 additions & 2 deletions test/bimap.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ test('bimap', function() {
eq(S.bimap.length, 3);
eq(S.bimap.toString(), 'bimap :: Bifunctor p => (a -> b) -> (c -> d) -> p a c -> p b d');

eq(S.bimap(S.toUpper, S.inc, S.Left('xxx')), S.Left('XXX'));
eq(S.bimap(S.toUpper, S.inc, S.Right(1000)), S.Right(1001));
eq(S.bimap(S.toUpper, S.add(1), S.Left('xxx')), S.Left('XXX'));
eq(S.bimap(S.toUpper, S.add(1), S.Right(1000)), S.Right(1001));

});
2 changes: 1 addition & 1 deletion test/compose.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ test('compose', function() {
eq(S.compose.length, 3);
eq(S.compose.toString(), 'compose :: (b -> c) -> (a -> b) -> a -> c');

eq(S.compose(S.mult(2), S.inc, 20), 42);
eq(S.compose(S.mult(2), S.add(1), 20), 42);

});
4 changes: 2 additions & 2 deletions test/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ test('create', function() {
eq(Object.keys(uncheckedDefaultEnv).sort(), expected);
eq(Object.keys(uncheckedCustomEnv).sort(), expected);

eq(uncheckedDefaultEnv.inc(42), S.inc(42));
eq(uncheckedDefaultEnv.inc('XXX'), 'XXX1');
eq(uncheckedDefaultEnv.add(1, 42), S.add(1, 42));
eq(uncheckedDefaultEnv.add(1, 'XXX'), '1XXX');

throws(function() { S.I(['foo', 'foo', 42]); },
TypeError,
Expand Down
19 changes: 0 additions & 19 deletions test/dec.js

This file was deleted.

4 changes: 2 additions & 2 deletions test/ifElse.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ test('ifElse', function() {
eq(S.ifElse.length, 4);
eq(S.ifElse.toString(), 'ifElse :: (a -> Boolean) -> (a -> b) -> (a -> b) -> a -> b');

eq(S.ifElse(S.odd, S.dec, S.inc, 9), 8);
eq(S.ifElse(S.odd, S.dec, S.inc, 0), 1);
eq(S.ifElse(S.odd, S.sub(1), S.add(1), 9), 8);
eq(S.ifElse(S.odd, S.sub(1), S.add(1), 0), 1);

});
19 changes: 0 additions & 19 deletions test/inc.js

This file was deleted.

2 changes: 1 addition & 1 deletion test/lift3.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ test('lift3', function() {
eq(S.lift3(S.reduce, [S.add], [0], [[1, 2, 3]]), [6]);
eq(S.lift3(S.reduce, [S.add], [0], []), []);

eq(S.lift3(area, S.dec, S.I, S.inc)(4), 6);
eq(S.lift3(area, S.sub(1), S.I, S.add(1))(4), 6);

});
6 changes: 3 additions & 3 deletions test/pipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ test('pipe', function() {

eq(S.pipe([], '99'), '99');
eq(S.pipe([parseInt], '99'), 99);
eq(S.pipe([parseInt, S.inc], '99'), 100);
eq(S.pipe([parseInt, S.inc, Math.sqrt], '99'), 10);
eq(S.pipe([parseInt, S.inc, Math.sqrt, S.dec], '99'), 9);
eq(S.pipe([parseInt, S.add(1)], '99'), 100);
eq(S.pipe([parseInt, S.add(1), Math.sqrt], '99'), 10);
eq(S.pipe([parseInt, S.add(1), Math.sqrt, S.sub(1)], '99'), 9);

});
Loading