You don't need each, collect or select in Coffeescript

Updated . Posted . Visible to the public. Deprecated.

JavaScript's Array class now has forEach(), map() and filter().

We also don't write so much CoffeeScript anymore.

Working with lists in Javascript is painful because the native Array class is so poorly designed.

One way to reduce the pain is to to use Underscore.js Show archive.org snapshot 's functions like _.each, _.map or _.select, which unfortunately clutters your code with awkward calls to the _ helper.

Fortunately when you use CoffeeScript you don't need any of that. CoffeeScript has a very versatile for keyword Show archive.org snapshot that can do anything that each, collect or select can do. Enjoy!

each

for item in items 
  ...

collect / map

ages = (person.age for person in people)

select / grep

adults = (person for person in people when person.age >= 18)
Henning Koch
Last edit
Tobias Kraze
Keywords
list, comprehensions
License
Source code in this card is licensed under the MIT License.
Posted by Henning Koch to makandra dev (2013-09-28 16:46)