Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I almost feel a bit gross when I have to write a "foreach" loop at this point

Agreed. I've transitioned to spending most of my time in JS, and wherever I can I use .map(), but the chaining it's not quite the same as LINQ. Someday I intend to write a library of Array addons to provide GroupBy and so on, but I can't imagine it'll be super efficient.



This is the problem with doing functional list stuff in languages that permit side effects: the order of execution for each function has to be strictly defined (in both senses of the word), so the compiler can't optimise the code. In Haskell the solution (see my comment on the original page) would probably (I haven't actually checked the GHC core output) be folded into a single loop.


In Underscore, you can do something like:

    _([ ... ]).map( ... ).filter( ... ).value()
It's not exactly extending the native array...but there are far fewer side effects to doing it this way.


The problem with this is that it's doing multiple iterations. LINQ on the other hand builds up an expression tree that is "compiled" into a single loop when `ToList()` (or some other method which gets the results) is eventually called.


Underscore has groupBy and some of the others you may be looking for.


Yes.. I do understand why extending the base Array object in JS is deeply frowned upon but at the same time I often wish that these sort of extensions were on the Array object. It feels clunky to use them otherwise.


You should check out Sugar, by far my favourite standard JS lib to use in any complex project: http://sugarjs.com/


This may be what you're looking for. I think it was developed my MS specifically for this reason:

https://github.com/Reactive-Extensions/RxJS




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: