@oazmi/kitchensink - v0.9.13
    Preparing search index...

    Function zipIteratorsMapperFactory

    • create a mapping function that operates on a list of iterable/iterator inputs, that are zipped together as tuples, and then passed on to the map_fn for transformation, one by one.

      Note

      if one of the input arrays or iterators is shorter in length than all the rest, then the mapping function will only operate up till the shortest array/iterator. similar to how python's zip function generates tuples up till the end of the shortest input array.

      Type Parameters

      • T extends any[]
      • V

      Parameters

      • map_fn: (tuple: T, index: number) => V

        a function that maps each tuple T (from the collection of input iterators) to some type V.

      Returns (
          ...iterators: (Iterable<any, any, any> | Iterator<any, any, any>)[],
      ) => IterableIterator<V, number>

      a generator function that will accept a list of iterators as its input, and that yields back the result of each zipped tuple being mapped via map_fn. the return value of the generator (after it concludes) is the length of the number of items that it had yielded.

      import { assertEquals } from "jsr:@std/assert"

      type MyObj = { key: string }
      type MyTuple = [number, boolean, MyObj]

      const myTupleMapper = zipIteratorsMapperFactory((tuple: MyTuple, index: number): string => {
      const [my_number, my_boolean, my_object] = tuple
      return `${index}-${my_object.key}/${my_number}/${my_boolean}`
      })

      myTupleMapper satisfies ((number_arr: number[], boolean_arr: boolean[], object_arr: MyObj[]) => IterableIterator<string>)

      const
      my_num_iter = rangeIterator(100), // infnite iterable, with values `[100, 101, 102, ...]`
      my_bool_arr = [true, false, false, true, false],
      my_obj_arr = [{ key: "a" }, { key: "b" }, { key: "c" }, { key: "d" }]
      // notice that `my_obj_arr` is shorter than the other two arrays. (i.e has a length of `4`).
      // this would mean that `myTupleMapper` would only operate on the first `4` elements of all the 3 arrays.

      const outputs_iter: Iterable<string> = myTupleMapper(my_num_iter, my_bool_arr, my_obj_arr)
      assertEquals([...outputs_iter], [
      "0-a/100/true",
      "1-b/101/false",
      "2-c/102/false",
      "3-d/103/true",
      ])

      // zipping with zero sized input iterators should not yield anything.
      assertEquals([...myTupleMapper([], [], [])], [])

      // for safety, map-zipping with no input iterators should not yield anything.
      assertEquals([...myTupleMapper()], [])