0.1.3 • Published 5 months ago

@fightingdreamer/iter-reduce v0.1.3

Weekly downloads
-
License
MIT
Repository
-
Last release
5 months ago

iter-reduce

Description

Missing reduce function for iterator based input, similar to Array.reduce.

Second implementation can better handle cases of input with length 0 or 1.

Install

bun add @fightingdreamer/iter-reduce

Usage

import {reduceWithInitial} from '@fightingdreamer/iter-reduce'

const vector = [1,2,3][Symbol.iterator]()
const result = reduceWithInitial(vector, (r, v) => r + v, 3)
const expect = 9
console.assert(result == expect)
import {reduceWithDefault} from '@fightingdreamer/iter-reduce'

const vector = [1][Symbol.iterator]()
const result = reduceWithDefault(vector, (r, v) => Math.min(r, v), 0)
const expect = 1
console.assert(result == expect)

Usage (node / commonjs)

const {reduceWithInitial} = require('@fightingdreamer/iter-reduce')

const vector = [1,2,3][Symbol.iterator]()
const result = reduceWithInitial(vector, (r, v) => r + v, 3)
const expect = 9
assert(result == expect)
const {reduceWithDefault} = require('@fightingdreamer/iter-reduce')

const vector = [1][Symbol.iterator]()
const result = reduceWithDefault(vector, (r, v) => Math.min(r, v), 0)
const expect = 1
assert(result == expect)

Functions

function reduceWithInitial<T, R>(
  iterator: Iterable<T>,
  callbackFn: (accumulator: R, currentValue: T, currentIndex: number) => R,
  initialValue: R,
): R

For inputs of any length will call callbackFn on every item, using initialValue as accumulator on first call.

This function is best suited for merging input into one item, or creating collection from input reusing already collected items.

function reduceWithDefault<T>(
  iterator: Iterable<T>,
  callbackFn: (accumulator: T, currentValue: T, currentIndex: number) => T,
  defaultValue: T,
): T

For inputs of length >= 2 will call callbackFn on every item except first one, using first item as accumulator on first call.

For inputs of length == 1 will not call callbackFn and return first item.

For inputs of length == 0 will not call callbackFn and return defaultValue item.

This function is best suited for cases when initial value can easily distort result, eg. finding Math.min from Iterable<number>.

0.1.2

5 months ago

0.1.3

5 months ago

0.1.1

5 months ago

0.1.0

5 months ago