Skip to content

Commit

Permalink
bugfix: groupBy transducer functionality
Browse files Browse the repository at this point in the history
Fixes ramda#3232
Reverts code change in #9b5d8925

Setting the accumulator when not
prsent rather than passing it tin to reducerby; this prevents the cached
copy behavior causing polution of accumulators in subsequent invocations.

It may be more desirable to correct the copy caching to prevent the
error while still deterministically setting the accumulator; but it's in
line with traditional transducer behavior to get the initial accumulator
value from the transformer, albeit not quite in this fashion.
  • Loading branch information
jaawerth committed Feb 8, 2022
1 parent c2cafc2 commit fc6b659
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
5 changes: 4 additions & 1 deletion source/groupBy.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ import reduceBy from './reduceBy.js';
* // }
*/
var groupBy = _curry2(_checkForMethod('groupBy', reduceBy(function(acc, item) {
if (acc == null) {
acc = [];
}
acc.push(item);
return acc;
}, [])));
}, null)));
export default groupBy;
8 changes: 8 additions & 0 deletions test/groupBy.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,12 @@ describe('groupBy', function() {
eq(_isTransformer(R.groupBy(byType, xf)), true);
});

it('works as a transducer without polluting state', function() {
eq(R.into([], R.groupBy(x => x.length), ['a', 'xyz', 'ab', 'xy', 'abc', 'x']), [
[1, ['a', 'x']],
[2, ['ab', 'xy']],
[3, ['xyz', 'abc']]
]);
eq(R.groupBy(x => x.length, ['a', 'xyz', 'ab', 'xy', 'abc', 'x']), {1: ['a', 'x'], 2: ['ab', 'xy'], 3: ['xyz', 'abc']});
});
});

0 comments on commit fc6b659

Please sign in to comment.