dtao / lazy.js

Like Underscore, but lazier

Home Page:http://danieltao.com/lazy.js/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

.length() does not work after some operations.

Redsandro opened this issue · comments

Lazy([{a:1}, {a:2}, {a:3}]).length()
// 3
Lazy([{a:1}, {a:2}, {a:3}]).where({a:2}).length()
// Expected: 1
// VM396:1 Uncaught TypeError: Lazy(...).where(...).length is not a function

That's right, you can see from the docs that length is only available on ArrayLikeSequences. This is because it can't be provided efficiently for arbitrary sequence types.

I might be open to adding it for the Sequence base type. But based on the wording and description of this issue it seems like this might have just been a matter of wrong expectations; so for now I'll close this.

@dtao ok, interesting. I'm doubting wether or not this is a matter of 'wrong expectations' or 'expected intuitivity'. I'm using this library for a while now, and I still find myself making similar mistakes assumptions for certain operators.

As the developer, you know exactly what's going on under the hood. But here's the mistake assumption I make evey now and then.

Expectation

  • ArrayLikeSequence -> filter -> ArrayLikeSequence
  • Result .value() is array.
  • Result has same set if methods as input

Reality

  • ArrayLikeSequence -> filter -> Sequence
  • Result .value() is array.
  • Result has different set of methods than input

It doesn't feel quite intuitive, but I am sure there is a functional necessity in order to keep things lazy.


What I am trying to do (that made me open this issue) is to filter an ArrayLikeSequence and see if there are more than 100 objects left. The problem with .value().length is that I would have to materialize the entire lazy object just to count the number of items. Is there a way to do this on Sequence without having to use .value() and step out of the realm of lazy?