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

Proposed documentation and unit tests for #2396 #2401

Open
wants to merge 2 commits 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 19 additions & 8 deletions source/applySpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,20 @@ import values from './values';


/**
* Given a spec object recursively mapping properties to functions, creates a
* function producing an object of the same structure, by mapping each property
* to the result of calling its associated function with the supplied arguments.
* Given a spec hierarchy of objects and/or arrays recursively mapping properties/elements
* to functions, creates a value of the same structure, by mapping each
* property/element to the result of calling its associated function with
* the supplied arguments.
*
* @func
* @memberOf R
* @since v0.20.0
* @category Function
* @sig {k: ((a, b, ..., m) -> v)} -> ((a, b, ..., m) -> {k: v})
* @param {Object} spec an object recursively mapping properties to functions for
* producing the values for these properties.
* @return {Function} A function that returns an object of the same structure
* as `spec', with each property set to the value returned by calling its
* @sig {k: ((a, b, ..., m) -> v)} | [((a, b, ..., m) -> v)] -> ((a, b, ..., m) -> {k: v} | [v])
Copy link
Member

Choose a reason for hiding this comment

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

I'm not really sure how to write a good signature for this with its recursive nature as well as the variadic functions. But this one simply confuses me.

I don't know if we're better with a list of related signatures, but they would at least individually be much simpler:

applySpec :: {k: a -> v} -> (a -> {k: v}
          :: {k: (a, b) -> v} -> ((a, b) -> {k, v})
          :: {k: (a, b, c) -> v} -> ((a, b, c) -> {k, v})
          :: ...
          :: {[(a -> v)] -> (a -> [v]) 
          :: {[((a, b) -> v)] -> ((a, b) -> [v]) 
          :: {[((a, b, c) -> v)] -> ((a, b, c) -> [v]) 
          :: ...

But that still does not capture the recursive nature of this. I now very much regret that this is recursive; we seem to have abandoned our focus on simplicity here.

* @param {Object} spec a hierarchy of objects and/or arrays recursively mapping properties/elements
* to functions for producing the values for these properties/elements.
* @return {Function} A function that returns a value with the same structure
* as `spec', with each property/element set to the value returned by calling its
* associated function with the supplied arguments.
* @see R.converge, R.juxt
* @example
Expand All @@ -31,6 +32,16 @@ import values from './values';
* nested: { mul: R.multiply }
* });
* getMetrics(2, 4); // => { sum: 6, nested: { mul: 8 } }
*
* var beforeCurrentAndAfter = R.applySpec([R.add(-1), R.identity, R.add(1)]);
* beforeCurrentAndAfter(3); // => [2, 3, 4];
*
* var operations = R.applySpec([
* { type: R.always('add'), value: R.add },
* { type: R.always('multiply'), value: R.multiply }
* ]);
* operations(2, 4); // => [ { type: 'add', value: 6 }, { type: 'multiply', value: 8 } ];
*
* @symb R.applySpec({ x: f, y: { z: g } })(a, b) = { x: f(a, b), y: { z: g(a, b) } }
*/
var applySpec = _curry1(function applySpec(spec) {
Expand Down
23 changes: 23 additions & 0 deletions test/applySpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,27 @@ describe('applySpec', function() {
eq(R.applySpec({ sum: R.add })(1)(2), { sum: 3 });
});

it('works with arrays', function() {
eq(R.applySpec([R.add(-1), R.identity, R.add(1)])(3), [2, 3, 4]);
});

it('works with arrays of objects', function() {
eq(
R.applySpec([
{ type: R.always('add'), value: R.add },
{ type: R.always('multiply'), value: R.multiply }
])(2, 4),
[{ type: 'add', value: 6 }, { type: 'multiply', value: 8 }]
);
});

it('works with objects containing arrays', function() {
eq(
R.applySpec({
sums: [R.add(1), R.add(2), R.add(3)],
products: [R.multiply(2), R.multiply(3), R.multiply(4)]
})(3),
{ sums: [4, 5, 6], products: [6, 9, 12] }
);
});
});