Skip to content

Commit

Permalink
fix(transducers): add type hints
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Nov 22, 2019
1 parent d609182 commit 651e281
Show file tree
Hide file tree
Showing 13 changed files with 45 additions and 30 deletions.
2 changes: 1 addition & 1 deletion packages/transducers/src/iter/choices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const choices = <T>(
choices: ArrayLike<T> & Iterable<T>,
weights?: ArrayLike<number>,
rnd: IRandom = SYSTEM
) =>
): IterableIterator<T> =>
repeatedly(
weights
? weightedRandom(ensureArray(choices), weights, rnd)
Expand Down
5 changes: 4 additions & 1 deletion packages/transducers/src/iter/concat.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Nullable } from "@thi.ng/api";
import { ensureIterable } from "@thi.ng/arrays";

/**
Expand All @@ -15,7 +16,9 @@ import { ensureIterable } from "@thi.ng/arrays";
*
* @param xs
*/
export function* concat<T>(...xs: Iterable<T>[]): IterableIterator<T> {
export function* concat<T>(
...xs: Nullable<Iterable<T>>[]
): IterableIterator<T> {
for (let x of xs) {
x != null && (yield* ensureIterable(x));
}
Expand Down
2 changes: 1 addition & 1 deletion packages/transducers/src/iter/cycle.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export function* cycle<T>(input: Iterable<T>) {
export function* cycle<T>(input: Iterable<T>): IterableIterator<T> {
let cache: T[] = [];
for (let i of input) {
cache.push(i);
Expand Down
2 changes: 1 addition & 1 deletion packages/transducers/src/iter/extend-sides.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export function* extendSides<T>(
src: Iterable<T>,
numLeft = 1,
numRight = numLeft
) {
): IterableIterator<T> {
let prev: T | typeof SEMAPHORE = SEMAPHORE;
for (let x of src) {
if (numLeft > 0 && prev === SEMAPHORE) {
Expand Down
5 changes: 4 additions & 1 deletion packages/transducers/src/iter/iterate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ import { Fn2 } from "@thi.ng/api";
* @param fn
* @param seed
*/
export function* iterate<T>(fn: Fn2<T, number, T>, seed: T) {
export function* iterate<T>(
fn: Fn2<T, number, T>,
seed: T
): IterableIterator<T> {
let i = 0;
while (true) {
yield seed;
Expand Down
5 changes: 4 additions & 1 deletion packages/transducers/src/iter/norm-range.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
* @param n number of steps
* @param inclLast include last value (i.e. `1.0`)
*/
export function* normRange(n: number, inclLast = true) {
export function* normRange(
n: number,
inclLast = true
): IterableIterator<number> {
if (n > 0) {
for (let i = 0, m = inclLast ? n + 1 : n; i < m; i++) {
yield i / n;
Expand Down
2 changes: 1 addition & 1 deletion packages/transducers/src/iter/pad-sides.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export const padSides = <T>(
x: T,
numLeft = 1,
numRight = numLeft
) =>
): IterableIterator<T> =>
numLeft > 0
? numRight > 0
? concat(repeat(x, numLeft), src, repeat(x, numRight))
Expand Down
2 changes: 1 addition & 1 deletion packages/transducers/src/iter/repeat.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export function* repeat<T>(x: T, n = Infinity) {
export function* repeat<T>(x: T, n = Infinity): IterableIterator<T> {
while (n-- > 0) {
yield x;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/transducers/src/iter/repeatedly.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Fn0 } from "@thi.ng/api";

export function* repeatedly<T>(fn: Fn0<T>, n = Infinity) {
export function* repeatedly<T>(fn: Fn0<T>, n = Infinity): IterableIterator<T> {
while (n-- > 0) {
yield fn();
}
Expand Down
2 changes: 1 addition & 1 deletion packages/transducers/src/iter/symmetric.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ interface Cell<T> {
*
* @param src
*/
export function* symmetric<T>(src: Iterable<T>) {
export function* symmetric<T>(src: Iterable<T>): IterableIterator<T> {
let head: Cell<T> | undefined = undefined;
for (let x of src) {
head = { x, n: head };
Expand Down
2 changes: 1 addition & 1 deletion packages/transducers/src/iter/wrap-sides.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export function* wrapSides<T>(
src: Iterable<T>,
numLeft = 1,
numRight = numLeft
) {
): IterableIterator<T> {
const _src: T[] = ensureArray(src);
!(inRange(numLeft, 0, _src.length) && inRange(numRight, 0, _src.length)) &&
illegalArgs(`allowed wrap range: [0..${_src.length}]`);
Expand Down
41 changes: 23 additions & 18 deletions packages/transducers/src/iter/zip.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,26 @@
/**
* Accepts a number of iterables and combines them into an iterable of
* tuples of successively consumed input values.
*
* @remarks
* Tuples are formed by merging each value of each input iterable, such
* that the first yielded tuple contains the first elements of the given
* inputs, the second tuple contains the second elements of the inputs,
* etc.
*
* The number of resulting tuples will be the same as the length of the
* shortest input iterable. Given only a single argument, `zip` yields a
* sequence of 1-tuples.
*
* @example
* ```ts
* zip([1, 2, 3], [3, 4, 5, 0, 9])
* // [ 1, 3 ] [ 2, 4 ] [ 3, 5 ]
*
* zip([1, 2, 3])
* // [ 1 ] [ 2 ] [ 3 ]
* ```
*/
export function zip<A>(a: Iterable<A>): IterableIterator<[A]>;
export function zip<A, B>(
a: Iterable<A>,
Expand Down Expand Up @@ -62,21 +85,3 @@ export function* zip(...src: Iterable<any>[]): IterableIterator<any[]> {
yield tuple;
}
}

/**
* Zip function accepts a list of iterables, and combines them by merging each value of each iterable,
* Such as the first yield element contains the first elements of the given iterables, the second of which contains the second elements of the given iterables, and so on.
*
* The returned iterable is truncated in length to the length of the shortest argument sequence. With a single sequence argument, it yields a list of 1-tuples.
*
* ```
* tx.zip([1, 2, 3], [3, 4, 5, 0, 9])
* // [ 1, 3 ] [ 2, 4 ] [ 3, 5 ]
*
* tx.zip([1, 2, 3])
* // [ 1 ] [ 2 ] [ 3 ]
* ```
*
* @deprecated renamed to `zip`
*/
export const tuples = zip;
3 changes: 2 additions & 1 deletion packages/transducers/src/xform/cat.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Nullable } from "@thi.ng/api";
import { Reducer, Transducer } from "../api";
import { compR } from "../func/compr";
import { ensureReduced, isReduced, unreduced } from "../reduced";
Expand Down Expand Up @@ -32,7 +33,7 @@ import { ensureReduced, isReduced, unreduced } from "../reduced";
* @see thi.ng/transducers/iter/concat
* @see thi.ng/transducers/xform/mapcat
*/
export const cat = <T>(): Transducer<Iterable<T> | null | undefined, T> => (
export const cat = <T>(): Transducer<Nullable<Iterable<T>>, T> => (
rfn: Reducer<any, T>
) => {
const r = rfn[2];
Expand Down

0 comments on commit 651e281

Please sign in to comment.