Skip to content

Commit cef2554

Browse files
committed
Merge pull request coffeescript-cookbook#37 from fhemberger/master
Various updates
2 parents 5c1ec63 + db17fc3 commit cef2554

File tree

2 files changed

+6
-9
lines changed

2 files changed

+6
-9
lines changed

chapters/arrays/max-array-value.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,20 @@ You need to find the largest value contained in an array.
99

1010
## Solution
1111

12-
In ECMAScript 5, use `Array#reduce`. In older javascripts, use Math.max over a list comprehension:
12+
In ECMAScript 5, use `Array#reduce`. In older javascripts, use Math.max:
1313

1414
{% highlight coffeescript %}
1515
# ECMAScript 5
1616
[12,32,11,67,1,3].reduce (a,b) -> Math.max a, b
1717
# => 67
1818

1919
# Pre-EC5
20-
max = Math.max(max or= num, num) for num in [12,32,11,67,1,3]
20+
max = Math.max.apply(null, [12,32,11,67,1,3])
2121
# => [ 12, 32, 32, 67, 67, 67 ]
2222
max
2323
# => 67
2424
{% endhighlight %}
2525

2626
## Discussion
2727

28-
`Math.max` compares two numbers and returns the larger of the two; the rest of this recipe (both versions) is just iterating over the array to find the largest one. It is interesting to note that when assigning from a list comprehension, the last item evaluated will be returned to the assignment but the expression itself (such as in the node.js coffeescript interpreter) evaluates to the list comprehension's complete mapping. This means that the Pre-EC5 version will duplicate the array.
29-
30-
For very large arrays in ECMAScript 4, a more memory efficient approach might be to initialize `max` to the first element of the array, and then loop over it with a traditional for loop. Since non-idiomatic CoffeeScript is discouraged in the Cookbook, this approach is left as an exercise to the reader.
28+
`Math.max` compares two numbers and returns the larger of the two; the rest of this recipe (both versions) is just iterating over the array to find the largest one.

chapters/strings/trimming-whitespace-from-a-string.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,12 @@ To trim only trailing whitespace, use the following:
3434

3535
## Discussion
3636

37-
Opera, Firefox and Chrome all have a native string prototype `trim` method, and the other browsers could add one as well. For this particular method, I would use the built-in method where possible:
37+
Opera, Firefox and Chrome all have a native string prototype `trim` method, and the other browsers could add one as well. For this particular method, I would use the built-in method where possible, otherwise create a polyfill:
3838

3939
{% highlight coffeescript %}
40-
trim = (val) ->
41-
if String::trim? then val.trim() else val.replace /^\s+|\s+$/g, ""
40+
if !String::trim? then String::trim = -> @replace /^\s+|\s+$/g, ""
4241

43-
trim " padded string "
42+
" padded string ".trim
4443
# => 'padded string'
4544
{% endhighlight %}
4645

0 commit comments

Comments
 (0)