Skip to content

Commit d7d741f

Browse files
author
David Brady
committed
Added arrays/list-comprehensions and arrays/mapping-arrays
1 parent 580d3a9 commit d7d741f

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
---
2+
layout: recipe
3+
title: List Comprehensions
4+
chapter: Arrays
5+
---
6+
7+
h2. Problem
8+
9+
You have an array of objects and want to map them to another array, similar to Python's list comprehensions.
10+
11+
h2. Solution
12+
13+
Use a list comprehension, but don't forget about <a href="/chapters/arrays/mapping-arrays">mapping arrays</a>.
14+
15+
{% highlight coffeescript %}
16+
electric_mayhem = [ { name: "Doctor Teeth", instrument: "piano" },
17+
{ name: "Janice", instrument: "lead guitar" },
18+
{ name: "Sgt. Floyd Pepper", instrument: "bass" },
19+
{ name: "Zoot", instrument: "sax" },
20+
{ name: "Lips", instrument: "trumpet" },
21+
{ name: "Animal", instrument: "drums" } ]
22+
23+
names = (muppet.name for muppet in electric_mayhem)
24+
# => [ 'Doctor Teeth', 'Janice', 'Sgt. Floyd Pepper', 'Zoot', 'Lips', 'Animal' ]
25+
{% endhighlight %}
26+
27+
h2. Discussion
28+
29+
Because CoffeeScript directly support list comprehensions, they work pretty much as advertised wherever you would use one in Python. For simple mappings, list comprehensions are much more readable. For complicated transformations or for chained mappings, <a href="/chapters/arrays/mapping-arrays">mapping arrays</a> might be more elegant.
30+
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
---
2+
layout: recipe
3+
title: Mapping Arrays
4+
chapter: Arrays
5+
---
6+
7+
h2. Problem
8+
9+
You have an array of objects and want to map them to another array, similar to Ruby's map.
10+
11+
h2. Solution
12+
13+
Use map() with an anonymous function, but don't forget about <a href="/chapters/arrays/list-comprehensions">list comprehensions</a>.
14+
15+
{% highlight coffeescript %}
16+
electric_mayhem = [ { name: "Doctor Teeth", instrument: "piano" },
17+
{ name: "Janice", instrument: "lead guitar" },
18+
{ name: "Sgt. Floyd Pepper", instrument: "bass" },
19+
{ name: "Zoot", instrument: "sax" },
20+
{ name: "Lips", instrument: "trumpet" },
21+
{ name: "Animal", instrument: "drums" } ]
22+
23+
names = electric_mayhem.map (muppet) -> muppet.name
24+
# => [ 'Doctor Teeth', 'Janice', 'Sgt. Floyd Pepper', 'Zoot', 'Lips', 'Animal' ]
25+
{% endhighlight %}
26+
27+
h2. Discussion
28+
29+
Because CoffeeScript has clean support for anonymous functions, mapping an array in CoffeeScript is nearly as easy as it is in Ruby.
30+
31+
Maps are are good way to handle complicated transforms and chained mappings in CoffeeScript. If your transformation is as simple as the one above, however, it may read more cleanly as a <a href="/chapters/arrays/list-comprehensions">list comprehension</a>.
32+

0 commit comments

Comments
 (0)