|
| 1 | +--- |
| 2 | +layout: default |
| 3 | +title: Wanted Recipes |
| 4 | +--- |
| 5 | +h1. Wanted Recipes |
| 6 | + |
| 7 | +Here's a list of recipes we think we need. Pick one, implement it, and remove it from the page. Alternately, add a quick note here for a recipe you'd like to see so someone else can add it. |
| 8 | + |
| 9 | +In the notes below, "JS" means the recipe is just a simple passthrough to an existing JavaScript method. |
| 10 | + |
| 11 | +h2. Syntax |
| 12 | + |
| 13 | +* loops: for x in [1..10] |
| 14 | +* loops: for x in [1..10] then do_one_thing |
| 15 | +* list comprehensions: y*3 for y in (x*2 for x in [1,2,3]) # => [6, 12, 18] |
| 16 | +* maps: Previous line might be better represented as ([1,2,3].map (x) -> x*2).map (y) -> y*3 |
| 17 | +* list comp gotcha: y = x for x in [1..3]; y # => 3; but y = (x for x in [1..3]); y # => [ 1, 2, 3 ] |
| 18 | +* Ensuring variables are closed over # with "do" |
| 19 | + |
| 20 | +h2. Objects |
| 21 | + |
| 22 | +* cloning an object (see http://stackoverflow.com/questions/122102/what-is-the-most-efficient-way-to-clone-a-javascript-object ) -- snag both the generic JS version, port to CS, also snag the jQuery version and port to CS+jQ. |
| 23 | + |
| 24 | +h2. Strings |
| 25 | + |
| 26 | +* Find a substring in a string # not by regex! JS indexOf(), lastIndexOf() |
| 27 | +* HTML methods # JS .sup(), .sub(), .blink(), .link(url), etc. May not exist in your JS impl! |
| 28 | +* Splitting a string # JS "foo bar baz".split ' ' # => [ 'foo', 'bar', 'baz' ] |
| 29 | +* substr # str.substr(x,y) === str[x..x+y-1] === str[x...x+y] |
| 30 | +* substring # str.substring(x,y) === str.slice(x,y) === str[x..y-1] === str[x...y] |
| 31 | +* Uppercasing a string # JS toUpperCase() |
| 32 | +* Lowercasing a string # JS toLowerCase() |
| 33 | +* Replacing substrings |
| 34 | + |
| 35 | +h2. Arrays |
| 36 | + |
| 37 | +* Using Arrays to Swap Variables # [x,y] = [y,x] |
| 38 | +* Mapping arrays to other arrays with map # [1,2,3].map (x) -> x*2 # => [2, 4, 6] |
| 39 | +* Reducing arrays to values (ruby inject) with reduce # [1..10].reduce (a,b) -> a+b # 55 |
| 40 | +* Reducing arrays in reverse order # JS reduceRight |
| 41 | +{% highlight coffeescript %} |
| 42 | +["example", "contrived ", "pretty ", "a ", "is ", "here "].reduceRight (x,y) -> x+y |
| 43 | +# => 'here is a pretty contrived example' |
| 44 | +{% endhighlight %} |
| 45 | +* Testing every element in an array |
| 46 | +{% highlight coffeescript %} |
| 47 | +evens = (x for x in [0..10] by 2) |
| 48 | +even = (x) -> x % 2 == 0 |
| 49 | +evens.every even |
| 50 | +# => true |
| 51 | +{% endhighlight %} |
| 52 | +* Filtering arrays # [1..10.filter (x) -> x % 2 == 0 # => [ 2, 4, 6, 8, 10 ] |
| 53 | +* Detecting presence of matching items in an array # [1..10].some (x) -> x % 2 == 0 # => true |
| 54 | +* Processing an array item by item # [10..1].forEach (x) -> console.log x # => nothing;, but a countdown is displayed on the console |
| 55 | + |
| 56 | +h2. Dates and Times |
| 57 | + |
| 58 | +* Calculating the phase of the moon |
| 59 | +* Holidays: Calculating Easter |
| 60 | +* Holidays: Calculating US Thanksgiving |
| 61 | +* Holidays: Calculating CA Thanksgiving |
| 62 | + |
| 63 | +h2. Math |
| 64 | + |
| 65 | +* Converting between degrees and radians |
| 66 | +* square root # JS Math.sqrt |
| 67 | +* Constants # JS Math.PI, Math.E |
| 68 | +* floor, ceil, round # JS Math.floor, Math.ceil, Math.round |
| 69 | +* Raising a number to a power # JS Math.pow(x, y) |
| 70 | +* Logarithms # Math.log |
| 71 | +* Finding the base-n log # Math.log(num) / Math.log(base) |
| 72 | +* Exponents # Math.exp |
| 73 | + |
| 74 | +h2. Functions |
| 75 | + |
| 76 | +* Nested functions |
| 77 | + |
| 78 | +{% highlight coffeescript %} |
| 79 | +hypotenuse = (a, b) -> |
| 80 | + square = (x) -> x * x |
| 81 | + Math.sqrt(square(a) + square(b)) |
| 82 | + |
| 83 | +console.log hypotenuse 3, 4 |
| 84 | +# => 5 |
| 85 | + |
| 86 | +square 5 |
| 87 | +# ReferenceError: square is not defined |
| 88 | +{% endhighlight %} |
| 89 | + |
| 90 | +* Optional Arguments # use arg? to detect presence: if arg=0, arg? == true |
| 91 | + |
| 92 | +{% highlight coffeescript %} |
| 93 | +foo = (a, b=42, c) -> if c? then a*b*c else a*b |
| 94 | +[Function] |
| 95 | +foo 6 |
| 96 | +# => 252 |
| 97 | +foo 1, 2 |
| 98 | +# => 2 |
| 99 | +foo 1, 2, 3 |
| 100 | +# => 6 |
| 101 | +{% endhighlight %} |
| 102 | + |
| 103 | +* Variable arguments with splats |
| 104 | +* Recursion of unnamed functions with arguments.callee |
| 105 | + |
| 106 | +{% highlight coffeescript %} |
| 107 | +console.log [1,2,3,4,5].map (x) -> |
| 108 | + if x <= 1 then return 1 |
| 109 | + return x * arguments.callee(x-1) |
| 110 | +# => [1, 2, 6, 24, 120 ] |
| 111 | +{% endhighlight %} |
| 112 | + |
| 113 | +h2. jQuery |
| 114 | + |
| 115 | +* Callback bindings # using => instead of -> |
| 116 | +* jQuery AJAX from CS |
| 117 | + |
| 118 | +h2. Regular Expressions |
| 119 | + |
| 120 | +* Searching for substrings # "foo bar baz".match(/ba./) # => [ 'bar', index: 4, input: 'foo bar baz' ] |
| 121 | +* Searching for substrings # "foo bar baz".search(/ba./) # => 4 |
| 122 | +* Replacing substrings # "foo bar baz".replace( /ba./, 'foo') # => "foo foo baz" |
| 123 | +* Extended "heregexes" |
| 124 | + |
| 125 | +h2. AJAX |
| 126 | + |
| 127 | +* Getting data from a remote server # using raw XHTTPRequest instead of jQuery's $.ajax |
| 128 | + |
| 129 | + |
| 130 | + |
0 commit comments