@thi.ng/transducers
Advanced tools
Comparing version 0.2.0 to 0.3.0
@@ -1,2 +0,2 @@ | ||
import { IDeref, Predicate } from "@thi.ng/api/api"; | ||
import { IDeref, Predicate } from "@thi.ng/api"; | ||
export declare type Transducer<A, B> = (rfn: Reducer<any, B>) => Reducer<any, A>; | ||
@@ -21,3 +21,3 @@ export interface Reducer<A, B> extends Array<any> { | ||
export declare function reduce<A, B>([i, _, r]: Reducer<A, B>, acc: A, xs: Iterable<B>): any; | ||
export declare function transduce<A, B, C>(tx: Transducer<A, B>, rfn: Reducer<C, B>, acc: C, xs: Iterable<A>): C; | ||
export declare function transduce<A, B, C>(tx: Transducer<A, B>, rfn: Reducer<C, B>, acc: C, xs: Iterable<A>): any; | ||
export declare function iterator<A, B>(tx: Transducer<A, B>, xs: Iterable<A>): IterableIterator<B>; | ||
@@ -43,5 +43,9 @@ export declare function map<A, B>(fn: (x: A) => B): Transducer<A, B>; | ||
export declare function partitionBy<A>(fn: (x: A) => any): Transducer<A, A>; | ||
export declare const conj: Reducer<any[], any>; | ||
export declare const assoc: Reducer<any, [PropertyKey, any]>; | ||
export declare function chunkSort<A>(n: number, key?: ((x) => any)): Transducer<A, A>; | ||
export declare function streamSort<T>(n: number, key: (x) => any): Transducer<T, T>; | ||
export declare const push: Reducer<any[], any>; | ||
export declare const conj: Reducer<Set<any>, any>; | ||
export declare const assocObj: Reducer<any, [PropertyKey, any]>; | ||
export declare const assocMap: Reducer<Map<any, any>, [any, any]>; | ||
export declare const add: Reducer<number, number>; | ||
export declare const mul: Reducer<number, number>; |
83
index.js
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const compare_1 = require("@thi.ng/api/compare"); | ||
; | ||
@@ -87,16 +88,18 @@ class Reduced { | ||
const _rfn = tx(rfn); | ||
return _rfn[1](reduce(_rfn, acc, xs)); | ||
return unreduced(_rfn[1](reduce(_rfn, acc, xs))); | ||
} | ||
exports.transduce = transduce; | ||
function* iterator(tx, xs) { | ||
const [_, c, r] = tx(exports.conj); | ||
const [_, c, r] = tx(exports.push); | ||
for (let x of xs) { | ||
const y = r([], x); | ||
if (isReduced(y)) { | ||
yield* c(y.deref()); | ||
yield* unreduced(c(y.deref())); | ||
return; | ||
} | ||
yield* y; | ||
if (y.length) { | ||
yield* y; | ||
} | ||
} | ||
yield* c([]); | ||
yield* unreduced(c([])); | ||
} | ||
@@ -128,3 +131,3 @@ exports.iterator = iterator; | ||
if (isReduced(acc)) { | ||
return acc; | ||
break; | ||
} | ||
@@ -222,5 +225,5 @@ } | ||
let m = n; | ||
return compR(rfn, (acc, x) => m > 1 ? (m--, r(acc, x)) : | ||
m === 1 ? ensureReduced(r(acc, x)) : | ||
acc); | ||
return compR(rfn, (acc, x) => --m > 0 ? r(acc, x) : | ||
m === 0 ? ensureReduced(r(acc, x)) : | ||
reduced(acc)); | ||
}; | ||
@@ -314,3 +317,3 @@ } | ||
} | ||
return acc; | ||
return rfn[1](acc); | ||
}, | ||
@@ -348,3 +351,3 @@ (acc, x) => { | ||
} | ||
return acc; | ||
return rfn[1](acc); | ||
}, | ||
@@ -367,3 +370,47 @@ (acc, x) => { | ||
exports.partitionBy = partitionBy; | ||
exports.conj = [ | ||
function chunkSort(n, key = identity) { | ||
return comp(partition(n, n, true), mapcat((chunk) => chunk.sort((a, b) => compare_1.compare(key(a), key(b))))); | ||
} | ||
exports.chunkSort = chunkSort; | ||
function binarySearch(arr, key, x) { | ||
const kx = key(x); | ||
let low = 0, high = arr.length - 1; | ||
while (low <= high) { | ||
const mid = (low + high) >>> 1, c = compare_1.compare(key(arr[mid]), kx); | ||
if (c < 0) { | ||
low = mid + 1; | ||
} | ||
else if (c > 0) { | ||
high = mid - 1; | ||
} | ||
else { | ||
return mid; | ||
} | ||
} | ||
return low; | ||
} | ||
function streamSort(n, key) { | ||
return ([i, c, r]) => { | ||
const buf = []; | ||
return [ | ||
() => i(), | ||
(acc) => { | ||
while (buf.length && !isReduced(acc)) { | ||
acc = r(acc, buf.shift()); | ||
} | ||
acc = c(acc); | ||
return acc; | ||
}, | ||
(acc, x) => { | ||
buf.splice(binarySearch(buf, key, x), 0, x); | ||
if (buf.length === n) { | ||
acc = r(acc, buf.shift()); | ||
} | ||
return acc; | ||
} | ||
]; | ||
}; | ||
} | ||
exports.streamSort = streamSort; | ||
exports.push = [ | ||
() => [], | ||
@@ -373,3 +420,8 @@ (acc) => acc, | ||
]; | ||
exports.assoc = [ | ||
exports.conj = [ | ||
() => new Set(), | ||
(acc) => acc, | ||
(acc, x) => acc.add(x), | ||
]; | ||
exports.assocObj = [ | ||
() => new Object(), | ||
@@ -379,2 +431,7 @@ (acc) => acc, | ||
]; | ||
exports.assocMap = [ | ||
() => new Map(), | ||
(acc) => acc, | ||
(acc, [k, v]) => acc.set(k, v), | ||
]; | ||
exports.add = [ | ||
@@ -381,0 +438,0 @@ () => 0, |
{ | ||
"name": "@thi.ng/transducers", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"description": "Simplified transducer implementations", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
14955
487
0