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

Add count method #6

Merged
merged 5 commits into from
May 30, 2017
Merged

Add count method #6

merged 5 commits into from
May 30, 2017

Conversation

sea1jxr
Copy link
Contributor

@sea1jxr sea1jxr commented May 21, 2017

I was really wishing I had count today, while using genSequence so I added it.

@coveralls
Copy link

coveralls commented May 21, 2017

Coverage Status

Coverage remained the same at 100.0% when pulling a935826 on sea1jxr:count into 7c2a4e3 on Jason3S:master.

@Jason3S
Copy link
Collaborator

Jason3S commented May 28, 2017

Sorry about not getting back to you sooner on this one.

Nicely done with the code.

I have a concern that has nothing to do with your code. The .count() will consume the iterator. If the iterator is an array, you can re-use it. But it is not re-usable if the iterator came from a generator.

Copy link
Collaborator

@Jason3S Jason3S left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think your code is fine, I am just worried about confusion generated because someone doesn't understand why the sequence was consumed.

it('test count with 2 elements', () => {
const values: number[] = [18, 7];
expect(genSequence(values).count()).to.equal(2);
});
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nicely done. There is two more tests necessary.
Need to test the behavior of what happens if the sequence is used again after .count().

Example:

const values = [1, 2, 3, 4, 5, 6]
const seq = genSequence(values);
const cnt = seq.count();
const cntAgain = seq.count();

and

const values = [1, 2, 3, 4, 5, 6]
const seq = genSequence(values).filter(a => !!(a % 2));
const cnt = seq.count();
const cntAgain = seq.count();

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.NET has the same issue with IEnumerables and LINQ, but they allow it, and many times the sequence can handle a reuse.

Sequence loses the "next" function because it had to reset when it saw done which felt a bit strange.
@coveralls
Copy link

coveralls commented May 29, 2017

Coverage Status

Coverage remained the same at 100.0% when pulling 73ffd5c on sea1jxr:count into 7c2a4e3 on Jason3S:master.

@sea1jxr
Copy link
Contributor Author

sea1jxr commented May 29, 2017

I made sequences reusable. I ended up removing the next() method because the iterator needed to be reset after it saw done. I thought maybe that was a bad practice, but the spec doesn't seem to prohibit it, so if there are good scenarios for next() we could bring it back for reusable sequences.

This was kind of an experiment to see if I could make them reusable. It turned out well, so I thought I would run it by you.

Let me know what you think.

@Jason3S
Copy link
Collaborator

Jason3S commented May 30, 2017

Wow. Very cool.
You have made it late binding. Similar to RxJs.
I need a bit more time to think about the implications.

I think your suggestion is worth exploring. I will have to bump the major version number, since it is a change to the underlying behavior.

Typing and thinking at the same time (not a good combo for me):

  • I think .next() is necessary to meet the iterator spec and removing it might break some stuff (not sure).

@coveralls
Copy link

coveralls commented May 30, 2017

Coverage Status

Coverage remained the same at 100.0% when pulling 30b5bcc on sea1jxr:count into 7c2a4e3 on Jason3S:master.

@coveralls
Copy link

Coverage Status

Coverage remained the same at 100.0% when pulling 5eaa9dc on sea1jxr:count into 7c2a4e3 on Jason3S:master.

1 similar comment
@coveralls
Copy link

Coverage Status

Coverage remained the same at 100.0% when pulling 5eaa9dc on sea1jxr:count into 7c2a4e3 on Jason3S:master.

@sea1jxr
Copy link
Contributor Author

sea1jxr commented May 30, 2017

I added back .next(). Take your time and think through the scenarios.

@coveralls
Copy link

Coverage Status

Coverage remained the same at 100.0% when pulling 1c1b7b3 on sea1jxr:count into 7c2a4e3 on Jason3S:master.

1 similar comment
@coveralls
Copy link

Coverage Status

Coverage remained the same at 100.0% when pulling 1c1b7b3 on sea1jxr:count into 7c2a4e3 on Jason3S:master.

@Jason3S Jason3S merged commit f6ea88d into streetsidesoftware:master May 30, 2017
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