Hyperlinkv0.8.0-beta.28

Array

Array.unionWithconsteffect/Array.ts:3106
<S extends Iterable<any>, T extends Iterable<any>>(
  that: T,
  isEquivalent: (
    self: ReadonlyArray.Infer<S>,
    that: ReadonlyArray.Infer<T>
  ) => boolean
): (
  self: S
) => ReadonlyArray.OrNonEmpty<
  S,
  T,
  ReadonlyArray.Infer<S> | ReadonlyArray.Infer<T>
>
<A, B>(
  self: NonEmptyReadonlyArray<A>,
  that: Iterable<B>,
  isEquivalent: (self: A, that: B) => boolean
): NonEmptyArray<A | B>
<A, B>(
  self: Iterable<A>,
  that: NonEmptyReadonlyArray<B>,
  isEquivalent: (self: A, that: B) => boolean
): NonEmptyArray<A | B>
<A, B>(
  self: Iterable<A>,
  that: Iterable<B>,
  isEquivalent: (self: A, that: B) => boolean
): Array<A | B>

Computes the union of two arrays using a custom equivalence, removing duplicates.

When to use

Use when you need the union of two arrays but duplicate detection must use a custom equivalence instead of the default Equal.equivalence().

Example (Computing unions with custom equality)

import { Array } from "effect"

console.log(Array.unionWith([1, 2], [2, 3], (a, b) => a === b)) // [1, 2, 3]
Source effect/Array.ts:310628 lines
export const unionWith: {
  <S extends Iterable<any>, T extends Iterable<any>>(
    that: T,
    isEquivalent: (self: ReadonlyArray.Infer<S>, that: ReadonlyArray.Infer<T>) => boolean
  ): (self: S) => ReadonlyArray.OrNonEmpty<S, T, ReadonlyArray.Infer<S> | ReadonlyArray.Infer<T>>
  <A, B>(
    self: NonEmptyReadonlyArray<A>,
    that: Iterable<B>,
    isEquivalent: (self: A, that: B) => boolean
  ): NonEmptyArray<A | B>
  <A, B>(
    self: Iterable<A>,
    that: NonEmptyReadonlyArray<B>,
    isEquivalent: (self: A, that: B) => boolean
  ): NonEmptyArray<A | B>
  <A, B>(self: Iterable<A>, that: Iterable<B>, isEquivalent: (self: A, that: B) => boolean): Array<A | B>
} = dual(3, <A>(self: Iterable<A>, that: Iterable<A>, isEquivalent: (self: A, that: A) => boolean): Array<A> => {
  const a = fromIterable(self)
  const b = fromIterable(that)
  if (isReadonlyArrayNonEmpty(a)) {
    if (isReadonlyArrayNonEmpty(b)) {
      const dedupe = dedupeWith(isEquivalent)
      return dedupe(appendAll(a, b))
    }
    return a
  }
  return b
})
Referenced by 1 symbols