dtao / lazy.js

Like Underscore, but lazier

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

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Examples are slightly misleading.

Izhaki opened this issue · comments

Here is one of the examples for Sequence.map():

Lazy([1, 2, 3]).map(increment)  // sequence: [2, 3, 4]

And it gets a nice tick to show it passed the test (autodoc - amazing work, I must say).

One issue though. If you run the same line in the console, you get:

> Lazy([1, 2, 3]).map(increment) 
< MappedArrayWrapper
      mapFn: increment(x)
      parent: ArrayWrapper
      __proto__: Sequence

The problem with the comment is that you may infer that it returns an array (of the type Sequence). In practice, you need to use something like .each() or .toArray() to actually get an array.

I can definitely see what you mean, but I also really like the sweet autodoc coverage. What would you suggest? Maybe just a slightly different syntax to make the assertion on the right look a little less like an array?

Lazy([1, 2, 3]).map(increment)  // sequence(2, 3, 4)

Something like that, maybe?

Mostly adequate guide uses:

Maybe.of('Malkovich Malkovich').map(match(/a/ig));
//=> Maybe(['a', 'a'])

With this format you can't really infer it's an array. It reads to me more like "A value equivalent to calling Maybe(['a', 'a'])", which is not 100% correct since in this example it would really be Maybe.of(['a', 'a']), but at least it is clear you are not getting an array back.

So my suggestion would be to use:

Lazy([1, 2, 3]).map(increment)  // Sequence([2, 3, 4])

You could also use the standard-ish of Maybe<'a', 'a'>, so Sequence<2, 3, 4> — no Array syntax, so it can't be confused with a flattened / evaluated Lazy; and not even directly valid JavaScript, so it can't be inferred that it's equivalent to what you'd get typing Sequence(2, 3, 4) into a REPL?

Just my 2¢!

Circling back to this one, while I understand why you were initially confused, it doesn't seem to me there's really any problem here that needs to be solved. One could argue I should adopt some different syntax for expressing examples in the docs; but short of introducing a robust literal representation of sequences (i.e. a format for which bidirectional transformations are supported) I don't think there's a solution here that nobody would find confusing or surprising.

Respectfully closing.