From: Antony Scriven on 30 Mar 2010 10:57 On Mar 30, 2:55 pm, "Dmitry A. Soshnikov" wrote: > [...] > > var array = [1, 2, 3]; > > array.map(function (item) { > return item % 2 != 0; > > }); [1, 3] Typo: should be `array.filter(...'. --Antony
From: Antony Scriven on 30 Mar 2010 11:06 On Mar 30, 3:04 pm, "Dmitry A. Soshnikov" wrote: > [... on forEach() and friends vs. for-loop ...] > > Addition: of course if you don't need some polymorphic > unlimited test conditions/applies, you can simply use > for-iteration in place - this is faster and absolutely > normal as it is also a construction of the language. So > if some will tell you that using a functional style - is > cool just because it is modern (or sort of), don't > listen. But the other talk is (as I said above) an > increasing of abstraction and providing elegant solutions > with a good code reuse - in this case the functional > approach is a very good decision. Agreed. I'd like to add that in my experience that I get code right first time more often when using the functional style; I guess that's because of the reasons you mention. I think the functional style expresses intent, rather than the mechanics of the iteration; I certainly don't make any off-by-one errors when using forEach(). Also you get a new scope within the function passed to forEach() which can be helpful. --Antony
First
|
Prev
|
Pages: 1 2 Prev: It's a fact not lost on the opportunity to see yourself Next: Location Intelligence demo |