Skip to content

Commit 9f367f7

Browse files
committed
Merge pull request coffeescript-cookbook#15 from drtangible/master
New recipes: Using arrays to swap variable, splitting a string
2 parents 8e58565 + ea1106f commit 9f367f7

File tree

3 files changed

+89
-2
lines changed

3 files changed

+89
-2
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
---
2+
layout: recipe
3+
title: Using Arrays to Swap Variables
4+
chapter: Arrays
5+
---
6+
7+
h1. Using Arrays to Swap Variables
8+
9+
h2. Problem
10+
11+
You want to use an array to swap variables.
12+
13+
h2. Solution
14+
15+
Use CoffeeScript's "destructuring assignment":http://jashkenas.github.com/coffee-script/#destructuring syntax:
16+
17+
{% highlight coffeescript %}
18+
a = 1
19+
b = 3
20+
21+
[a, b] = [b, a]
22+
23+
a
24+
# => 3
25+
26+
b
27+
# => 1
28+
{% endhighlight %}
29+
30+
31+
h2. Discussion
32+
33+
Destructuring assignment allows swapping two values without the use of a temporary variable.
34+
35+
This can be useful when traversing arrays and ensuring iteration only happens over the shortest one:
36+
37+
{% highlight coffeescript %}
38+
39+
ray1 = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
40+
ray2 = [ 5, 9, 14, 20 ]
41+
42+
intersection = (a, b) ->
43+
[a, b] = [b, a] if a.length > b.length
44+
value for value in a when value in b
45+
46+
intersection ray1, ray2
47+
# => [ 5, 9 ]
48+
49+
intersection ray2, ray1
50+
# => [ 5, 9 ]
51+
52+
{% endhighlight %}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
---
2+
layout: recipe
3+
title: Splitting a String
4+
chapter: Strings
5+
---
6+
7+
h2. Problem
8+
9+
You want to split a string.
10+
11+
h2. Solution
12+
13+
Use JavaScript's String split() method:
14+
15+
{% highlight coffeescript %}
16+
"foo bar baz".split " "
17+
# => [ 'foo', 'bar', 'baz' ]
18+
{% endhighlight %}
19+
20+
h2. Discussion
21+
22+
String's split() is a standard JavaScript method. It can be used to split a string on any delimiter, including regular expressions. It also accepts a second parameter that specifies the number of splits to return.
23+
24+
{% highlight coffeescript %}
25+
"foo-bar-baz".split "-"
26+
# => [ 'foo', 'bar', 'baz' ]
27+
{% endhighlight %}
28+
29+
{% highlight coffeescript %}
30+
"foo bar \t baz".split /\s+/
31+
# => [ 'foo', 'bar', 'baz' ]
32+
{% endhighlight %}
33+
34+
{% highlight coffeescript %}
35+
"the sun goes down and I sit on the old broken-down river pier".split " ", 2
36+
# => [ 'the', 'sun' ]
37+
{% endhighlight %}

wanted-recipes.textile

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ h2. Objects
1717
h2. Strings
1818

1919
* HTML methods # JS .sup(), .sub(), .blink(), .link(url), etc. May not exist in your JS impl!
20-
* Splitting a string # JS "foo bar baz".split ' ' # => [ 'foo', 'bar', 'baz' ]
2120
* substr # str.substr(x,y) === str[x..x+y-1] === str[x...x+y]
2221
* substring # str.substring(x,y) === str.slice(x,y) === str[x..y-1] === str[x...y]
2322
* Uppercasing a string # JS toUpperCase()
@@ -27,7 +26,6 @@ h2. Strings
2726

2827
h2. Arrays
2928

30-
* Using Arrays to Swap Variables # [x,y] = [y,x]
3129
* Reducing arrays to values (ruby inject) with reduce # [1..10].reduce (a,b) -> a+b # 55
3230
* Reducing arrays in reverse order # JS reduceRight
3331
{% highlight coffeescript %}

0 commit comments

Comments
 (0)