caolan / highland

High-level streams library for Node.js and the browser

Home Page:https://caolan.github.io/highland

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Process groups after `group` call separately and combine again.

benyamin-noori opened this issue · comments

The goal is to group items in a stream into multiple groups. Run transformations on those groups separately and then re-combine all the groups into one stream.

The only call I can use after group seems to be each and that doesn't pass individual groups to my callback it passes the entire dictionary of grouped objects. Calling map won't pass anything to my callback. Example:

const groups = stream.group(func);
groups.map(item => console.log(item)); // prints nothing
groups.each(item => console.log(item)); // instead of item being one of the groups created, it's a dictionary with all the groups included. 

Until you consume a stream, none of the actions you declare on a stream (ie: .map(item => console.log(item)) will be performed.

Try something like this, where you have only one each of declaration and usage of the variable groups, and your calls are chained:

const groups = _([1, 2, 3, 4])
  .group(x => x > 2)
  .flatMap(_.values)
  .map(group => (console.log(group), group)) // first { 'false': [ 1, 2 ] }, then { 'true': [ 3, 4 ] }

groups.each(item => console.log(item)) // first { 'false': [ 1, 2 ] }, then { 'true': [ 3, 4 ] }

note: (console.log('some message'), x) === x