Hyperlinkv0.8.0-beta.28

Iterable

Iterable.groupconsteffect/Iterable.ts:1297
<A>(self: Iterable<A>): Iterable<NonEmptyArray<A>>

Groups equal, consecutive elements of an Iterable into NonEmptyArrays.

Example (Grouping consecutive elements)

import { Iterable } from "effect"

const numbers = [1, 1, 2, 2, 2, 3, 1, 1]
const grouped = Iterable.group(numbers)
console.log(Array.from(grouped))
// [[1, 1], [2, 2, 2], [3], [1, 1]]

const letters = "aabbccaa"
const groupedLetters = Iterable.group(letters)
console.log(Array.from(groupedLetters))
// [["a", "a"], ["b", "b"], ["c", "c"], ["a", "a"]]

// Works with objects using deep equality
const objects = [
  { type: "A", value: 1 },
  { type: "A", value: 1 },
  { type: "B", value: 2 },
  { type: "A", value: 1 }
]
const groupedObjects = Iterable.group(objects)
console.log(Array.from(groupedObjects).length) // 3 groups
// Note: Only consecutive equal objects are grouped together
grouping
export const group: <A>(self: Iterable<A>) => Iterable<NonEmptyArray<A>> = groupWith(
  Equal.asEquivalence()
)