Integration into the default types
See original GitHub issueHi
I like the functional, lazy style of thinking very much, so I was quite excited when I first heard of Lazy.js!
There is one drawback though which is not really a fault of Lazy.js but rather that JS is not designed as lazy. Frequently there is a rather simple task, like I want to uppercase a string:
(Sorry for the Coffescript; I can convert it to JS if you wish)
upp = (s) ->
Lazy(s).toUpperCase().toString()
My code becomes more type conversion than anything else.
This would be all fine if I could just ignore the normal functions and always expect lazy data types as arguments/return value. I can not however because most libraries (and bits of existing code in my projects) expect that the native non-lazy types are used.
Here is another example; let’s say I want to process an array, cast to a string and process that string and return it:
specialString = (v) ->
s__ = Lazy v
.map (x) -> x+1
.toString()
Lazy s__
.substring 1, 8
.toString()
My solution in these simple cases is normally to fall back to the native functions or underscore; I don’t think this is a very good solution and I think it would be much nicer if I just could write something like this and have support for both – code that is not written explicitly for lazy.js and code that is.
upp = (s) -> s.toUpperCase()
specialString = (v) ->
v.map (x) -> x+1
.toString()
.substring 1,8
I realize that there are much better ways to implement the snippets above, but I hope they they serve to illustrate my observations.
I thought about this problem but I could not really find a solution, so I thought I’d put this problem out here and ask if we maybe can come up with a way to better integrate those two worlds.
Despite of this I’ve now used lazy.js for a while productively and it is really cool! Thanks for the hard work guys!
Issue Analytics
- State:
- Created 9 years ago
- Comments:5 (2 by maintainers)
Top GitHub Comments
Hi!
So I am again evaluating lazy.js. The good news is I can now basically say “my function takes and returns es6 iterators” and that is working pretty well. Lazy JS is an es6 iterator, so returning that works fine. The problem is: I still need the cast to lazy JS.
I wrote my own lib based on es6 iterators – just to get a feel for the problem:
I usually import that with something like:
var {iter, map, each} = itertools;
I also used my own version of the Lazy() function – I called ititer()
(it’s a no-op except for objects).But I call that implicitly
map()
andeach()
, so when I use those functions I can avoid the extra call toiter()
. Would it be possible to do something similar in lazy.js?I mean, provide functions like
Lazy.map(iter, function)
?There is: https://github.com/dtao/lazy.js/issues/66 😃