Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement iterable/repeat #40

Merged
merged 2 commits into from
Jul 8, 2015
Merged

Implement iterable/repeat #40

merged 2 commits into from
Jul 8, 2015

Conversation

kasperisager
Copy link
Contributor

This commit implements a repeat() function for indefinitely repeating a given iterator as specified in #13 and #16.

@kasperisager
Copy link
Contributor Author

Should be all set now 👍

Note to self: Rebasing kills comments on GitHub 😕

This commit implements a `repeat()` function for indefinitely repeating
a given iterator as specified in #13 and #16.
@nikhedonia
Copy link
Contributor

you are quick!
another alternative would be:

function* repeat(){
    if( Array.isArray(this) && this.length == 0 ){ return; }
    while(1){
        yield* this;
    }
}

But i'm not sure if there are any remaining cases which would let the loop spin without yielding...

@jussi-kalliokoski
Copy link
Owner

This doesn't look like it can handle non-restartable iterators, e.g.

function step (start, amount) {
  let current = start;
  while ( true ) {
    yield current;
    current += start;
  }
}

step(2, 2)::head(3)::repeat()::head(6)::to(Array).should.equal([2, 4, 6, 2, 4, 6]);

we should probably iterate over it while yielding and storing first, then keep yielding the stored items.

@kasperisager
Copy link
Contributor Author

Yielding and storing the items of the iterator first and then indefinitely yielding from the stored array worked a treat.

jussi-kalliokoski added a commit that referenced this pull request Jul 8, 2015
@jussi-kalliokoski jussi-kalliokoski merged commit 99250f1 into jussi-kalliokoski:master Jul 8, 2015
@jussi-kalliokoski
Copy link
Owner

Thank you, great stuff! Keep it coming! ;) ❤️ 🎉

@jussi-kalliokoski jussi-kalliokoski added this to the 0.1.0 milestone Jul 8, 2015
@kasperisager kasperisager deleted the feature/iterable/repeat branch July 8, 2015 15:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants