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

Lazy(object).length() ? #56

Closed
ken-okabe opened this issue Dec 20, 2013 · 5 comments
Closed

Lazy(object).length() ? #56

ken-okabe opened this issue Dec 20, 2013 · 5 comments

Comments

@ken-okabe
Copy link

To obtain object keys length with lazy.js, I do

var len = Lazy(object).keys().toArray().length;

However, I want to write simply

var len = Lazy(object).length();

Does this make sense? or any philosophy we should not have this?

Thx

@akre54
Copy link

akre54 commented Dec 20, 2013

For parity with underscore this would be size (which is basically implemented the way you described under the hood)

@ken-okabe
Copy link
Author

Thannks akre54,

http://underscorejs.org/#size

Allright, so we can have size method both for Lazy Sequence and ObjectLikeSequence

I've notcied there is

 length GeneratedSequence#length

Returns the length of this sequence.
Signature

GeneratedSequence.length = function()

Name    Type(s)     Description
returns     number  

The length, or undefined if this is an indefinite sequence. 

and not sure what this is and how to use, also.

dtao added a commit that referenced this issue Jan 2, 2014
@dtao
Copy link
Owner

dtao commented Jan 2, 2014

OK, I just added size so you can get the number of elements in any sequence.

Currently what I'm thinking is that size is offered for all sequence types (it's on the Sequence prototype) but may require full evaluation whereas length is only available on ArrayLikeSequences but does not require full (it is known up front, before iterating). Maybe this is confusing. I'm not sure I'm 100% settled on it yet. But I at least see the appeal of having a method that's available on all sequences, even if it potentially incurs a full evaluation (I've at least indicated as much in the docs for the method).

As for your question about GeneratedSequence#length: you get a generate sequence using Lazy.generate:

var sequence = Lazy.generate(Math.random, 10);

In the above example, sequence.length() will return 10, since a length of 10 was explicitly specified. On the other hand, you could also do this:

var sequence = Lazy.generate(Math.random);

In this case, sequence.length() will return undefined (although now that I think on it, maybe it should return Infinity) and can go on indefinitely. You would presumably pull from this sequence using, e.g., take:

sequence.take(10).toArray();

Alternately, a generated sequence could go on forever and let you just keep iterating asynchronously. This is how the prime number generator example works.

@dtao
Copy link
Owner

dtao commented Jan 3, 2014

OK, this is now available in 0.3.1: http://danieltao.com/lazy.js/docs/#Sequence-size

@dtao dtao closed this as completed Jan 3, 2014
@ken-okabe
Copy link
Author

Thank you dtao.

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

No branches or pull requests

3 participants