Hyperlinkv0.8.0-beta.28

Array

Array.zipWithconsteffect/Array.ts:2240
<B, A, C>(that: NonEmptyReadonlyArray<B>, f: (a: A, b: B) => C): (
  self: NonEmptyReadonlyArray<A>
) => NonEmptyArray<C>
<B, A, C>(that: Iterable<B>, f: (a: A, b: B) => C): (
  self: Iterable<A>
) => Array<C>
<A, B, C>(
  self: NonEmptyReadonlyArray<A>,
  that: NonEmptyReadonlyArray<B>,
  f: (a: A, b: B) => C
): NonEmptyArray<C>
<B, A, C>(
  self: Iterable<A>,
  that: Iterable<B>,
  f: (a: A, b: B) => C
): Array<C>

Combines elements from two iterables pairwise using a function. If the iterables differ in length, extra elements are discarded.

When to use

Use when zipping two iterables in an array pipeline and each pair should become a computed array element instead of a tuple.

Example (Zipping with addition)

import { Array } from "effect"

console.log(Array.zipWith([1, 2, 3], [4, 5, 6], (a, b) => a + b)) // [5, 7, 9]
zippingzip
Source effect/Array.ts:224018 lines
export const zipWith: {
  <B, A, C>(that: NonEmptyReadonlyArray<B>, f: (a: A, b: B) => C): (self: NonEmptyReadonlyArray<A>) => NonEmptyArray<C>
  <B, A, C>(that: Iterable<B>, f: (a: A, b: B) => C): (self: Iterable<A>) => Array<C>
  <A, B, C>(self: NonEmptyReadonlyArray<A>, that: NonEmptyReadonlyArray<B>, f: (a: A, b: B) => C): NonEmptyArray<C>
  <B, A, C>(self: Iterable<A>, that: Iterable<B>, f: (a: A, b: B) => C): Array<C>
} = dual(3, <B, A, C>(self: Iterable<A>, that: Iterable<B>, f: (a: A, b: B) => C): Array<C> => {
  const as = fromIterable(self)
  const bs = fromIterable(that)
  if (isReadonlyArrayNonEmpty(as) && isReadonlyArrayNonEmpty(bs)) {
    const out: NonEmptyArray<C> = [f(headNonEmpty(as), headNonEmpty(bs))]
    const len = Math.min(as.length, bs.length)
    for (let i = 1; i < len; i++) {
      out[i] = f(as[i], bs[i])
    }
    return out
  }
  return []
})
Referenced by 2 symbols