From 1280cfd51daf9ed174d32f024f12d46febdd27e7 Mon Sep 17 00:00:00 2001 From: Karsten Schmidt Date: Wed, 22 Aug 2018 23:27:42 +0100 Subject: [PATCH] feat(dcons): add IReducible impl, update deps & imports --- packages/dcons/package.json | 5 ++-- packages/dcons/src/index.ts | 46 +++++++++++++++++++++++++++---------- 2 files changed, 37 insertions(+), 14 deletions(-) diff --git a/packages/dcons/package.json b/packages/dcons/package.json index 5042024642..3b226ed38d 100644 --- a/packages/dcons/package.json +++ b/packages/dcons/package.json @@ -32,7 +32,8 @@ "@thi.ng/checks": "^1.5.7", "@thi.ng/compare": "^0.1.6", "@thi.ng/equiv": "^0.1.7", - "@thi.ng/errors": "^0.1.6" + "@thi.ng/errors": "^0.1.6", + "@thi.ng/transducers": "^1.16.0" }, "keywords": [ "datastructure", @@ -48,4 +49,4 @@ "publishConfig": { "access": "public" } -} +} \ No newline at end of file diff --git a/packages/dcons/src/index.ts b/packages/dcons/src/index.ts index dbe048b5e4..97686dbd57 100644 --- a/packages/dcons/src/index.ts +++ b/packages/dcons/src/index.ts @@ -1,9 +1,21 @@ -import * as api from "@thi.ng/api/api"; +import { + Comparator, + ICompare, + ICopy, + IEmpty, + IEquiv, + ILength, + IRelease, + IStack, + Predicate +} from "@thi.ng/api/api"; +import { isArrayLike } from "@thi.ng/checks/is-arraylike"; import { compare } from "@thi.ng/compare"; import { equiv } from "@thi.ng/equiv"; import { illegalArgs } from "@thi.ng/errors/illegal-arguments"; import { illegalState } from "@thi.ng/errors/illegal-state"; -import { isArrayLike } from "@thi.ng/checks/is-arraylike"; +import { IReducible, ReductionFn } from "@thi.ng/transducers/api"; +import { isReduced } from "@thi.ng/transducers/reduced"; export interface ConsCell { value: T; @@ -12,13 +24,14 @@ export interface ConsCell { } export class DCons implements - api.ICompare>, - api.ICopy>, - api.IEmpty>, - api.IEquiv, - api.ILength, - api.IRelease, - api.IStack> { + ICompare>, + ICopy>, + IEmpty>, + IEquiv, + ILength, + IReducible, + IRelease, + IStack> { head: ConsCell; tail: ConsCell; @@ -107,6 +120,15 @@ export class DCons implements } } + $reduce(rfn: ReductionFn, acc: any) { + let cell = this.head; + while (cell && !isReduced(acc)) { + acc = rfn(acc, cell.value); + cell = cell.next; + } + return acc; + } + drop() { const cell = this.head; if (cell) { @@ -185,7 +207,7 @@ export class DCons implements } } - insertSorted(value: T, cmp?: api.Comparator) { + insertSorted(value: T, cmp?: Comparator) { cmp = cmp || compare; let cell = this.head; while (cell) { @@ -207,7 +229,7 @@ export class DCons implements } } - findWith(fn: api.Predicate) { + findWith(fn: Predicate) { let cell = this.head; while (cell) { if (fn(cell.value)) { @@ -423,7 +445,7 @@ export class DCons implements return res; } - filter(pred: api.Predicate) { + filter(pred: Predicate) { const res = new DCons(); let cell = this.head; while (cell) {