Skip to content

Commit 1d81784

Browse files
committed
Merge branch 'master' of github.com:coffeescript-cookbook/coffeescript-cookbook.github.com
2 parents 8d1d36c + 9f367f7 commit 1d81784

File tree

5 files changed

+139
-2
lines changed

5 files changed

+139
-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: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
---
2+
layout: recipe
3+
title: Get Days Between two Dates
4+
chapter: Dates and Times
5+
---
6+
7+
h2. Problem
8+
9+
You need to find how much seconds minutes, hours, days, months or years has passed between two dates.
10+
11+
h2. Solution
12+
13+
Use JavaScript's Date function getTime(). Which provides how much time in miliseconds has passed since 01/01/1970:
14+
15+
{% highlight coffeescript %}
16+
DAY = 1000 * 60 * 60 * 24
17+
18+
d1 = new Date('02/01/2011')
19+
d2 = new Date('02/06/2011')
20+
21+
days_passed = Math.round((d2.getTime() - d1.getTime()) / DAY)
22+
{% endhighlight %}
23+
24+
h2. Discussion
25+
26+
Using miliseconds makes the life easier to avoid overflow mistakes with Dates. So we first calculate how much miliseconds has a day.
27+
Then, given two distincit dates, just get the diference in miliseconds betwen two dates and then divide by how much miliseconds has a
28+
day. It will get you the days between two distinct dates.
29+
30+
If you'd like to calculate the hours between two dates objects you can do that just by dividing the diference in miliseconds by the
31+
convertion of miliseconds to hours. The same goes to minutes and seconds.
32+
33+
{% highlight coffeescript %}
34+
HOUR = 1000 * 60 * 60
35+
36+
d1 = new Date('02/01/2011 02:20')
37+
d2 = new Date('02/06/2011 05:20')
38+
39+
hour_passed = Math.round((d2.getTime() - d1.getTime()) / HOUR)
40+
{% endhighlight %}
41+

chapters/regular_expressions/heregexes.textile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,12 @@ pattern = ///
2525
h2. Discussion
2626

2727
Breaking up your complex regular expressions and commenting key sections makes them a lot more decipherable and maintainable. For example, changing this regex to allow an optional space between the prefix and line number would now be fairly obvious.
28+
29+
h3. Whitespace characters in heregexes
30+
31+
Whitespace is ignored in heregexes -- so what do you do if you need to match a
32+
literal ASCII space?
33+
34+
One solution is to use the @\s@ character class, which will match spaces, tabs
35+
and line breaks. If you only want to match a space, though, you'll need to use
36+
@\x20@ to denote a literal ASCII space.
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)