Skip to content

Latest commit

 

History

History
130 lines (98 loc) · 4.08 KB

wanted-recipes.textile

File metadata and controls

130 lines (98 loc) · 4.08 KB
layout title
default
Wanted Recipes

Wanted Recipes

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.

In the notes below, “JS” means the recipe is just a simple passthrough to an existing JavaScript method.

Syntax

  • loops: for x in [1..10]
  • loops: for x in [1..10] then do_one_thing
  • list comprehensions: y*3 for y in (x*2 for x in [1,2,3]) # => [6, 12, 18]
  • maps: Previous line might be better represented as ([1,2,3].map (x) → x*2).map (y) → y*3
  • 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”

Objects

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]
  • substring # str.substring(x,y) = str.slice(x,y) = str[x..y-1] === str[x…y]
  • Uppercasing a string # JS toUpperCase()
  • Lowercasing a string # JS toLowerCase()
  • Replacing substrings

Arrays

  • Using Arrays to Swap Variables # [x,y] = [y,x]
  • Mapping arrays to other arrays with map # [1,2,3].map (x) → x*2 # => [2, 4, 6]
  • 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 %}
    [“example”, "contrived ", "pretty ", "a ", "is ", "here "].reduceRight (x,y) → x+y
  • => ‘here is a pretty contrived example’
    {% endhighlight %}
  • Testing every element in an array
    {% highlight coffeescript %}
    evens = (x for x in [0..10] by 2)
    even = (x) → x % 2 == 0
    evens.every even
  • => true
    {% endhighlight %}
  • Filtering arrays # [1..10.filter (x) → x % 2 == 0 # => [ 2, 4, 6, 8, 10 ]
  • Detecting presence of matching items in an array # [1..10].some (x) → x % 2 == 0 # => true
  • Processing an array item by item # [10..1].forEach (x) → console.log x # => nothing;, but a countdown is displayed on the console

Dates and Times

  • Calculating the phase of the moon
  • Holidays: Calculating Easter
  • Holidays: Calculating US Thanksgiving
  • Holidays: Calculating CA Thanksgiving

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
  • Raising a number to a power # JS Math.pow(x, y)
  • Logarithms # Math.log
  • Finding the base-n log # Math.log(num) / Math.log(base)
  • Exponents # Math.exp

Functions

  • Nested functions

{% highlight coffeescript %}
hypotenuse = (a, b) →
square = (x) → x * x
Math.sqrt(square(a) + square(b))

console.log hypotenuse 3, 4

  1. => 5

square 5

  1. ReferenceError: square is not defined
    {% endhighlight %}
  • Optional Arguments # use arg? to detect presence: if arg=0, arg? == true

{% highlight coffeescript %}
foo = (a, b=42, c) → if c? then a*b*c else a*b
[Function]
foo 6

  1. => 252
    foo 1, 2
  2. => 2
    foo 1, 2, 3
  3. => 6
    {% endhighlight %}
  • Variable arguments with splats
  • Recursion of unnamed functions with arguments.callee

{% highlight coffeescript %}
console.log [1,2,3,4,5].map (x) →
if x <= 1 then return 1
return x * arguments.callee(x-1)

  1. => [1, 2, 6, 24, 120 ]
    {% endhighlight %}

jQuery

  • Callback bindings # using => instead of →
  • jQuery AJAX from CS

Regular Expressions

  • Searching for substrings # “foo bar baz”.match(/ba./) # => [ ‘bar’, index: 4, input: ‘foo bar baz’ ]
  • Searching for substrings # “foo bar baz”.search(/ba./) # => 4
  • Replacing substrings # “foo bar baz”.replace( /ba./, ‘foo’) # => “foo foo baz”
  • Extended “heregexes”

AJAX

  • Getting data from a remote server # using raw XHTTPRequest instead of jQuery’s $.ajax