Skip to content

Commit

Permalink
refactor(transducers): replace wrapBoth/Left/Right w/ wrap()
Browse files Browse the repository at this point in the history
- deprecate existing wrap*() iters
- update docs & readme
  • Loading branch information
postspectacular committed May 14, 2018
1 parent fbba5d2 commit e238541
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 41 deletions.
6 changes: 1 addition & 5 deletions packages/transducers/README.md
Expand Up @@ -813,11 +813,7 @@ itself. Returns nothing.

#### `vals<T>(x: IObjectOf<T>): IterableIterator<T>`

#### `wrapBoth<T>(src: T[], n?: number): IterableIterator<T>`

#### `wrapLeft<T>(src: T[], n?: number): IterableIterator<T>`

#### `wrapRight<T>(src: T[], n?: number): IterableIterator<T>`
#### `wrap<T>(src: T[], n = 1, left = true, right = true): IterableIterator<T>`

## Authors

Expand Down
4 changes: 1 addition & 3 deletions packages/transducers/src/index.ts
Expand Up @@ -124,6 +124,4 @@ export * from "./iter/repeatedly";
export * from "./iter/reverse";
export * from "./iter/tuples";
export * from "./iter/vals";
export * from "./iter/wrap-both";
export * from "./iter/wrap-left";
export * from "./iter/wrap-right";
export * from "./iter/wrap";
19 changes: 5 additions & 14 deletions packages/transducers/src/iter/wrap-both.ts
@@ -1,21 +1,12 @@
import { illegalArgs } from "@thi.ng/errors/illegal-arguments";
import { wrap } from "./wrap";

/**
* Combination of `wrapLeft()` and `wrapRight()`. Yields iterator of
* `src` with the last `n` values of `src` prepended at the beginning
* and the first `n` values appended at the end. Throws error if `n` < 0
* or larger than `src.length`.
* See `wrap()`.
*
* @deprecated superceded by `wrap()`
* @param src
* @param n
*/
export function* wrapBoth<T>(src: T[], n = 1) {
(n < 0 || n > src.length) && illegalArgs(`wrong number of wrap items: got ${n} max: ${src.length}`);
for (let m = src.length, i = m - n; i < m; i++) {
yield src[i];
}
yield* src;
for (let i = 0; i < n; i++) {
yield src[i];
}
export function wrapBoth<T>(src: T[], n = 1) {
return wrap(src, n);
}
15 changes: 5 additions & 10 deletions packages/transducers/src/iter/wrap-left.ts
@@ -1,17 +1,12 @@
import { illegalArgs } from "@thi.ng/errors/illegal-arguments";
import { wrap } from "./wrap";

/**
* Yields iterator of `src` with the last `n` values of `src` prepended
* at the beginning. Throws error if `n` < 0 or larger than
* `src.length`.
* See `wrap()`.
*
* @deprecated superceded by `wrap()`
* @param src
* @param n
*/
export function* wrapLeft<T>(src: T[], n = 1) {
(n < 0 || n > src.length) && illegalArgs(`wrong number of wrap items: got ${n} max: ${src.length}`);
for (let m = src.length, i = m - n; i < m; i++) {
yield src[i];
}
yield* src;
export function wrapLeft<T>(src: T[], n = 1) {
return wrap(src, n, true, false);
}
14 changes: 5 additions & 9 deletions packages/transducers/src/iter/wrap-right.ts
@@ -1,16 +1,12 @@
import { illegalArgs } from "@thi.ng/errors/illegal-arguments";
import { wrap } from "./wrap";

/**
* Yields iterator of `src` with the first `n` values of `src` appended
* at the end. Throws error if `n` < 0 or larger than `src.length`.
* See `wrap()`.
*
* @deprecated superceded by `wrap()`
* @param src
* @param n
*/
export function* wrapRight<T>(src: T[], n = 1) {
(n < 0 || n > src.length) && illegalArgs(`wrong number of wrap items: got ${n} max: ${src.length}`);
yield* src;
for (let i = 0; i < n; i++) {
yield src[i];
}
export function wrapRight<T>(src: T[], n = 1) {
return wrap(src, n, false, true);
}
27 changes: 27 additions & 0 deletions packages/transducers/src/iter/wrap.ts
@@ -0,0 +1,27 @@
import { illegalArgs } from "@thi.ng/errors/illegal-arguments";

/**
* Yields iterator of `src` with the last `n` values of `src` prepended
* at the beginning (if `left` is truthy) and/or the first `n` values
* appended at the end (if `right` is truthy). Wraps both sides by
* default and throws error if `n` < 0 or larger than `src.length`.
*
* @param src
* @param n
* @param left
* @param right
*/
export function* wrap<T>(src: T[], n = 1, left = true, right = true) {
(n < 0 || n > src.length) && illegalArgs(`wrong number of wrap items: got ${n} max: ${src.length}`);
if (left) {
for (let m = src.length, i = m - n; i < m; i++) {
yield src[i];
}
}
yield* src;
if (right) {
for (let i = 0; i < n; i++) {
yield src[i];
}
}
}

0 comments on commit e238541

Please sign in to comment.