From b4ce259b8312b203521dd34a28dbf44417d97bfa Mon Sep 17 00:00:00 2001 From: Dan Gilbert Date: Thu, 9 Jun 2011 16:56:54 -0500 Subject: [PATCH 1/2] Adding example 'Using Arrays to Swap Variables'. --- .../using-arrays-to-swap-variables.textile | 52 +++++++++++++++++++ wanted-recipes.textile | 1 - 2 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 chapters/arrays/using-arrays-to-swap-variables.textile diff --git a/chapters/arrays/using-arrays-to-swap-variables.textile b/chapters/arrays/using-arrays-to-swap-variables.textile new file mode 100644 index 0000000..1049002 --- /dev/null +++ b/chapters/arrays/using-arrays-to-swap-variables.textile @@ -0,0 +1,52 @@ +--- +layout: recipe +title: Using Arrays to Swap Variables +chapter: Arrays +--- + +h1. Using Arrays to Swap Variables + +h2. Problem + +You want to use an array to swap variables. + +h2. Solution + +Use CoffeeScript's "destructuring assignment":http://jashkenas.github.com/coffee-script/#destructuring syntax: + +{% highlight coffeescript %} +a = 1 +b = 3 + +[a, b] = [b, a] + +a +# => 3 + +b +# => 1 +{% endhighlight %} + + +h2. Discussion + +Destructuring assignment allows swapping two values without the use of a temporary variable. + +This can be useful when traversing arrays and ensuring iteration only happens over the shortest one: + +{% highlight coffeescript %} + +ray1 = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ] +ray2 = [ 5, 9, 14, 20 ] + +intersection = (a, b) -> + [a, b] = [b, a] if a.length > b.length + value for value in a when value in b + +intersection ray1, ray2 +# => [ 5, 9 ] + +intersection ray2, ray1 +# => [ 5, 9 ] + +{% endhighlight %} diff --git a/wanted-recipes.textile b/wanted-recipes.textile index a0c37de..b63aa26 100644 --- a/wanted-recipes.textile +++ b/wanted-recipes.textile @@ -28,7 +28,6 @@ h2. Strings h2. Arrays -* Using Arrays to Swap Variables # [x,y] = [y,x] * Reducing arrays to values (ruby inject) with reduce # [1..10].reduce (a,b) -> a+b # 55 * Reducing arrays in reverse order # JS reduceRight {% highlight coffeescript %} From ea1106f5875e2dae28de028ddb96c9f64083ecf4 Mon Sep 17 00:00:00 2001 From: Dan Gilbert Date: Fri, 10 Jun 2011 17:26:18 -0500 Subject: [PATCH 2/2] New recipe: splitting a string. --- chapters/strings/splitting-a-string.textile | 37 +++++++++++++++++++++ wanted-recipes.textile | 1 - 2 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 chapters/strings/splitting-a-string.textile diff --git a/chapters/strings/splitting-a-string.textile b/chapters/strings/splitting-a-string.textile new file mode 100644 index 0000000..83c1162 --- /dev/null +++ b/chapters/strings/splitting-a-string.textile @@ -0,0 +1,37 @@ +--- +layout: recipe +title: Splitting a String +chapter: Strings +--- + +h2. Problem + +You want to split a string. + +h2. Solution + +Use JavaScript's String split() method: + +{% highlight coffeescript %} +"foo bar baz".split " " +# => [ 'foo', 'bar', 'baz' ] +{% endhighlight %} + +h2. Discussion + +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. + +{% highlight coffeescript %} +"foo-bar-baz".split "-" +# => [ 'foo', 'bar', 'baz' ] +{% endhighlight %} + +{% highlight coffeescript %} +"foo bar \t baz".split /\s+/ +# => [ 'foo', 'bar', 'baz' ] +{% endhighlight %} + +{% highlight coffeescript %} +"the sun goes down and I sit on the old broken-down river pier".split " ", 2 +# => [ 'the', 'sun' ] +{% endhighlight %} diff --git a/wanted-recipes.textile b/wanted-recipes.textile index b63aa26..ea6427a 100644 --- a/wanted-recipes.textile +++ b/wanted-recipes.textile @@ -18,7 +18,6 @@ h2. Objects h2. Strings * HTML methods # JS .sup(), .sub(), .blink(), .link(url), etc. May not exist in your JS impl! -* Splitting a string # JS "foo bar baz".split ' ' # => [ 'foo', 'bar', 'baz' ] * substr # str.substr(x,y) === str[x..x+y-1] === str[x...x+y] * substring # str.substring(x,y) === str.slice(x,y) === str[x..y-1] === str[x...y] * Uppercasing a string # JS toUpperCase()