Hyperlinkv0.8.0-beta.28

Iterable

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

Applies a function to pairs of elements at the same index in two Iterables, collecting the results. If one input Iterable is short, excess elements of the longer Iterable are discarded.

Example (Zipping with a combining function)

import { Iterable } from "effect"

// Add corresponding elements
const a = [1, 2, 3, 4]
const b = [10, 20, 30, 40]
const sums = Iterable.zipWith(a, b, (x, y) => x + y)
console.log(Array.from(sums)) // [11, 22, 33, 44]

// Combine strings
const firstNames = ["John", "Jane", "Bob"]
const lastNames = ["Doe", "Smith", "Johnson"]
const fullNames = Iterable.zipWith(
  firstNames,
  lastNames,
  (first, last) => `${first} ${last}`
)
console.log(Array.from(fullNames)) // ["John Doe", "Jane Smith", "Bob Johnson"]

// Different lengths - stops at shorter
const short = [1, 2]
const long = ["a", "b", "c", "d"]
const combined = Iterable.zipWith(
  short,
  long,
  (num, letter) => `${num}${letter}`
)
console.log(Array.from(combined)) // ["1a", "2b"]

// Complex transformations
const prices = [10.99, 25.50, 5.00]
const quantities = [2, 1, 3]
const totals = Iterable.zipWith(prices, quantities, (price, qty) => {
  return Math.round(price * qty * 100) / 100 // round to 2 decimal places
})
console.log(Array.from(totals)) // [21.98, 25.5, 15]
zipping
Source effect/Iterable.ts:93419 lines
export const zipWith: {
  <B, A, C>(that: Iterable<B>, f: (a: A, b: B) => C): (self: Iterable<A>) => Iterable<C>
  <A, B, C>(self: Iterable<A>, that: Iterable<B>, f: (a: A, b: B) => C): Iterable<C>
} = dual(3, <B, A, C>(self: Iterable<A>, that: Iterable<B>, f: (a: A, b: B) => C): Iterable<C> => ({
  [Symbol.iterator]() {
    const selfIterator = self[Symbol.iterator]()
    const thatIterator = that[Symbol.iterator]()
    return {
      next() {
        const selfResult = selfIterator.next()
        const thatResult = thatIterator.next()
        if (selfResult.done || thatResult.done) {
          return { done: true, value: undefined }
        }
        return { done: false, value: f(selfResult.value, thatResult.value) }
      }
    }
  }
}))
Referenced by 1 symbols