Hyperlinkv0.8.0-beta.28

Array

Array.groupWithconsteffect/Array.ts:2971
<A>(isEquivalent: (self: A, that: A) => boolean): (
  self: NonEmptyReadonlyArray<A>
) => NonEmptyArray<NonEmptyArray<A>>
<A>(
  self: NonEmptyReadonlyArray<A>,
  isEquivalent: (self: A, that: A) => boolean
): NonEmptyArray<NonEmptyArray<A>>

Groups consecutive equal elements using a custom equivalence function.

When to use

Use when you already have a non-empty array arranged so matching elements are adjacent and need a custom equivalence function.

Details

Only adjacent elements are grouped. Non-adjacent duplicates stay separate. Requires a NonEmptyReadonlyArray.

Example (Grouping consecutive equal elements)

import { Array } from "effect"

console.log(Array.groupWith(["a", "a", "b", "b", "b", "c", "a"], (x, y) => x === y))
// [["a", "a"], ["b", "b", "b"], ["c"], ["a"]]
groupinggroupgroupBy
Source effect/Array.ts:297121 lines
export const groupWith: {
  <A>(isEquivalent: (self: A, that: A) => boolean): (self: NonEmptyReadonlyArray<A>) => NonEmptyArray<NonEmptyArray<A>>
  <A>(self: NonEmptyReadonlyArray<A>, isEquivalent: (self: A, that: A) => boolean): NonEmptyArray<NonEmptyArray<A>>
} = dual(
  2,
  <A>(self: NonEmptyReadonlyArray<A>, isEquivalent: (self: A, that: A) => boolean): NonEmptyArray<NonEmptyArray<A>> =>
    chop(self, (as) => {
      const h = headNonEmpty(as)
      const out: NonEmptyArray<A> = [h]
      let i = 1
      for (; i < as.length; i++) {
        const a = as[i]
        if (isEquivalent(a, h)) {
          out.push(a)
        } else {
          break
        }
      }
      return [out, as.slice(i)]
    })
)
Referenced by 1 symbols