From 56482a31664a14965772f8a48d2bd755e0613e9c Mon Sep 17 00:00:00 2001 From: Geoffrey Booth Date: Tue, 22 Nov 2016 09:30:39 -0800 Subject: [PATCH] =?UTF-8?q?Docs=20for=20`for=E2=80=A6from`=20(#4368)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Documentation of `for...from` for iterating over generator functions * Add note that the CoffeeScript compiler does not, in fact, generate JavaScript that runs in every JavaScript runtime :cry: --- Cakefile | 2 +- .../examples/generator_iteration.coffee | 13 +++++++++ documentation/index.html.js | 29 +++++++++++++++---- 3 files changed, 38 insertions(+), 6 deletions(-) create mode 100644 documentation/examples/generator_iteration.coffee diff --git a/Cakefile b/Cakefile index e9f92a5e7c..84ba247d43 100644 --- a/Cakefile +++ b/Cakefile @@ -59,7 +59,7 @@ codeFor = -> cshtml = "
#{hljs.highlight('coffeescript', cs).value}
" # Temporary fix until highlight.js adds support for newer CoffeeScript keywords # Added in https://github.com/isagalaev/highlight.js/pull/1357, awaiting release - if file in ['generators', 'modules'] + if file in ['generator_iteration', 'generators', 'modules'] cshtml = cshtml.replace /(yield|import|export|from|as|default) /g, '$1 ' jshtml = "
#{hljs.highlight('javascript', js).value}
" append = if executable is yes then '' else "alert(#{executable});" diff --git a/documentation/examples/generator_iteration.coffee b/documentation/examples/generator_iteration.coffee new file mode 100644 index 0000000000..ec98b08f14 --- /dev/null +++ b/documentation/examples/generator_iteration.coffee @@ -0,0 +1,13 @@ +fibonacci = -> + [previous, current] = [1, 1] + loop + [previous, current] = [current, previous + current] + yield current + return + +getFibonacciNumbers = (length) -> + results = [1] + for n from fibonacci() + results.push n + break if results.length is length + results diff --git a/documentation/index.html.js b/documentation/index.html.js index 75bf60c954..6edec190c3 100644 --- a/documentation/index.html.js +++ b/documentation/index.html.js @@ -107,8 +107,17 @@ compiles one-to-one into the equivalent JS, and there is no interpretation at runtime. You can use any existing JavaScript library seamlessly from CoffeeScript (and vice-versa). The compiled output is - readable and pretty-printed, will work in every JavaScript runtime, and tends - to run as fast or faster than the equivalent handwritten JavaScript. + readable, pretty-printed, and tends to run as fast or faster than the + equivalent handwritten JavaScript. +

+ +

+ The CoffeeScript compiler goes to great lengths to generate output JavaScript + that runs in every JavaScript runtime, but there are exceptions. Use + generator functions only if you know that your + target + runtimes can support them. If you use modules, you + will need to use an additional tool to resolve them.

@@ -577,6 +586,9 @@ Block check to avoid properties that may be inherited from the prototype, use
for own key, value of object

+

+ To iterate a generator function, use from. + See Generator Functions.

The only low-level loop that CoffeeScript provides is the while loop. The main difference from JavaScript is that the while loop can be used @@ -864,8 +876,9 @@ Block constructed.

+ CoffeeScript functions also support - ES6 generator functions + ES2015 generator functions through the yield keyword. There's no function*(){} nonsense — a generator in CoffeeScript is simply a function that yields.

@@ -874,6 +887,11 @@ Block yield* is called yield from, and yield return may be used if you need to force a generator that doesn't yield.

+

+ + You can iterate over a generator function using for…from. +

+ <%= codeFor('generator_iteration', 'getFibonacciNumbers(10)') %>

@@ -975,6 +993,7 @@ Block

<%= codeFor('modules') %>

+ Note that the CoffeeScript compiler does not resolve modules; writing an import or export statement in CoffeeScript will produce an import or export statement in the resulting output. @@ -1393,7 +1412,7 @@ six = -> <%= releaseHeader('2015-09-03', '1.10.0', '1.9.3') %>