Skip to content

Commit

Permalink
✨ feat: Add method reversed().
Browse files Browse the repository at this point in the history
Fixes #107.
  • Loading branch information
make-github-pseudonymous-again committed Mar 25, 2021
1 parent a1f9ae6 commit 17acfff
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 4 deletions.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ Parent is [@aureooms/js-persistent](https://github.com/aureooms/js-persistent).
* [`Tree#takeUntil(Function) -> Tree`](#treetakeuntilfunction---tree)
* [`Tree#dropUntil(Function) -> Tree`](#treedropuntilfunction---tree)
* [:flying_saucer: Visit](#flying_saucer-visit)
* [`TreeSymbol.iterator -> Iterable`](#treesymboliterator---iterable)
* [`Tree[Symbol.iterator]() -> Iterable`](#treesymboliterator---iterable)
* [`Tree#reversed() -> Iterable`](#treereversed---iterable)
* [:scroll: References](#scroll-references)
* [:link: Links](#link-links)

Expand Down Expand Up @@ -306,6 +307,14 @@ Returns an iterator on the values of the tree in left-to-right order.
for ( const x of tree ) console.log( x ) ;
```

#### `Tree#reversed() -> Iterable`

Returns an iterator on the values of the tree in right-to-left order.

```js
for ( const x of tree.reversed() ) console.log( x ) ;
```


## :scroll: References

Expand Down
4 changes: 4 additions & 0 deletions src/1-digit/0-Digit.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,7 @@ Digit.prototype._nodes_with_list = function (M, list, other) {
Digit.prototype[Symbol.iterator] = function () {
return this._list()[Symbol.iterator]();
};

Digit.prototype.reversed = function () {
return this._list().reverse()[Symbol.iterator]();
};
4 changes: 4 additions & 0 deletions src/2-node/2-Node2.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ Node2.prototype[Symbol.iterator] = function () {
return [this.a, this.b][Symbol.iterator]();
};

Node2.prototype.reversed = function () {
return [this.b, this.a][Symbol.iterator]();
};

Node2.prototype.measure = function () {
return this.v;
};
Expand Down
4 changes: 4 additions & 0 deletions src/2-node/3-Node3.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ Node3.prototype[Symbol.iterator] = function () {
return [this.a, this.b, this.c][Symbol.iterator]();
};

Node3.prototype.reversed = function () {
return [this.c, this.b, this.a][Symbol.iterator]();
};

Node3.prototype.measure = function () {
return this.v;
};
Expand Down
2 changes: 2 additions & 0 deletions src/3-tree/implementations/0-Empty.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ Empty.prototype[Symbol.iterator] = function () {
return _EMPTY;
};

Empty.prototype.reversed = function* () {};

/**
* It is assumed that p(i+|this|) is true.
*/
Expand Down
8 changes: 5 additions & 3 deletions src/3-tree/implementations/1-Single.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,11 @@ Single.prototype.concat = function (other) {
return other.cons(this.a);
};

Single.prototype[Symbol.iterator] = function* () {
yield this.a;
};
Single.prototype[Symbol.iterator] =
// eslint-disable-next-line no-multi-assign
Single.prototype.reversed = function* () {
yield this.a;
};

/**
* It is assumed that p(i+|this|) is true.
Expand Down
6 changes: 6 additions & 0 deletions src/3-tree/implementations/2-Deep.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,12 @@ Deep.prototype[Symbol.iterator] = function* () {
yield* this.right;
};

Deep.prototype.reversed = function* () {
yield* this.right.reversed();
for (const node of this.middle.reversed()) yield* node.reversed();
yield* this.left.reversed();
};

/**
* It is assumed that p(i+|this|) is true.
*/
Expand Down
4 changes: 4 additions & 0 deletions src/4-lazy/0-Lazy.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,7 @@ Lazy.prototype.concat = function (other) {
Lazy.prototype[Symbol.iterator] = function () {
return this.force()[Symbol.iterator]();
};

Lazy.prototype.reversed = function () {
return this.force().reversed();
};
28 changes: 28 additions & 0 deletions test/src/methods/reversed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import test from 'ava';

import {Measures} from '@aureooms/js-measure';
const {COUNTER} = Measures;

import {list, range, tee, reversed} from '@aureooms/js-itertools';

import {from} from '../../../src/index.js';

const macro = (t, iterable) => {
const [copy1, copy2] = tee(iterable, 2);
const expected = list(reversed(copy1));
const actual = list(from(COUNTER, copy2).reversed());
t.deepEqual(actual, expected);
};

macro.title = (title, iterable) =>
`from(COUNTER, ${title || JSON.stringify(iterable)})`;

test(macro, []);
test('range(0)', macro, range(0));
test(macro, [0]);
test(macro, 'ab');
test(macro, '724');
test(macro, 'abcd');
test('range(9)', macro, range(9));
test('range(1000)', macro, range(1000));
test('range(100000)', macro, range(100000));

0 comments on commit 17acfff

Please sign in to comment.