-
-
Notifications
You must be signed in to change notification settings - Fork 95
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
//. ``` | ||
//. | ||
//. Compare this to the behaviour of Ramda's unchecked equivalent: | ||
//. | ||
//. ```javascript | ||
//. R.inc('XXX'); | ||
//. // => NaN | ||
//. R.add(2, true); | ||
//. // => 3 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
@@ -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) { | ||
|
@@ -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); | ||
|
@@ -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 = | ||
|
@@ -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)) | ||
|
@@ -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) { | ||
|
@@ -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) { | ||
|
@@ -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) { | ||
|
@@ -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) { | ||
|
@@ -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) { | ||
|
@@ -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); | ||
|
@@ -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 | ||
//. | ||
|
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
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
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
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
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
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
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
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.