From 0cf7801f36f967b8d570d29a6f64e4db62ad6fa6 Mon Sep 17 00:00:00 2001 From: Jeremy Ashkenas Date: Mon, 4 Jan 2010 23:26:27 -0500 Subject: [PATCH] more docs for 0.2 -- blocks and splats --- documentation/coffee/blocks.coffee | 4 ++ documentation/coffee/splats.coffee | 25 +++++++ documentation/index.html.erb | 40 +++++++++-- documentation/js/blocks.js | 8 +++ documentation/js/splats.js | 16 +++++ index.html | 103 +++++++++++++++++++++++++++-- 6 files changed, 182 insertions(+), 14 deletions(-) create mode 100644 documentation/coffee/blocks.coffee create mode 100644 documentation/coffee/splats.coffee create mode 100644 documentation/js/blocks.js create mode 100644 documentation/js/splats.js diff --git a/documentation/coffee/blocks.coffee b/documentation/coffee/blocks.coffee new file mode 100644 index 00000000..6f31ee16 --- /dev/null +++ b/documentation/coffee/blocks.coffee @@ -0,0 +1,4 @@ +$('table.list').each() table => + $('tr.account', table).each() row => + row.show() + row.highlight() diff --git a/documentation/coffee/splats.coffee b/documentation/coffee/splats.coffee new file mode 100644 index 00000000..de02d242 --- /dev/null +++ b/documentation/coffee/splats.coffee @@ -0,0 +1,25 @@ +gold: silver: the_field: "unknown" + +medalists: first, second, *rest => + gold: first + silver: second + the_field: rest + +contenders: [ + "Michael Phelps" + "Liu Xiang" + "Yao Ming" + "Allyson Felix" + "Shawn Johnson" + "Roman Sebrle" + "Guo Jingjing" + "Tyson Gay" + "Asafa Powell" + "Usain Bolt" +] + +medalists(*contenders) + +alert("Gold: " + gold) +alert("Silver: " + silver) +alert("The Field: " + the_field) \ No newline at end of file diff --git a/documentation/index.html.erb b/documentation/index.html.erb index 5cb307cd..d520428f 100644 --- a/documentation/index.html.erb +++ b/documentation/index.html.erb @@ -82,11 +82,13 @@ Conditionals, Ternaries, and Conditional Assignment
The Existence Operator
Aliases
+ Splats
While Loops
Comprehensions (Arrays, Objects, and Ranges)
Array Slicing and Splicing with Ranges
Everything is an Expression
Inheritance, and Calling Super from a Subclass
+ Blocks
Embedded JavaScript
Switch/When/Else
Try/Catch/Finally
@@ -101,9 +103,9 @@

CoffeeScript on the left, compiled JavaScript output on the right.

<%= code_for('overview', 'cubed_list') %> - +

