effect-io-ai

Package: effect
Module: Iterable

Iterable.zipWith

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]

Signature

declare 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>; }

Source

Since v2.0.0