<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>le accidental occurrence &#187; underscore</title>
	<atom:link href="http://blog.42at.com/tag/underscore/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.42at.com</link>
	<description>42at</description>
	<lastBuildDate>Thu, 09 Jun 2011 07:00:30 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>_xiterator: an expression iterator for Underscore</title>
		<link>http://blog.42at.com/_xiterator-an-expression-iterator-for-underscore</link>
		<comments>http://blog.42at.com/_xiterator-an-expression-iterator-for-underscore#comments</comments>
		<pubDate>Thu, 15 Apr 2010 20:41:47 +0000</pubDate>
		<dc:creator>Moos</dc:creator>
				<category><![CDATA[blog]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[underscore]]></category>

		<guid isPermaLink="false">http://blog.42at.com/?p=186</guid>
		<description><![CDATA[Underscore.js is an excellent, compact Javascript  library extending the language with useful tools.  Most of the utils are drawn from Prototype and/or been inspired by languages such as Ruby. Underscore reverts to native code where it is supported. A hand-full of so-called Collection functions which operate on arrays &#38; objects, such as _any(), _all(), _map(), and [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://documentcloud.github.com/underscore/">Underscore.js</a> is an excellent, compact Javascript  library extending the language with useful tools.  Most of the utils are drawn from <a href="http://prototypejs.org">Prototype</a> and/or been inspired by languages such as Ruby. Underscore reverts to native  code where it is supported.</p>
<p>A hand-full of so-called Collection functions which operate on arrays &amp; objects, such as _any(), _all(), _map(), and _detect(), take an iterator function as an argument:</p>
<pre class="brush: jscript;">
if ( _.any([1,2,3], function iterator(value){
    return ! _.isNumber(value);
})) {
    // ...
}
</pre>
<p>_.isNumber() is one of a dozen or so type checking routines provided by Underscore, in addition to, _isString(), _isFunction(), _isArray(), and others.</p>
<p>Often times the iterator is a simple type-checking, like above, or some sort of expression that needs to be evaluated.  So why not just enter the expression:</p>
<pre class="brush: jscript;">if (_.any([1,2,3], &quot;!isNumber&quot;)) {
 // ...
}</pre>
<p>This is what <strong>_xiterator</strong>, an add-on for Underscore, provides.  It  is simply the meat of the iterator in a compact semantic.</p>
<p>Let&#8217;s throw in some range checking:</p>
<pre class="brush: jscript;">
if (_.any([1,2,3], function iterator(value){
    return ! _.isNumber(value) || value &lt; 0 || value &gt; 10;
})){
    // ...
}
</pre>
<p>can be  replaced by:</p>
<pre class="brush: jscript;">
if (_.any([1,2,3], &quot;!isNumber || &lt; 0 || &gt; 10&quot;)){
    // ....
}
</pre>
<h2>Expression iterators</h2>
<p>The expression is composed of valid Javascript code.  In the case of relational operators (&lt;, &lt;=, ==, etc.) the left operand is <em>implied</em> to be the value of the iterator.  The expression is evaluated in global scope.</p>
<p>Any of the Underscore _.isXXX type-checking routines can be used without the argument (the _.  part is implied).  In addition, three new routines are added: <em>isBlank</em>, <em>isOdd</em>, <em>isEven</em>.</p>
<p>Parenthetical expressions are fully supported:</p>
<pre class="brush: jscript;"> _.any([1,2,3], &quot;(isNumber &amp;&amp; &gt; 0) || (isString &amp;&amp; !isBlank)&quot;);</pre>
<p>Validating functions:</p>
<pre class="brush: jscript;"> function checkZip(value) {...}

 _.any(zips, &quot;!isBlank &amp;&amp; checkZip(__value) &quot;);  // __value is the placeholder</pre>
<p>In addition to __value, __key and __list placeholder variables, corresponding to the formal arguments of the iterator, are available.</p>
<p>How about a regular expression:</p>
<pre class="brush: jscript;"> _.all([1,2,3], /\d+/);   // either as string or RegExp object</pre>
<p>Of course, functions are still supported and will work as before.</p>
<h3>Accessing original methods</h3>
<p>Since the expression string needs to be parsed, runtime performance will be affected.  Performance sensitive applications involving huge sets should use the original routine.</p>
<p>The original routines  can be explicitly accessed by passing no argument, eg.</p>
<pre class="brush: jscript;">
var originalAny = _.any();
originalAny(veryLargeSet, function(){...});
</pre>
<p>Or more compactly,</p>
<pre class="brush: jscript;">_.any()(veryLargeSet, function(){...});
</pre>
<p>Expressions cannot be used on original methods:</p>
<pre class="brush: jscript;">
originalAny(veryLargeSet, 'isNumber'); //  =&gt; raises exception
</pre>
<h3>The code</h3>
<p>Details, code, and examples (including <a href="http://moos.github.com/_xiterator">test suite</a>) are on <a href="http://github.com/moos/_xiterator">Github</a>.   Released under MIT license.</p>
<p><strong>Update: </strong>Just came across <a href="http://osteele.com/sources/javascript/functional/">Functional.js</a>.  Above technique is not too dissimilar to its string `lambda` functionality.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.42at.com/_xiterator-an-expression-iterator-for-underscore/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

