-
-
Notifications
You must be signed in to change notification settings - Fork 150
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(transducers): add dup() & palindrome(), update readme
- Loading branch information
1 parent
a7407ad
commit 546bf9f
Showing
5 changed files
with
70 additions
and
0 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
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,31 @@ | ||
import { ensureArray } from "@thi.ng/arrays"; | ||
import { isArray, isString } from "@thi.ng/checks"; | ||
import { concat } from "./concat"; | ||
|
||
/** | ||
* Returns the concatentation of `x` with itself. If input is an | ||
* iterable, it MUST be finite! | ||
* | ||
* @remarks | ||
* Also see the {@link duplicate} transducer for achieving a different | ||
* kind of value duplication. | ||
* | ||
* @example | ||
* ```ts | ||
* dup("hello"); // "hellohello" | ||
* dup([1, 2, 3]); // [1, 2, 3, 1, 2, 3] | ||
* dup(range(3)); // IterableIterator<number> | ||
* ``` | ||
* | ||
* @param x - | ||
*/ | ||
export function dup(x: string): string; | ||
export function dup<T>(x: T[]): T[]; | ||
export function dup<T>(x: Iterable<T>): Iterable<T>; | ||
export function dup(x: any): any { | ||
return isString(x) | ||
? x + x | ||
: isArray(x) | ||
? x.concat(x) | ||
: ((x = ensureArray(x)), concat(x, x)); | ||
} |
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,33 @@ | ||
import { ensureArray } from "@thi.ng/arrays"; | ||
import { isArray, isString } from "@thi.ng/checks"; | ||
import { str } from "../rfn/str"; | ||
import { concat } from "./concat"; | ||
import { reverse } from "./reverse"; | ||
|
||
/** | ||
* Returns the concatentation of `x` with its own duplicate in reverse | ||
* order. If input is an iterable, it MUST be finite! | ||
* | ||
* @remarks | ||
* In the general case, this is similar to `concat(x, reverse(x))`, | ||
* though keeps input type intact. | ||
* | ||
* @example | ||
* ```ts | ||
* palindrome("hello"); // "helloolleh" | ||
* palindrome([1, 2, 3]); // [1, 2, 3, 3, 2, 1] | ||
* palindrome(range(3)); // IterableIterator<number> | ||
* ``` | ||
* | ||
* @param x - | ||
*/ | ||
export function palindrome(x: string): string; | ||
export function palindrome<T>(x: T[]): T[]; | ||
export function palindrome<T>(x: Iterable<T>): Iterable<T>; | ||
export function palindrome(x: any): any { | ||
return isString(x) | ||
? str("", concat(x, reverse(x))) | ||
: isArray(x) | ||
? x.concat(x.slice().reverse()) | ||
: ((x = ensureArray(x)), concat(x, reverse(x))); | ||
} |