-
-
Notifications
You must be signed in to change notification settings - Fork 151
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(transducers): replace wrapBoth/Left/Right w/ wrap()
- deprecate existing wrap*() iters - update docs & readme
- Loading branch information
1 parent
fbba5d2
commit e238541
Showing
6 changed files
with
44 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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]; | ||
} | ||
} | ||
} |