From 3be22bd43ba3bd985756a3c4a05891b33f754433 Mon Sep 17 00:00:00 2001 From: Jeremy Ashkenas Date: Fri, 24 Dec 2010 09:22:27 -0800 Subject: [PATCH] Documenting and testing 'do' --- documentation/coffee/do.coffee | 4 ++ documentation/coffee/scoped_loops.coffee | 4 -- documentation/index.html.erb | 11 +++--- documentation/js/do.js | 10 +++++ documentation/js/scoped_loops.js | 10 ----- documentation/js/splices.js | 4 +- index.html | 50 ++++++++++-------------- test/test_functions.coffee | 8 ++-- 8 files changed, 45 insertions(+), 56 deletions(-) create mode 100644 documentation/coffee/do.coffee delete mode 100644 documentation/coffee/scoped_loops.coffee create mode 100644 documentation/js/do.js delete mode 100644 documentation/js/scoped_loops.js diff --git a/documentation/coffee/do.coffee b/documentation/coffee/do.coffee new file mode 100644 index 00000000..e30a4fab --- /dev/null +++ b/documentation/coffee/do.coffee @@ -0,0 +1,4 @@ +for fileName in list + do (fileName) -> + fs.readFile fileName, (err, contents) -> + compile fileName, contents.toString() \ No newline at end of file diff --git a/documentation/coffee/scoped_loops.coffee b/documentation/coffee/scoped_loops.coffee deleted file mode 100644 index 45d28358..00000000 --- a/documentation/coffee/scoped_loops.coffee +++ /dev/null @@ -1,4 +0,0 @@ -funcs = [] - -for i in [0..3] -> - funcs.push (number) -> number + i \ No newline at end of file diff --git a/documentation/index.html.erb b/documentation/index.html.erb index 3292a253..8d2770fa 100644 --- a/documentation/index.html.erb +++ b/documentation/index.html.erb @@ -530,13 +530,12 @@ Expressions for own key, value of object

- In JavaScript, it's extremely common to run into trouble when trying to - generate functions within a loop — all of the created functions share - the final value of loop variables. CoffeeScript provides a "scoped loop" - syntax for loops that should have function scope, similar to a forEach. - Note how the body of the loop gets lifted into a cached function. + When using a JavaScript loop to generate functions, it's common to insert + a closure wrapper in order to ensure that loop variables are closed over, + and all the generated functions don't just share the final values. CoffeeScript + provides the do keyword, which immediately invokes a passed function.

- <%= code_for('scoped_loops', 'funcs[3](2)') %> + <%= code_for('do') %>

diff --git a/documentation/js/do.js b/documentation/js/do.js new file mode 100644 index 00000000..388f7f95 --- /dev/null +++ b/documentation/js/do.js @@ -0,0 +1,10 @@ +var fileName, _fn, _i, _len; +_fn = function(fileName) { + return fs.readFile(fileName, function(err, contents) { + return compile(fileName, contents.toString()); + }); +}; +for (_i = 0, _len = list.length; _i < _len; _i++) { + fileName = list[_i]; + _fn(fileName); +} \ No newline at end of file diff --git a/documentation/js/scoped_loops.js b/documentation/js/scoped_loops.js deleted file mode 100644 index f4355e3c..00000000 --- a/documentation/js/scoped_loops.js +++ /dev/null @@ -1,10 +0,0 @@ -var funcs, i, _fn; -funcs = []; -_fn = function(i) { - funcs.push(function(number) { - return number + i; - }); -}; -for (i = 0; i <= 3; i++) { - _fn(i); -} \ No newline at end of file diff --git a/documentation/js/splices.js b/documentation/js/splices.js index a6f916f1..f5795813 100644 --- a/documentation/js/splices.js +++ b/documentation/js/splices.js @@ -1,3 +1,3 @@ -var numbers; +var numbers, _ref; numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; -[].splice.apply(numbers, [3, 4].concat([-3, -4, -5, -6])); \ No newline at end of file +[].splice.apply(numbers, [3, 4].concat(_ref = [-3, -4, -5, -6])), _ref; \ No newline at end of file diff --git a/index.html b/index.html index b7ffe3b9..dedc4712 100644 --- a/index.html +++ b/index.html @@ -908,36 +908,26 @@ ages = (function() { for own key, value of object

- In JavaScript, it's extremely common to run into trouble when trying to - generate functions within a loop — all of the created functions share - the final value of loop variables. CoffeeScript provides a "scoped loop" - syntax for loops that should have function scope, similar to a forEach. - Note how the body of the loop gets lifted into a cached function. + When using a JavaScript loop to generate functions, it's common to insert + a closure wrapper in order to ensure that loop variables are closed over, + and all the generated functions don't just share the final values. CoffeeScript + provides the do keyword, which immediately invokes a passed function.

-
funcs = []
-
-for i in [0..3] ->
-  funcs.push (number) -> number + i
-
var funcs, i, _fn;
-funcs = [];
-_fn = function(i) {
-  funcs.push(function(number) {
-    return number + i;
+    
for fileName in list
+  do (fileName) ->
+    fs.readFile fileName, (err, contents) ->
+      compile fileName, contents.toString()
+
var fileName, _fn, _i, _len;
+_fn = function(fileName) {
+  return fs.readFile(fileName, function(err, contents) {
+    return compile(fileName, contents.toString());
   });
 };
-for (i = 0; i <= 3; i++) {
-  _fn(i);
+for (_i = 0, _len = list.length; _i < _len; _i++) {
+  fileName = list[_i];
+  _fn(fileName);
 }
-
load
run: funcs[3](2)

+
load

@@ -970,12 +960,12 @@ middle = copy.slice(3, 7);;alert(middle);'>run: middle
numbers[3..6] = [-3, -4, -5, -6] -

var numbers;
+
var numbers, _ref;
 numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
-[].splice.apply(numbers, [3, 4].concat([-3, -4, -5, -6]));
-
load
load
run: numbers

+[].splice.apply(numbers, [3, 4].concat(_ref = [-3, -4, -5, -6])), _ref;;alert(numbers);'>run: numbers

Note that JavaScript strings are immutable, and can't be spliced.

diff --git a/test/test_functions.coffee b/test/test_functions.coffee index 193839c6..4123f801 100644 --- a/test/test_functions.coffee +++ b/test/test_functions.coffee @@ -22,18 +22,18 @@ obj = { name: 'Fred' bound: -> - (=> eq this, obj)() + do (=> eq this, obj) unbound: -> - (-> ok this isnt obj)() + do (-> ok this isnt obj) nested: -> (=> - (=> + do (=> (=> eq this, obj )() - )() + ) )() }