<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 togethergrouping
Source effect/Iterable.ts:12973 lines
export const const group: <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
group: <function (type parameter) A in <A>(self: Iterable<A>): Iterable<NonEmptyArray<A>>A>(self: Iterable<A>self: interface Iterable<T, TReturn = any, TNext = any>Iterable<function (type parameter) A in <A>(self: Iterable<A>): Iterable<NonEmptyArray<A>>A>) => interface Iterable<T, TReturn = any, TNext = any>Iterable<type NonEmptyArray<A> = [A, ...A[]]A mutable array guaranteed to have at least one element.
When to use
Use when mutation is acceptable and non-emptiness must be tracked at the type
level.
Details
This is the mutable counterpart of
NonEmptyReadonlyArray
. Most Array
module functions return NonEmptyArray when the result is guaranteed
non-empty.
Example (Typing a mutable non-empty array)
import type { Array } from "effect"
const nonEmpty: Array.NonEmptyArray<number> = [1, 2, 3]
nonEmpty.push(4)
NonEmptyArray<function (type parameter) A in <A>(self: Iterable<A>): Iterable<NonEmptyArray<A>>A>> = const groupWith: {
<A>(
isEquivalent: (self: A, that: A) => boolean
): (
self: Iterable<A>
) => Iterable<NonEmptyArray<A>>
<A>(
self: Iterable<A>,
isEquivalent: (self: A, that: A) => boolean
): Iterable<NonEmptyArray<A>>
}
groupWith(
import EqualEqual.const asEquivalence: <
A
>() => Equivalence<A>
Wraps
equals
as an Equivalence<A>.
When to use
Use when you want to pass Equal.equals to APIs that require an
Equivalence.
Details
- Returns a function
(a: A, b: A) => boolean that delegates to
equals
.
- Pure; allocates a thin wrapper on each call.
Example (Deduplicating with Equal semantics)
import { Array, Equal } from "effect"
const eq = Equal.asEquivalence<number>()
const result = Array.dedupeWith([1, 2, 2, 3, 1], eq)
console.log(result) // [1, 2, 3]
asEquivalence()
)