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 an absent absent/nullish accumulator, rather than pasing an
empty on to reduceBy, appears to correct the cached copy behavior
causing pollution in the accumulators on subsequent invocations, as well
as the "passthrough"; I believe this is due due the cached
shallow-copying combined with the currying of `reduceBy`.

It may be more desirable to correct the copy caching to prevent the
error while still deterministically setting the accumulator, but for
what it's worth, it's at least in line with traditional transducer
behavior to get the initial value from the transformer, albeit not
*quite* this way!
  • Loading branch information
jaawerth committed Feb 8, 2022
1 parent c2cafc2 commit 3056747
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 3056747

Please sign in to comment.