-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add flatten & remove Iterum delegation (#5)
- Loading branch information
Showing
24 changed files
with
85 additions
and
229 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
const Iterable = require('../core/iterable') | ||
const validation = [['Number', 'Undefined']] | ||
|
||
function* flatten (n = 1) { | ||
let iterator = this[Symbol.iterator]() | ||
const stack = [] | ||
|
||
while (true) { | ||
let done | ||
let value | ||
let pop | ||
let push | ||
do { | ||
const state = iterator.next() | ||
;({value, done} = state) | ||
pop = done && stack.length > 0 | ||
push = !done && value instanceof Iterable && stack.length < n | ||
if (pop) { | ||
iterator = stack.pop() | ||
} else if (push) { | ||
stack.push(iterator) | ||
iterator = value[Symbol.iterator]() | ||
} | ||
} while (pop || push) | ||
if (done) { | ||
return | ||
} | ||
yield value | ||
} | ||
} | ||
|
||
module.exports = { | ||
gen: flatten, | ||
validation | ||
} |
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
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
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,43 @@ | ||
const {expect} = require('chai') | ||
const Iterum = require('../../src/index.js') | ||
|
||
describe('flatten', function () { | ||
it('flatten 0 returns iterable that iterate over the same values', function () { | ||
const iterable = [1, [2, 3, 4], 5] | ||
const nonFlattenedIterable = Iterum(iterable) | ||
.flatten(0) | ||
expect([...nonFlattenedIterable]).to.be.deep.equal([...iterable]) | ||
}) | ||
|
||
it('flatten (n = 1)', function () { | ||
const iterable = [[[1], 2], 3, [4, 5], [[6], 7]] | ||
const expectedIterable = [[1], 2, 3, 4, 5, [6], 7] | ||
const flattenedIterable = Iterum(iterable) | ||
.flatten(1) | ||
expect([...flattenedIterable]).to.be.deep.equal([...expectedIterable]) | ||
}) | ||
|
||
it('flatten has n = 1 by default', function () { | ||
const iterable = [[[1], 2], 3, [4, 5], [[6], 7]] | ||
const expectedIterable = [[1], 2, 3, 4, 5, [6], 7] | ||
const flattenedIterable = Iterum(iterable) | ||
.flatten() | ||
expect([...flattenedIterable]).to.be.deep.equal([...expectedIterable]) | ||
}) | ||
|
||
it('flatten (n = 4)', function () { | ||
const iterable = [1, [2, [3, [4, [5, [6, [7]]]]]]] | ||
const expectedIterable = [1, 2, 3, 4, 5, [6, [7]]] | ||
const flattenedIterable = Iterum(iterable) | ||
.flatten(4) | ||
expect([...flattenedIterable]).to.be.deep.equal([...expectedIterable]) | ||
}) | ||
|
||
it('flatten (n = Infinity)', function () { | ||
const iterable = [[[[[[[7], 6], 5], 4], 3], 2], 1, [2, [3, [4, [5, [6, [7]]]]]]] | ||
const expectedIterable = [7, 6, 5, 4, 3, 2, 1, 2, 3, 4, 5, 6, 7] | ||
const flattenedIterable = Iterum(iterable) | ||
.flatten(Infinity) | ||
expect([...flattenedIterable]).to.be.deep.equal([...expectedIterable]) | ||
}) | ||
}) |
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
Oops, something went wrong.