Elixir's Enum.reduce_while in TypeScript

✓ Verified: Elixir 1.18
Examples tested against actual runtime. CI re-verifies continuously. Only documented examples are tested.

How to use

Install via yarn add locutus and import: import { reduce_while } from 'locutus/elixir/Enum/reduce_while'.

Or with CommonJS: const { reduce_while } = require('locutus/elixir/Enum/reduce_while')

Use a bundler that supports tree-shaking so you only ship the functions you actually use. Vite, webpack, Rollup, and Parcel all handle this. For server-side use this is less of a concern.

Examples

These examples are extracted from test cases that automatically verify our functions against their native counterparts.

#codeexpected result
1reduce_while([1, 2, 3, 4], 0, (acc, value) => (value < 4 ? ['cont', acc + value] : ['halt', acc]))6
2reduce_while([1, 3, 4, 6], null, (acc, value) => (value % 2 === 0 ? ['halt', value] : ['cont', acc]))4
3reduce_while([], 'done', (acc, _value) => ['cont', acc])'done'

Notes

  • Mirrors Elixir Enum.reduce_while/3 using [‘cont’, acc] and [‘halt’, acc] control tuples.

Here's what our current TypeScript equivalent to Elixir's Enum.reduce_while looks like.

type ReduceWhileTag = 'cont' | 'halt'
type ReduceWhileTuple<TAccumulator> = [ReduceWhileTag, TAccumulator]
type ReduceWhileObject<TAccumulator> = { tag: ReduceWhileTag; value: TAccumulator }
type ReduceWhileStep<TAccumulator> = ReduceWhileTuple<TAccumulator> | ReduceWhileObject<TAccumulator>
type ReduceWhileReducer<TAccumulator, TValue> = (
accumulator: TAccumulator,
value: TValue,
index: number,
array: TValue[],
) => ReduceWhileStep<TAccumulator>

function normalizeReduceWhileStep<TAccumulator>(value: ReduceWhileStep<TAccumulator>): ReduceWhileTuple<TAccumulator> {
if (Array.isArray(value)) {
const tag = value[0]
if ((tag === 'cont' || tag === 'halt') && value.length >= 2) {
return [tag, value[1] as TAccumulator]
}
} else if (value && typeof value === 'object') {
const tag = value.tag
if (tag === 'cont' || tag === 'halt') {
return [tag, value.value]
}
}

throw new TypeError("reduce_while(): reducer must return ['cont', value], ['halt', value], or { tag, value }")
}

export function reduce_while<TAccumulator, TValue>(
values: TValue[] | unknown,
initial: TAccumulator,
reducer: ReduceWhileReducer<TAccumulator, TValue>,
): TAccumulator {
// discuss at: https://locutus.io/elixir/reduce_while/
// parity verified: Elixir 1.18
// original by: Kevin van Zonneveld (https://kvz.io)
// note 1: Mirrors Elixir Enum.reduce_while/3 using ['cont', acc] and ['halt', acc] control tuples.
// example 1: reduce_while([1, 2, 3, 4], 0, (acc, value) => (value < 4 ? ['cont', acc + value] : ['halt', acc]))
// returns 1: 6
// example 2: reduce_while([1, 3, 4, 6], null, (acc, value) => (value % 2 === 0 ? ['halt', value] : ['cont', acc]))
// returns 2: 4
// example 3: reduce_while([], 'done', (acc, _value) => ['cont', acc])
// returns 3: 'done'

if (typeof reducer !== 'function') {
throw new TypeError('reduce_while(): reducer must be a function')
}

if (!Array.isArray(values) || values.length === 0) {
return initial
}

let accumulator = initial
for (let i = 0; i < values.length; i++) {
const [tag, nextAccumulator] = normalizeReduceWhileStep(reducer(accumulator, values[i] as TValue, i, values))
accumulator = nextAccumulator
if (tag === 'halt') {
break
}
}

return accumulator
}

Improve this function

Locutus is a community effort following The McDonald's Theory: we ship first iterations, hoping others will improve them. If you see something that could be better, we'd love your contribution.

View on GitHub · Edit on GitHub · View Raw


We have 12 Elixir functions so far - help us add more

Got a rainy Sunday afternoon and a taste for a porting puzzle?

  • Get inspiration from the Elixir documentation.
  • Click "New file" in the appropriate folder on GitHub. This will fork the project to your account, directly add the file to it, and send a Pull Request to us.

We will then review it. If it's useful to the project and in line with our contributing guidelines your work will become part of Locutus and you'll be automatically credited in the authors section accordingly.

« More Elixir Enum functions


Star