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

Added function S.on #282

Merged
merged 1 commit into from
Nov 16, 2016
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
24 changes: 24 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,30 @@
}
S.pipe = def('pipe', {}, [$.Array($.Function), a, b], pipe);

//# on :: (b -> b -> c) -> (a -> b) -> a -> a -> c
//.
//. Takes a binary function `f`, a unary function `g`, and two
//. values `x` and `y`. Returns `f(g(x))(g(y))`.
//.
//. See also [`on_`](#on_).
//.
//. ```javascript
//. > S.on(S.concat, S.reverse, [1, 2, 3], [4, 5, 6])
//. [3, 2, 1, 6, 5, 4]
//. ```
function on(f, g, x, y) {
return on_(uncurry2(f), g, x, y);
}
S.on = def('on', {}, [$.Function, $.Function, a, a, c], on);

//# on_ :: ((b, b) -> c) -> (a -> b) -> a -> a -> c
//.
//. Version of [`on`](#on) accepting uncurried functions.
function on_(f, g, x, y) {
return f(g(x), g(y));
}
S.on_ = def('on_', {}, [$.Function, $.Function, a, a, c], on_);

//. ### Maybe type
//.
//. The Maybe type represents optional values: a value of type `Maybe a` is
Expand Down
21 changes: 21 additions & 0 deletions test/on.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict';

var S = require('..');

var eq = require('./internal/eq');
var rem = require('./internal/rem');


describe('on', function() {
Copy link
Member

Choose a reason for hiding this comment

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

Nit: Two empty lines after imports, please.


it('is a quaternary function', function() {
eq(typeof S.on, 'function');
eq(S.on.length, 4);
});

it('passes the last two arguments through the modifying function into the converging function', function() {
eq(S.on(rem, S.prop('x'), {x: 5, y: 5}, {x: 3, y: 3}), 2);
eq(S.on(S.concat, S.reverse)([1, 2, 3], [4, 5, 6]), [3, 2, 1, 6, 5, 4]);
Copy link
Member

Choose a reason for hiding this comment

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

Because Sanctuary functions can be applied to their arguments one at a time or all at once, they're not well suited to testing higher-order functions. We should use rem or rem_ to differentiate between f(x)(y) and f(x, y) in the implementation.

});

});
20 changes: 20 additions & 0 deletions test/on_.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict';

var S = require('..');

var eq = require('./internal/eq');
var rem_ = require('./internal/rem_');


describe('on_', function() {

Copy link
Member

Choose a reason for hiding this comment

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

Let's include an 'is a quaternary function' block for this function too.

it('is a quaternary function', function() {
eq(typeof S.on_, 'function');
eq(S.on_.length, 4);
});

it('passes the last two arguments through the modifying function into the converging function', function() {
eq(S.on_(rem_, S.prop('x'), {x: 5, y: 5}, {x: 3, y: 3}), 2);
});

});