Clojure's core/reduce_kv in TypeScript

How to use

Install via yarn add locutus and import: import { reduce_kv } from 'locutus/clojure/core/reduce_kv'.

Or with CommonJS: const { reduce_kv } = require('locutus/clojure/core/reduce_kv')

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_kv((acc, key, value) => acc + key + value, '', {a: 1, b: 2})'a1b2'
2reduce_kv((acc, key, value) => acc + Number(key) * Number(value), 0, [10, 20, 30])80
3reduce_kv((acc, key, value) => ({...acc, [key]: Number(value) + 1}), {}, {x: 4}){x: 5}

Notes

  • Reduces map/array entries with key + value, similar to Clojure reduce-kv.

Here's what our current TypeScript equivalent to Clojure's core/reduce_kv looks like.

type ReduceKvReducer<TAccumulator, TValue> = (
accumulator: TAccumulator,
key: string | number,
value: TValue,
) => TAccumulator

type ReduceKvCollection<TValue> = Record<string, TValue> | TValue[]

export function reduce_kv<TAccumulator, TValue = unknown>(
reducer: ReduceKvReducer<TAccumulator, TValue>,
initial: TAccumulator,
collection: ReduceKvCollection<TValue> | unknown,
): TAccumulator {
// discuss at: https://locutus.io/clojure/reduce_kv/
// original by: Kevin van Zonneveld (https://kvz.io)
// note 1: Reduces map/array entries with key + value, similar to Clojure reduce-kv.
// example 1: reduce_kv((acc, key, value) => acc + key + value, '', {a: 1, b: 2})
// returns 1: 'a1b2'
// example 2: reduce_kv((acc, key, value) => acc + Number(key) * Number(value), 0, [10, 20, 30])
// returns 2: 80
// example 3: reduce_kv((acc, key, value) => ({...acc, [key]: Number(value) + 1}), {}, {x: 4})
// returns 3: {x: 5}

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

let acc = initial

if (Array.isArray(collection)) {
for (let i = 0; i < collection.length; i++) {
acc = reducer(acc, i, collection[i] as TValue)
}
return acc
}

if (!collection || typeof collection !== 'object') {
return acc
}

for (const [key, value] of Object.entries(collection as Record<string, TValue>)) {
acc = reducer(acc, key, value)
}

return acc
}

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 14 Clojure functions so far - help us add more

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

  • Get inspiration from ClojureDocs.
  • 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 Clojure core functions


Star