Skip to content

Commit

Permalink
feat(dcons): add IReducible impl, update deps & imports
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Aug 22, 2018
1 parent f14f7ce commit 1280cfd
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 14 deletions.
5 changes: 3 additions & 2 deletions packages/dcons/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -48,4 +49,4 @@
"publishConfig": {
"access": "public"
}
}
}
46 changes: 34 additions & 12 deletions packages/dcons/src/index.ts
Original file line number Diff line number Diff line change
@@ -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<T> {
value: T;
Expand All @@ -12,13 +24,14 @@ export interface ConsCell<T> {
}

export class DCons<T> implements
api.ICompare<DCons<T>>,
api.ICopy<DCons<T>>,
api.IEmpty<DCons<T>>,
api.IEquiv,
api.ILength,
api.IRelease,
api.IStack<T, DCons<T>> {
ICompare<DCons<T>>,
ICopy<DCons<T>>,
IEmpty<DCons<T>>,
IEquiv,
ILength,
IReducible<any, T>,
IRelease,
IStack<T, DCons<T>> {

head: ConsCell<T>;
tail: ConsCell<T>;
Expand Down Expand Up @@ -107,6 +120,15 @@ export class DCons<T> implements
}
}

$reduce(rfn: ReductionFn<any, T>, 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) {
Expand Down Expand Up @@ -185,7 +207,7 @@ export class DCons<T> implements
}
}

insertSorted(value: T, cmp?: api.Comparator<T>) {
insertSorted(value: T, cmp?: Comparator<T>) {
cmp = cmp || compare;
let cell = this.head;
while (cell) {
Expand All @@ -207,7 +229,7 @@ export class DCons<T> implements
}
}

findWith(fn: api.Predicate<T>) {
findWith(fn: Predicate<T>) {
let cell = this.head;
while (cell) {
if (fn(cell.value)) {
Expand Down Expand Up @@ -423,7 +445,7 @@ export class DCons<T> implements
return res;
}

filter(pred: api.Predicate<T>) {
filter(pred: Predicate<T>) {
const res = new DCons<T>();
let cell = this.head;
while (cell) {
Expand Down

0 comments on commit 1280cfd

Please sign in to comment.