mirror of
https://github.com/jashkenas/coffeescript.git
synced 2022-11-09 12:23:24 -05:00
docs for range comprehensiosn
This commit is contained in:
parent
eeef8d3612
commit
983dac84a2
4 changed files with 13 additions and 21 deletions
|
@ -2,7 +2,4 @@
|
|||
lunch: food.eat() for food in ['toast', 'cheese', 'wine'].
|
||||
|
||||
# Zebra-stripe a table.
|
||||
highlight(row) for row, i in table if i % 2 is 0.
|
||||
|
||||
# Check the first one hundred combinations.
|
||||
lockpick(combinations[i]) for i in [1..100].
|
||||
highlight(row) for row, i in table if i % 2 is 0.
|
|
@ -357,6 +357,11 @@ coffee --print app/scripts/*.coffee > concatenation.js</pre>
|
|||
would use a loop, <b>each</b>/<b>forEach</b>, <b>map</b>, or <b>select</b>/<b>filter</b>.
|
||||
</p>
|
||||
<%= code_for('array_comprehensions') %>
|
||||
<p>
|
||||
If you're not iterating over an actual array, you can use a range to
|
||||
specify the start and end of an array comprehension:
|
||||
<tt>coundown(i) for i in [10..1].</tt>
|
||||
</p>
|
||||
|
||||
<p id="slice">
|
||||
<b class="header">Slicing Arrays with Ranges</b>
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
(function(){
|
||||
var __a, __b, __c, __d, __e, __f, __g, __h, __i, __j, __k, __l, food, i, lunch, row;
|
||||
var __a, __b, __c, __d, __e, __f, __g, __h, food, i, lunch, row;
|
||||
// Eat lunch.
|
||||
__a = ['toast', 'cheese', 'wine'];
|
||||
__d = [];
|
||||
|
@ -17,10 +17,4 @@
|
|||
__h[__f] = i % 2 === 0 ? highlight(row) : null;
|
||||
}
|
||||
__h;
|
||||
// Check the first one hundred combinations.
|
||||
__k = [];
|
||||
for (__l=0, i=1, __j=100; i<=__j; i++, __l++) {
|
||||
__k[__l] = lockpick(combinations[i]);
|
||||
}
|
||||
__k;
|
||||
})();
|
16
index.html
16
index.html
|
@ -581,10 +581,7 @@ lunch<span class="Keyword">:</span> food.eat() <span class="Keyword">for</span>
|
|||
|
||||
<span class="Comment"><span class="Comment">#</span> Zebra-stripe a table.</span>
|
||||
highlight(row) <span class="Keyword">for</span> row, i <span class="Keyword">in</span> table <span class="Keyword">if</span> i <span class="Keyword">%</span> <span class="Number">2</span> <span class="Keyword">is</span> <span class="Number">0</span>.
|
||||
|
||||
<span class="Comment"><span class="Comment">#</span> Check the first one hundred combinations.</span>
|
||||
lockpick(combinations[i]) <span class="Keyword">for</span> i <span class="Keyword">in</span> [<span class="Number">1</span>..<span class="Number">100</span>].
|
||||
</pre><pre class="idle"><span class="Storage">var</span> __a, __b, __c, __d, __e, __f, __g, __h, __i, __j, __k, __l, food, i, lunch, row;
|
||||
</pre><pre class="idle"><span class="Storage">var</span> __a, __b, __c, __d, __e, __f, __g, __h, food, i, lunch, row;
|
||||
<span class="Comment"><span class="Comment">//</span> Eat lunch.</span>
|
||||
__a <span class="Keyword">=</span> [<span class="String"><span class="String">'</span>toast<span class="String">'</span></span>, <span class="String"><span class="String">'</span>cheese<span class="String">'</span></span>, <span class="String"><span class="String">'</span>wine<span class="String">'</span></span>];
|
||||
__d <span class="Keyword">=</span> [];
|
||||
|
@ -602,13 +599,12 @@ __h <span class="Keyword">=</span> [];
|
|||
__h[__f] <span class="Keyword">=</span> i <span class="Keyword">%</span> <span class="Number">2</span> <span class="Keyword">===</span> <span class="Number">0</span> ? highlight(row) : <span class="BuiltInConstant">null</span>;
|
||||
}
|
||||
__h;
|
||||
<span class="Comment"><span class="Comment">//</span> Check the first one hundred combinations.</span>
|
||||
__k <span class="Keyword">=</span> [];
|
||||
<span class="Keyword">for</span> (__l<span class="Keyword">=</span><span class="Number">0</span>, i<span class="Keyword">=</span><span class="Number">1</span>, __j<span class="Keyword">=</span><span class="Number">100</span>; i<span class="Keyword"><=</span>__j; i<span class="Keyword">++</span>, __l<span class="Keyword">++</span>) {
|
||||
__k[__l] <span class="Keyword">=</span> lockpick(combinations[i]);
|
||||
}
|
||||
__k;
|
||||
</pre><br class='clear' /></div>
|
||||
<p>
|
||||
If you're not iterating over an actual array, you can use a range to
|
||||
specify the start and end of an array comprehension:
|
||||
<tt>coundown(i) for i in [10..1].</tt>
|
||||
</p>
|
||||
|
||||
<p id="slice">
|
||||
<b class="header">Slicing Arrays with Ranges</b>
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue