Clojure's core/partition_by in TypeScript

✓ Verified: Clojure 1.12
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 { partition_by } from 'locutus/clojure/core/partition_by'.

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

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
1partition_by((value) => value % 2 === 0, [1, 1, 2, 2, 3, 4, 5, 5])[[1, 1], [2, 2], [3], [4], [5, 5]]
2partition_by((value) => value.length, ['a', 'ab', 'cd', 'efg'])[['a'], ['ab', 'cd'], ['efg']]
3partition_by((value) => value, [])[]

Notes

  • Groups consecutive values while selector(value) remains equal, similar to Clojure partition-by.

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

type PartitionBySelector<T, TKey> = (value: T, index: number, array: T[]) => TKey

function stablePartitionKey(value: unknown): string {
if (value === null) {
return 'null'
}
if (typeof value === 'undefined') {
return 'undefined'
}
if (typeof value === 'number') {
if (Number.isNaN(value)) {
return 'number:NaN'
}
if (Object.is(value, -0)) {
return 'number:-0'
}
return `number:${value}`
}
if (typeof value === 'string') {
return `string:${value}`
}
if (typeof value === 'boolean') {
return `boolean:${value}`
}
if (typeof value === 'bigint') {
return `bigint:${value}`
}
if (Array.isArray(value)) {
return `array:[${value.map((item) => stablePartitionKey(item)).join(',')}]`
}
if (typeof value === 'object') {
const record = value as { [key: string]: unknown }
const entries = Object.keys(record)
.sort()
.map((key) => `${JSON.stringify(key)}:${stablePartitionKey(record[key])}`)
return `object:{${entries.join(',')}}`
}

return `${typeof value}:${String(value)}`
}

export function partition_by<T, TKey>(selector: PartitionBySelector<T, TKey>, values: T[] | unknown): T[][] {
// discuss at: https://locutus.io/clojure/partition_by/
// parity verified: Clojure 1.12
// original by: Kevin van Zonneveld (https://kvz.io)
// note 1: Groups consecutive values while selector(value) remains equal, similar to Clojure partition-by.
// example 1: partition_by((value) => value % 2 === 0, [1, 1, 2, 2, 3, 4, 5, 5])
// returns 1: [[1, 1], [2, 2], [3], [4], [5, 5]]
// example 2: partition_by((value) => value.length, ['a', 'ab', 'cd', 'efg'])
// returns 2: [['a'], ['ab', 'cd'], ['efg']]
// example 3: partition_by((value) => value, [])
// returns 3: []

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

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

const out: T[][] = []
let currentBucket: T[] = []
let previousKey: string | null = null

for (let i = 0; i < values.length; i++) {
const value = values[i] as T
const nextKey = stablePartitionKey(selector(value, i, values))

if (previousKey === null || nextKey === previousKey) {
currentBucket.push(value)
previousKey = nextKey
continue
}

out.push(currentBucket)
currentBucket = [value]
previousKey = nextKey
}

if (currentBucket.length > 0) {
out.push(currentBucket)
}

return out
}

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 15 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