Skip to content

Commit

Permalink
Merge pull request coffeescript-cookbook#6 from AaronW/master
Browse files Browse the repository at this point in the history
Degrees and Radians
  • Loading branch information
peterhellberg committed May 14, 2011
2 parents b58b6b8 + 6f60ee2 commit 8a89b59
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 4 deletions.
33 changes: 33 additions & 0 deletions chapters/math/radians-degrees.textile
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
layout: recipe
title: Converting Radians and Degrees
chapter: Math
---

h2. Problem

You need to convert between radians and degrees.

h2. Solution

Use Javascript's Math.PI and a simple formula to convert between the two.

{% highlight coffeescript %}
# To convert from radians to degrees
radiansToDegrees = (radians) ->
degrees = radians * 180 / Math.PI

radiansToDegrees(1)
# => 57.29577951308232

# To convert from degrees to radians
degreesToRadians = (degrees) ->
radians = degrees * Math.PI / 180

degreesToRadians(1)
# => 0.017453292519943295
{% endhighlight %}

h2. Discussion

Questions?
32 changes: 32 additions & 0 deletions chapters/strings/finding-substrings.textile
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
---
layout: recipe
title: Finding Substrings
chapter: Strings
---

h2. Problem

You need to find the first or last occurrence of a search string within a message.

h2. Solution

Use Javascript's indexOf() and lastIndexOf() to find the first and last occurrences of a string, respectively.
Syntax: string.indexOf(searchstring, start)

{% highlight coffeescript %}
message = "This is a test string. This has a repeat or two. This might even have a third."
message.indexOf("This",0)
# => 0

# Modifying the start parameter
message.indexOf("This",5)
# => 23

message.lastIndexOf("This")
# => 49

{% endhighlight %}

h2. Discussion

Still need recipe to count occurrences of a given string within a message.
30 changes: 30 additions & 0 deletions chapters/syntax/for_loops.textile
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
layout: recipe
title: For Loops
chapter: Syntax
---

h2. Problem

You need to iterate over an array, object or range with a for loop.

h2. Solution

{% highlight coffeescript %}
# for(i = 1; i<= 10; i++)
x for x in [1..10]
# => [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]

# To count by 2
# for(i=1; i<= 10; i=i+2)
x for x in [1..10] by 2
# => [ 1, 3, 5, 7, 9 ]

# Perform a simple operation like squaring each item.
x * x for x in [1..10]
# = > [1,4,9,16,25,36,49,64,81,100]
{% endhighlight %}

h2. Discussion

Comprehensions replace for loops in CoffeeScript, but they simply compile into the traditional javascript equivalent for-loop.
4 changes: 0 additions & 4 deletions wanted-recipes.textile
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ In the notes below, "JS" means the recipe is just a simple passthrough to an exi

h2. Syntax

* loops: for x in [1..10]
* loops: for x in [1..10] then do_one_thing
* list comp gotcha: y = x for x in [1..3]; y # => 3; but y = (x for x in [1..3]); y # => [ 1, 2, 3 ]
* Ensuring variables are closed over # with "do"

Expand All @@ -21,7 +19,6 @@ h2. Objects

h2. Strings

* Find a substring in a string # not by regex! JS indexOf(), lastIndexOf()
* 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]
Expand Down Expand Up @@ -59,7 +56,6 @@ h2. Dates and Times

h2. Math

* Converting between degrees and radians
* square root # JS Math.sqrt
* Constants # JS Math.PI, Math.E
* floor, ceil, round # JS Math.floor, Math.ceil, Math.round
Expand Down

0 comments on commit 8a89b59

Please sign in to comment.