Hyperlinkv0.8.0-beta.28

Array

Array.dedupeAdjacentWithconsteffect/Array.ts:4472
<A>(isEquivalent: (self: A, that: A) => boolean): (
  self: Iterable<A>
) => Array<A>
<A>(
  self: Iterable<A>,
  isEquivalent: (self: A, that: A) => boolean
): Array<A>

Removes consecutive duplicate elements using a custom equivalence.

When to use

Use when consecutive duplicates should be collapsed using a custom equivalence, while equivalent values that appear later should remain in the result.

Details

Non-adjacent duplicates are preserved.

Example (Deduplicating adjacent elements)

import { Array } from "effect"

console.log(Array.dedupeAdjacentWith([1, 1, 2, 2, 3, 3], (a, b) => a === b))
// [1, 2, 3]
Source effect/Array.ts:447214 lines
export const dedupeAdjacentWith: {
  <A>(isEquivalent: (self: A, that: A) => boolean): (self: Iterable<A>) => Array<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 out: Array<A> = []
  let lastA: Option.Option<A> = Option.none()
  for (const a of self) {
    if (Option.isNone(lastA) || !isEquivalent(a, lastA.value)) {
      out.push(a)
      lastA = Option.some(a)
    }
  }
  return out
})
Referenced by 1 symbols