Skip to content

Commit

Permalink
added documentation for _.times
Browse files Browse the repository at this point in the history
  • Loading branch information
jashkenas committed Feb 24, 2010
1 parent 65d2c30 commit c23b2ce
Showing 1 changed file with 60 additions and 49 deletions.
109 changes: 60 additions & 49 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -121,54 +121,9 @@ <h2>Downloads <i style="padding-left: 12px; font-size:12px;">(Right-click, and u
</table>
</p>

<h2 id="styles">Object-Oriented and Functional Styles</h2>

<p>
You can use Underscore in either an object-oriented or a functional style,
depending on your preference. The following two lines of code are
identical ways to double a list of numbers.
</p>

<pre>
_.map([1, 2, 3], function(n){ return n * 2; });
_([1, 2, 3]).map(function(n){ return n * 2; });</pre>

<p>
Using the object-oriented style allows you to chain together methods. Calling
<tt>chain</tt> on a wrapped object will cause all future method calls to
return wrapped objects as well. When you've finished the computation,
use <tt>value</tt> to retrieve the final value. Here's an example of chaining
together a <b>map/flatten/reduce</b>, in order to get the word count of
every word in a song.
</p>

<pre>
var lyrics = [
{line : 1, words : "I'm a lumberjack and I'm okay"},
{line : 2, words : "I sleep all night and I work all day"},
{line : 3, words : "He's a lumberjack and he's okay"},
{line : 4, words : "He sleeps all night and he works all day"}
];

_(lyrics).chain()
.map(function(line) { return line.words.split(' '); })
.flatten()
.reduce({}, function(counts, word) {
counts[word] = (counts[word] || 0) + 1;
return counts;
}).value();

=&gt; {lumberjack : 2, all : 4, night : 2 ... }</pre>

<p>
In addition, the
<a href="https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Objects/Array">Array prototype's methods</a>
are proxied through the chained Underscore object, so you can slip a
<tt>reverse</tt> or a <tt>push</tt> into your chain, and continue to
modify the array.
</p>

<h2>Table of Contents</h2>

<a href="#styles">Object-Oriented and Functional Styles</a>

<p>
<b>Collections</b>
Expand Down Expand Up @@ -217,8 +172,9 @@ <h2>Table of Contents</h2>
<b>Utility</b>
<br />
<span class="methods"><a href="#noConflict">noConflict</a>,
<a href="#identity">identity</a>, <a href="#breakLoop">breakLoop</a></span>,
<a href="#uniqueId">uniqueId</a>, <a href="#template">template</a></span>
<a href="#identity">identity</a>, <a href="#times">times</a>,
<a href="#breakLoop">breakLoop</a></span>, <a href="#uniqueId">uniqueId</a>,
<a href="#template">template</a></span>
</p>

<p>
Expand All @@ -228,6 +184,53 @@ <h2>Table of Contents</h2>
</p>

<div id="documentation">

<h2 id="styles">Object-Oriented and Functional Styles</h2>

<p>
You can use Underscore in either an object-oriented or a functional style,
depending on your preference. The following two lines of code are
identical ways to double a list of numbers.
</p>

<pre>
_.map([1, 2, 3], function(n){ return n * 2; });
_([1, 2, 3]).map(function(n){ return n * 2; });</pre>

<p>
Using the object-oriented style allows you to chain together methods. Calling
<tt>chain</tt> on a wrapped object will cause all future method calls to
return wrapped objects as well. When you've finished the computation,
use <tt>value</tt> to retrieve the final value. Here's an example of chaining
together a <b>map/flatten/reduce</b>, in order to get the word count of
every word in a song.
</p>

<pre>
var lyrics = [
{line : 1, words : "I'm a lumberjack and I'm okay"},
{line : 2, words : "I sleep all night and I work all day"},
{line : 3, words : "He's a lumberjack and he's okay"},
{line : 4, words : "He sleeps all night and he works all day"}
];

_(lyrics).chain()
.map(function(line) { return line.words.split(' '); })
.flatten()
.reduce({}, function(counts, word) {
counts[word] = (counts[word] || 0) + 1;
return counts;
}).value();

=&gt; {lumberjack : 2, all : 4, night : 2 ... }</pre>

<p>
In addition, the
<a href="https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Objects/Array">Array prototype's methods</a>
are proxied through the chained Underscore object, so you can slip a
<tt>reverse</tt> or a <tt>push</tt> into your chain, and continue to
modify the array.
</p>

<h2>Collection Functions (Arrays or Objects)</h2>

Expand Down Expand Up @@ -955,6 +958,14 @@ <h2>Utility Functions</h2>
moe === _.identity(moe);
=&gt; true</pre>

<p id="times">
<b class="header">times</b><code>_.times(n, iterator)</code>
<br />
Invokes the given iterator function <b>n</b> times.
</p>
<pre>
_(3).times(function(){ genie.grantWish(); });</pre>

<p id="breakLoop">
<b class="header">breakLoop</b><code>_.breakLoop()</code>
<br />
Expand Down

0 comments on commit c23b2ce

Please sign in to comment.