Hyperlinkv0.8.0-beta.28

Array

Array.dedupeWithconsteffect/Array.ts:4392
<S extends Iterable<any>>(
  isEquivalent: (
    self: ReadonlyArray.Infer<S>,
    that: ReadonlyArray.Infer<S>
  ) => boolean
): (self: S) => ReadonlyArray.With<S, ReadonlyArray.Infer<S>>
<A>(
  self: NonEmptyReadonlyArray<A>,
  isEquivalent: (self: A, that: A) => boolean
): NonEmptyArray<A>
<A>(
  self: Iterable<A>,
  isEquivalent: (self: A, that: A) => boolean
): Array<A>

Removes duplicates using a custom equivalence, preserving the order of the first occurrence.

When to use

Use to remove all duplicate elements with a custom equivalence when default equality is not appropriate.

Example (Deduplicating with custom equality)

import { Array } from "effect"

console.log(Array.dedupeWith([1, 2, 2, 3, 3, 3], (a, b) => a === b)) // [1, 2, 3]
Source effect/Array.ts:439223 lines
export const dedupeWith: {
  <S extends Iterable<any>>(
    isEquivalent: (self: ReadonlyArray.Infer<S>, that: ReadonlyArray.Infer<S>) => boolean
  ): (self: S) => ReadonlyArray.With<S, ReadonlyArray.Infer<S>>
  <A>(self: NonEmptyReadonlyArray<A>, isEquivalent: (self: A, that: A) => boolean): NonEmptyArray<A>
  <A>(self: Iterable<A>, isEquivalent: (self: A, that: A) => boolean): Array<A>
} = dual(
  2,
  <A>(self: Iterable<A>, isEquivalent: (self: A, that: A) => boolean): Array<A> => {
    const input = fromIterable(self)
    if (isReadonlyArrayNonEmpty(input)) {
      const out: NonEmptyArray<A> = [headNonEmpty(input)]
      const rest = tailNonEmpty(input)
      for (const r of rest) {
        if (out.every((a) => !isEquivalent(r, a))) {
          out.push(r)
        }
      }
      return out
    }
    return []
  }
)
Referenced by 3 symbols