- For a longer CoffeeScript example, check out + For a longer CoffeeScript example, check out Underscore.coffee, a port of Underscore.js to CoffeeScript, which, when compiled, passes the complete Underscore test suite. @@ -141,7 +143,7 @@ gem install coffee-script -r, --run - Compile and execute the CoffeeScripts without saving the intermediate + Compile and execute scripts without saving the intermediate JavaScript. Requires Narwhal. @@ -177,7 +179,7 @@ gem install coffee-script -e, --eval Compile and print a little snippet of CoffeeScript directly from the - command line (or from stdin). For example:
coffee -e "square: x => x * x." + command line (or from stdin). For example:
coffee -e "square: x => x * x" @@ -248,7 +250,8 @@ coffee --print app/scripts/*.coffee > concatenation.js

Functions and Invocation Functions are defined by a list of parameters, an arrow, and the - function body. The empty function looks like this: => + function body. The empty function looks like this: =>. All + functions in CoffeeScript are named, for the benefit of debug messages.

<%= code_for('functions', 'cube(5)') %> @@ -261,7 +264,7 @@ coffee --print app/scripts/*.coffee > concatenation.js <%= code_for('assignment', 'greeting') %>

Declarations of new variables are pushed up to the top of the nearest - lexical scope, so that assignment may always be used within expressions. + lexical scope, so that assignment may always be performed within expressions.

@@ -358,6 +361,15 @@ coffee --print app/scripts/*.coffee > concatenation.js

<%= code_for('aliases') %> +

+ Splats + The JavaScript arguments object is a useful way to work with + functions that accept variable numbers of arguments. CoffeeScript provides + splats *, both for function definition as well as invocation, + making variable arguments a little bit more palatable. +

+ <%= code_for('splats', true) %> +

While Loops The only low-level loop that CoffeeScript provides is the while loop. @@ -407,7 +419,7 @@ coffee --print app/scripts/*.coffee > concatenation.js array with new values (to splice it).

<%= code_for('splices', 'numbers') %> - +

Everything is an Expression (at least, as much as possible) You might have noticed how even though we don't add return statements @@ -455,6 +467,15 @@ coffee --print app/scripts/*.coffee > concatenation.js

<%= code_for('super', true) %> +

+ Blocks + Many common looping functions (in Prototype, jQuery, and Underscore, + for example) take a single function as their final argument. To functions + easier to pass, CoffeeScript includes Ruby-style block syntax, so you don't + have to close the parentheses on the other side. +

+ <%= code_for('blocks') %> +

Embedded JavaScript If you ever need to interpolate literal JavaScript snippets, you can @@ -499,6 +520,11 @@ coffee --print app/scripts/*.coffee > concatenation.js

  • Bugs and Feature Requests
  • +
  • + BistroCar
    + A Rails plugin by Jonas Nicklas that includes CoffeeScript helpers, + bundling and minification. +
  • Contributing

    diff --git a/documentation/js/blocks.js b/documentation/js/blocks.js new file mode 100644 index 00000000..f95dd31b --- /dev/null +++ b/documentation/js/blocks.js @@ -0,0 +1,8 @@ +(function(){ + $('table.list').each(function(table) { + return $('tr.account', table).each(function(row) { + row.show(); + return row.highlight(); + }); + }); +})(); \ No newline at end of file diff --git a/documentation/js/splats.js b/documentation/js/splats.js new file mode 100644 index 00000000..31f25a79 --- /dev/null +++ b/documentation/js/splats.js @@ -0,0 +1,16 @@ +(function(){ + var contenders, gold, medalists, silver, the_field; + gold = silver = the_field = "unknown"; + medalists = function medalists(first, second) { + var rest; + rest = Array.prototype.slice.call(arguments, 2); + gold = first; + silver = second; + return the_field = rest; + }; + contenders = ["Michael Phelps", "Liu Xiang", "Yao Ming", "Allyson Felix", "Shawn Johnson", "Roman Sebrle", "Guo Jingjing", "Tyson Gay", "Asafa Powell", "Usain Bolt"]; + medalists.apply(this, contenders); + alert("Gold: " + gold); + alert("Silver: " + silver); + alert("The Field: " + the_field); +})(); \ No newline at end of file diff --git a/index.html b/index.html index a64d1be7..044c1a4f 100644 --- a/index.html +++ b/index.html @@ -55,11 +55,13 @@ Conditionals, Ternaries, and Conditional Assignment
    The Existence Operator
    Aliases
    + Splats
    While Loops
    Comprehensions (Arrays, Objects, and Ranges)
    Array Slicing and Splicing with Ranges
    Everything is an Expression
    Inheritance, and Calling Super from a Subclass
    + Blocks
    Embedded JavaScript
    Switch/When/Else
    Try/Catch/Finally
    @@ -166,9 +168,9 @@ cubed_list = (function() { return __c; })(); ;alert(cubed_list);'>run: cubed_list
    - +

    - For a longer CoffeeScript example, check out + For a longer CoffeeScript example, check out Underscore.coffee, a port of Underscore.js to CoffeeScript, which, when compiled, passes the complete Underscore test suite. @@ -206,7 +208,7 @@ gem install coffee-script -r, --run - Compile and execute the CoffeeScripts without saving the intermediate + Compile and execute scripts without saving the intermediate JavaScript. Requires Narwhal. @@ -242,7 +244,7 @@ gem install coffee-script -e, --eval Compile and print a little snippet of CoffeeScript directly from the - command line (or from stdin). For example:
    coffee -e "square: x => x * x." + command line (or from stdin). For example:
    coffee -e "square: x => x * x" @@ -313,7 +315,8 @@ coffee --print app/scripts/*.coffee > concatenation.js

    Functions and Invocation Functions are defined by a list of parameters, an arrow, and the - function body. The empty function looks like this: => + function body. The empty function looks like this: =>. All + functions in CoffeeScript are named, for the benefit of debug messages.

    square: x => x * x
     cube:   x => square(x) * x
    @@ -350,7 +353,7 @@ difficulty = 0.5;
     ;alert(greeting);'>run: greeting

    Declarations of new variables are pushed up to the top of the nearest - lexical scope, so that assignment may always be used within expressions. + lexical scope, so that assignment may always be performed within expressions.

    @@ -539,6 +542,68 @@ let_the_wild_rumpus_begin() unless answer < speed_limit ? accelerate() : null;
    +

    + Splats + The JavaScript arguments object is a useful way to work with + functions that accept variable numbers of arguments. CoffeeScript provides + splats *, both for function definition as well as invocation, + making variable arguments a little bit more palatable. +

    +
    gold: silver: the_field: "unknown"
    +
    +medalists: first, second, *rest =>
    +  gold:       first
    +  silver:     second
    +  the_field:  rest
    +
    +contenders: [
    +  "Michael Phelps"
    +  "Liu Xiang"
    +  "Yao Ming"
    +  "Allyson Felix"
    +  "Shawn Johnson"
    +  "Roman Sebrle"
    +  "Guo Jingjing"
    +  "Tyson Gay"
    +  "Asafa Powell"
    +  "Usain Bolt"
    +]
    +
    +medalists(*contenders)
    +
    +alert("Gold: " + gold)
    +alert("Silver: " + silver)
    +alert("The Field: " + the_field)
    +
    var contenders, gold, medalists, silver, the_field;
    +gold = silver = the_field = "unknown";
    +medalists = function medalists(first, second) {
    +  var rest;
    +  rest = Array.prototype.slice.call(arguments, 2);
    +  gold = first;
    +  silver = second;
    +  return the_field = rest;
    +};
    +contenders = ["Michael Phelps", "Liu Xiang", "Yao Ming", "Allyson Felix", "Shawn Johnson", "Roman Sebrle", "Guo Jingjing", "Tyson Gay", "Asafa Powell", "Usain Bolt"];
    +medalists.apply(this, contenders);
    +alert("Gold: " + gold);
    +alert("Silver: " + silver);
    +alert("The Field: " + the_field);
    +

    +

    While Loops The only low-level loop that CoffeeScript provides is the while loop. @@ -713,7 +778,7 @@ numbers.splice.apply(numbers, [run: numbers
    - +

    Everything is an Expression (at least, as much as possible) You might have noticed how even though we don't add return statements @@ -933,6 +998,25 @@ sam.move(); tom.move(); ;'>run
    +

    + Blocks + Many common looping functions (in Prototype, jQuery, and Underscore, + for example) take a single function as their final argument. To functions + easier to pass, CoffeeScript includes Ruby-style block syntax, so you don't + have to close the parentheses on the other side. +

    +
    $('table.list').each() table =>
    +  $('tr.account', table).each() row =>
    +    row.show()
    +    row.highlight()
    +
    $('table.list').each(function(table) {
    +  return $('tr.account', table).each(function(row) {
    +    row.show();
    +    return row.highlight();
    +  });
    +});
    +

    +

    Embedded JavaScript If you ever need to interpolate literal JavaScript snippets, you can @@ -1050,6 +1134,11 @@ world...";

  • Bugs and Feature Requests
  • +
  • + BistroCar
    + A Rails plugin by Jonas Nicklas that includes CoffeeScript helpers, + bundling and minification. +
  • Contributing