Skip to content

Commit

Permalink
feat(transducers): add dup() & palindrome(), update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Jan 13, 2020
1 parent a7407ad commit 546bf9f
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/transducers/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -956,12 +956,14 @@ tx.transduce(tx.map((x) => x*10), tx.push(), tx.range(4))
- [choices](https://github.com/thi-ng/umbrella/tree/master/packages/transducers/src/iter/choices.ts)
- [concat](https://github.com/thi-ng/umbrella/tree/master/packages/transducers/src/iter/concat.ts)
- [cycle](https://github.com/thi-ng/umbrella/tree/master/packages/transducers/src/iter/cycle.ts)
- [dup](https://github.com/thi-ng/umbrella/tree/master/packages/transducers/src/iter/dup.ts)
- [extendSides](https://github.com/thi-ng/umbrella/tree/master/packages/transducers/src/iter/extend-sides.ts)
- [iterate](https://github.com/thi-ng/umbrella/tree/master/packages/transducers/src/iter/iterate.ts)
- [keys](https://github.com/thi-ng/umbrella/tree/master/packages/transducers/src/iter/keys.ts)
- [normRange](https://github.com/thi-ng/umbrella/tree/master/packages/transducers/src/iter/norm-range.ts)
- [padSides](https://github.com/thi-ng/umbrella/tree/master/packages/transducers/src/iter/pad-sides.ts)
- [pairs](https://github.com/thi-ng/umbrella/tree/master/packages/transducers/src/iter/pairs.ts)
- [palindrome](https://github.com/thi-ng/umbrella/tree/master/packages/transducers/src/iter/palindrome.ts)
- [permutations](https://github.com/thi-ng/umbrella/tree/master/packages/transducers/src/iter/permutations.ts)
- [permutationsN](https://github.com/thi-ng/umbrella/tree/master/packages/transducers/src/iter/permutationsN.ts)
- [range](https://github.com/thi-ng/umbrella/tree/master/packages/transducers/src/iter/range.ts)
Expand Down
2 changes: 2 additions & 0 deletions packages/transducers/README.tpl.md
Original file line number Diff line number Diff line change
Expand Up @@ -722,12 +722,14 @@ tx.transduce(tx.map((x) => x*10), tx.push(), tx.range(4))
- [choices](https://github.com/thi-ng/umbrella/tree/master/packages/transducers/src/iter/choices.ts)
- [concat](https://github.com/thi-ng/umbrella/tree/master/packages/transducers/src/iter/concat.ts)
- [cycle](https://github.com/thi-ng/umbrella/tree/master/packages/transducers/src/iter/cycle.ts)
- [dup](https://github.com/thi-ng/umbrella/tree/master/packages/transducers/src/iter/dup.ts)
- [extendSides](https://github.com/thi-ng/umbrella/tree/master/packages/transducers/src/iter/extend-sides.ts)
- [iterate](https://github.com/thi-ng/umbrella/tree/master/packages/transducers/src/iter/iterate.ts)
- [keys](https://github.com/thi-ng/umbrella/tree/master/packages/transducers/src/iter/keys.ts)
- [normRange](https://github.com/thi-ng/umbrella/tree/master/packages/transducers/src/iter/norm-range.ts)
- [padSides](https://github.com/thi-ng/umbrella/tree/master/packages/transducers/src/iter/pad-sides.ts)
- [pairs](https://github.com/thi-ng/umbrella/tree/master/packages/transducers/src/iter/pairs.ts)
- [palindrome](https://github.com/thi-ng/umbrella/tree/master/packages/transducers/src/iter/palindrome.ts)
- [permutations](https://github.com/thi-ng/umbrella/tree/master/packages/transducers/src/iter/permutations.ts)
- [permutationsN](https://github.com/thi-ng/umbrella/tree/master/packages/transducers/src/iter/permutationsN.ts)
- [range](https://github.com/thi-ng/umbrella/tree/master/packages/transducers/src/iter/range.ts)
Expand Down
2 changes: 2 additions & 0 deletions packages/transducers/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,13 @@ export * from "./iter/as-iterable";
export * from "./iter/choices";
export * from "./iter/concat";
export * from "./iter/cycle";
export * from "./iter/dup";
export * from "./iter/extend-sides";
export * from "./iter/iterate";
export * from "./iter/keys";
export * from "./iter/norm-range";
export * from "./iter/pad-sides";
export * from "./iter/palindrome";
export * from "./iter/pairs";
export * from "./iter/permutations";
export * from "./iter/range";
Expand Down
31 changes: 31 additions & 0 deletions packages/transducers/src/iter/dup.ts
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));
}
33 changes: 33 additions & 0 deletions packages/transducers/src/iter/palindrome.ts
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)));
}

0 comments on commit 546bf9f

Please sign in to comment.