@ts-common/iterator
Advanced tools
Comparing version 0.3.4 to 0.3.5
@@ -134,5 +134,9 @@ /** | ||
/** | ||
* Creates a new sequence of accamulated values. | ||
* Creates a new sequence of accumulated values. It's exclusive scan so it always returns at least one value. | ||
*/ | ||
readonly scan: <A>(func: (a: A, b: T, i: number) => A, init: A) => IterableEx<A>; | ||
/** | ||
* Firstly, the function maps a state and each element using the `func` function, then flattens the result. | ||
*/ | ||
readonly flatScan: <A, R>(func: (a: A, b: T, i: number) => readonly [A, Iterable<R>], init: A) => IterableEx<R>; | ||
}; | ||
@@ -160,2 +164,3 @@ export declare const iterable: <T>(createIterator: () => Iterator<T>) => IterableEx<T>; | ||
export declare const scan: <T, A>(input: Iterable<T> | undefined, func: (a: A, b: T, i: number) => A, init: A) => IterableEx<A>; | ||
export declare const flatScan: <T, A, R>(input: Iterable<T> | undefined, func: (a: A, b: T, i: number) => readonly [A, Iterable<R>], init: A) => IterableEx<R>; | ||
export declare const fold: <T, A>(input: Iterable<T> | undefined, func: (a: A, b: T, i: number) => A, init: A) => A; | ||
@@ -162,0 +167,0 @@ export declare const reduce: <T>(input: Iterable<T> | undefined, func: (a: T, b: T, i: number) => T) => T | undefined; |
@@ -32,2 +32,3 @@ "use strict"; | ||
scan: property(exports.scan), | ||
flatScan: property(exports.flatScan), | ||
}; | ||
@@ -44,6 +45,6 @@ }; | ||
let index = 0; | ||
/* tslint:disable-next-line:no-loop-statement */ | ||
// tslint:disable-next-line:no-loop-statement | ||
for (const value of input) { | ||
yield [index, value]; | ||
/* tslint:disable-next-line:no-expression-statement */ | ||
// tslint:disable-next-line:no-expression-statement | ||
index += 1; | ||
@@ -53,3 +54,3 @@ } | ||
exports.map = (input, func) => exports.iterable(function* () { | ||
/* tslint:disable-next-line:no-loop-statement */ | ||
// tslint:disable-next-line:no-loop-statement | ||
for (const [index, value] of exports.entries(input)) { | ||
@@ -65,3 +66,3 @@ yield func(value, index); | ||
} | ||
/* tslint:disable-next-line:no-loop-statement */ | ||
// tslint:disable-next-line:no-loop-statement | ||
for (const v of input) { | ||
@@ -76,5 +77,5 @@ // tslint:disable-next-line:no-if-statement | ||
exports.takeWhile = (input, func) => exports.iterable(function* () { | ||
/* tslint:disable-next-line:no-loop-statement */ | ||
// tslint:disable-next-line:no-loop-statement | ||
for (const [index, value] of exports.entries(input)) { | ||
/* tslint:disable-next-line:no-if-statement */ | ||
// tslint:disable-next-line:no-if-statement | ||
if (!func(value, index)) { | ||
@@ -106,3 +107,3 @@ return; | ||
const infinite = () => exports.iterable(function* () { | ||
/* tslint:disable-next-line:no-loop-statement */ | ||
// tslint:disable-next-line:no-loop-statement | ||
while (true) { | ||
@@ -119,5 +120,5 @@ yield; | ||
yield result; | ||
/* tslint:disable-next-line:no-loop-statement */ | ||
// tslint:disable-next-line:no-loop-statement | ||
for (const [index, value] of exports.entries(input)) { | ||
/* tslint:disable-next-line:no-expression-statement */ | ||
// tslint:disable-next-line:no-expression-statement | ||
result = func(result, value, index); | ||
@@ -127,7 +128,18 @@ yield result; | ||
}); | ||
exports.flatScan = (input, func, init) => exports.iterable(function* () { | ||
let state = init; | ||
// tslint:disable-next-line:no-loop-statement | ||
for (const [index, value] of exports.entries(input)) { | ||
// tslint:disable-next-line:no-expression-statement | ||
const [newState, result] = func(state, value, index); | ||
// tslint:disable-next-line:no-expression-statement | ||
state = newState; | ||
yield* result; | ||
} | ||
}); | ||
exports.fold = (input, func, init) => { | ||
let result = init; | ||
/* tslint:disable-next-line:no-loop-statement */ | ||
// tslint:disable-next-line:no-loop-statement | ||
for (const [index, value] of exports.entries(input)) { | ||
/* tslint:disable-next-line:no-expression-statement */ | ||
// tslint:disable-next-line:no-expression-statement | ||
result = func(result, value, index); | ||
@@ -150,13 +162,13 @@ } | ||
const iterators = inputs.map(i => i === undefined ? [][Symbol.iterator]() : i[Symbol.iterator]()); | ||
/* tslint:disable-next-line:no-loop-statement */ | ||
// tslint:disable-next-line:no-loop-statement | ||
while (true) { | ||
const result = new Array(inputs.length); | ||
/* tslint:disable-next-line:no-loop-statement */ | ||
// tslint:disable-next-line:no-loop-statement | ||
for (const [index, it] of exports.entries(iterators)) { | ||
const v = it.next(); | ||
/* tslint:disable-next-line:no-if-statement */ | ||
// tslint:disable-next-line:no-if-statement | ||
if (v.done) { | ||
return; | ||
} | ||
/* tslint:disable-next-line:no-object-mutation no-expression-statement */ | ||
// tslint:disable-next-line:no-object-mutation no-expression-statement | ||
result[index] = v.value; | ||
@@ -213,15 +225,12 @@ } | ||
exports.dropRight = (i, n = 1) => i === undefined ? exports.empty() : exports.take(i, i.length - n); | ||
exports.uniq = (i, key = v => v) => exports.iterable(function* () { | ||
const set = new Set(); | ||
// tslint:disable-next-line:no-loop-statement | ||
for (const v of i) { | ||
const k = key(v); | ||
// tslint:disable-next-line:no-if-statement | ||
if (!set.has(k)) { | ||
// tslint:disable-next-line:no-expression-statement | ||
set.add(k); | ||
yield v; | ||
} | ||
exports.uniq = (i, key = v => v) => exports.flatScan(i, (set, v) => { | ||
const k = key(v); | ||
// tslint:disable-next-line:no-if-statement | ||
if (!set.has(k)) { | ||
// tslint:disable-next-line:no-expression-statement | ||
set.add(k); | ||
return [set, [v]]; | ||
} | ||
}); | ||
return [set, []]; | ||
}, new Set()); | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@ts-common/iterator", | ||
"version": "0.3.4", | ||
"version": "0.3.5", | ||
"description": "Iterator library for JavaScript and TypeScript", | ||
@@ -69,7 +69,7 @@ "main": "dist/index.js", | ||
"jest-junit": "^6.4.0", | ||
"tslint": "^5.16.0", | ||
"tslint": "^5.17.0", | ||
"tslint-immutable": "^5.5.2", | ||
"typescript": "^3.4.5" | ||
"typescript": "^3.5.1" | ||
}, | ||
"dependencies": {} | ||
} |
@@ -137,5 +137,9 @@ /** | ||
/** | ||
* Creates a new sequence of accamulated values. | ||
* Creates a new sequence of accumulated values. It's exclusive scan so it always returns at least one value. | ||
*/ | ||
readonly scan: <A>(func: (a: A, b: T, i: number) => A, init: A) => IterableEx<A>, | ||
/** | ||
* Firstly, the function maps a state and each element using the `func` function, then flattens the result. | ||
*/ | ||
readonly flatScan: <A, R>(func: (a: A, b: T, i: number) => readonly [A, Iterable<R>], init: A) => IterableEx<R>, | ||
} | ||
@@ -173,2 +177,3 @@ | ||
scan: property(scan), | ||
flatScan: property(flatScan), | ||
} | ||
@@ -191,6 +196,6 @@ } | ||
let index = 0 | ||
/* tslint:disable-next-line:no-loop-statement */ | ||
// tslint:disable-next-line:no-loop-statement | ||
for (const value of input) { | ||
yield [index, value] as const | ||
/* tslint:disable-next-line:no-expression-statement */ | ||
// tslint:disable-next-line:no-expression-statement | ||
index += 1 | ||
@@ -205,3 +210,3 @@ } | ||
iterable(function *(): Iterator<T> { | ||
/* tslint:disable-next-line:no-loop-statement */ | ||
// tslint:disable-next-line:no-loop-statement | ||
for (const [index, value] of entries(input)) { | ||
@@ -221,3 +226,3 @@ yield func(value, index) | ||
} | ||
/* tslint:disable-next-line:no-loop-statement */ | ||
// tslint:disable-next-line:no-loop-statement | ||
for (const v of input) { | ||
@@ -239,5 +244,5 @@ // tslint:disable-next-line:no-if-statement | ||
iterable(function *(): Iterator<T> { | ||
/* tslint:disable-next-line:no-loop-statement */ | ||
// tslint:disable-next-line:no-loop-statement | ||
for (const [index, value] of entries(input)) { | ||
/* tslint:disable-next-line:no-if-statement */ | ||
// tslint:disable-next-line:no-if-statement | ||
if (!func(value, index)) { | ||
@@ -298,3 +303,3 @@ return | ||
iterable(function *(): Iterator<void> { | ||
/* tslint:disable-next-line:no-loop-statement */ | ||
// tslint:disable-next-line:no-loop-statement | ||
while (true) { yield } | ||
@@ -319,5 +324,5 @@ }) | ||
yield result | ||
/* tslint:disable-next-line:no-loop-statement */ | ||
// tslint:disable-next-line:no-loop-statement | ||
for (const [index, value] of entries(input)) { | ||
/* tslint:disable-next-line:no-expression-statement */ | ||
// tslint:disable-next-line:no-expression-statement | ||
result = func(result, value, index) | ||
@@ -328,2 +333,19 @@ yield result | ||
export const flatScan = <T, A, R>( | ||
input: Iterable<T> | undefined, | ||
func: (a: A, b: T, i: number) => readonly [A, Iterable<R>], | ||
init: A | ||
): IterableEx<R> => | ||
iterable(function *() { | ||
let state = init | ||
// tslint:disable-next-line:no-loop-statement | ||
for (const [index, value] of entries(input)) { | ||
// tslint:disable-next-line:no-expression-statement | ||
const [newState, result] = func(state, value, index) | ||
// tslint:disable-next-line:no-expression-statement | ||
state = newState | ||
yield *result | ||
} | ||
}) | ||
export const fold = <T, A>( | ||
@@ -335,5 +357,5 @@ input: Iterable<T> | undefined, | ||
let result: A = init | ||
/* tslint:disable-next-line:no-loop-statement */ | ||
// tslint:disable-next-line:no-loop-statement | ||
for (const [index, value] of entries(input)) { | ||
/* tslint:disable-next-line:no-expression-statement */ | ||
// tslint:disable-next-line:no-expression-statement | ||
result = func(result, value, index) | ||
@@ -388,13 +410,13 @@ } | ||
) | ||
/* tslint:disable-next-line:no-loop-statement */ | ||
// tslint:disable-next-line:no-loop-statement | ||
while (true) { | ||
const result = new Array<T>(inputs.length) | ||
/* tslint:disable-next-line:no-loop-statement */ | ||
// tslint:disable-next-line:no-loop-statement | ||
for (const [index, it] of entries(iterators)) { | ||
const v = it.next() | ||
/* tslint:disable-next-line:no-if-statement */ | ||
// tslint:disable-next-line:no-if-statement | ||
if (v.done) { | ||
return | ||
} | ||
/* tslint:disable-next-line:no-object-mutation no-expression-statement */ | ||
// tslint:disable-next-line:no-object-mutation no-expression-statement | ||
result[index] = v.value | ||
@@ -473,6 +495,5 @@ } | ||
export const uniq = <T>(i: Iterable<T>, key: (v: T) => unknown = v => v): IterableEx<T> => | ||
iterable(function *() { | ||
const set = new Set<unknown>() | ||
// tslint:disable-next-line:no-loop-statement | ||
for (const v of i) { | ||
flatScan( | ||
i, | ||
(set, v: T): readonly [Set<unknown>, readonly T[]] => { | ||
const k = key(v) | ||
@@ -483,5 +504,7 @@ // tslint:disable-next-line:no-if-statement | ||
set.add(k) | ||
yield v | ||
return [set, [v]] | ||
} | ||
} | ||
}) | ||
return [set, []] | ||
}, | ||
new Set<unknown>() | ||
) |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
65507
860