Skip to content

Consider groupBy generic operation #3

Open
@souenzzo

Description

@souenzzo

It's a bit more generic operation "partition"

let groupBy = (f, coll) => {
  let rf = (acc, el) => {
    let id = f(el);
    acc[id] = [... (acc[id] || []), el]
    return acc
  }
  return coll.reduce(rf , {})
}

Can be used as partition as:

let {false: f, true: t} = groupBy( x => x == 2 , [1,2,3])

Maybe should we accumulate over an actual Map allowing f return any object (?!)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions