<?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>Oliver Steele &#187; Projects</title>
	<atom:link href="http://osteele.com/archives/category/projects/feed" rel="self" type="application/rss+xml" />
	<link>http://osteele.com</link>
	<description>Languages of the real and artificial.</description>
	<lastBuildDate>Wed, 28 Oct 2009 22:31:37 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>What I didn’t get to</title>
		<link>http://osteele.com/archives/2007/12/what-i-didnt-get-to</link>
		<comments>http://osteele.com/archives/2007/12/what-i-didnt-get-to#comments</comments>
		<pubDate>Tue, 01 Jan 2008 04:25:23 +0000</pubDate>
		<dc:creator>Oliver</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Libraries]]></category>
		<category><![CDATA[Open Source]]></category>
		<category><![CDATA[OpenLaszlo]]></category>
		<category><![CDATA[Projects]]></category>

		<guid isPermaLink="false">http://osteele.com/2008/01/01/what-i-didnt-get-to</guid>
		<description><![CDATA[Here are some of the weekend projects that I didn't finish this year.  These aren't good enough to put on my project list or my "sources page":http://osteele.com/sources/.  Some of these aren't even working, and some of them I might not finish at all (most of my weekends are spoken for).  And some of them I can't bear to look at (I'm not proud of the code, and don't want to be judged by it...), but I'm making myself put them out there anyway.  I feel bad for the neglected little things, trapped on my hard drive, and I'd like to let them see the sun, even if just briefly before they flicker out and die.]]></description>
			<content:encoded><![CDATA[<p>Here are some of the weekend projects that I didn&#8217;t finish this year.  These aren&#8217;t good enough to put on my project list or my <a href="http://osteele.com/sources/">sources page</a>.  Some of these aren&#8217;t even working, and some of them I might not finish at all (most of my weekends are spoken for).  And some of them I can&#8217;t bear to look at (I&#8217;m not proud of the code, and don&#8217;t want to be judged by it&#8230;), but I&#8217;m making myself put them out there anyway.  I feel bad for the neglected little things, trapped on my hard drive, and I&#8217;d like to let them see the sun, even if just briefly before they flicker out and die.</p>

<p>Libraries:<br />
* <a href="http://osteele.com/sources/openlaszlo/lzosutils">LzOSUtils</a> &#8212; jQuery-compatible <code>ajax</code> function, Flash-&gt;JS bridge with callbacks, declarative Flash 8 filter effects, console that reports to Firebug, dashed lines, Prototype-compatible string and collection methods, etc.  Completely undocumented and fairly disorganized.<br />
* <a href="http://osteele.com/sources/openlaszlo/lztestkit">LzTestKit</a> &#8212; mocks and asynchronous and automated testing for OpenLaszlo; still pretty raw.<br />
* <a href="http://osteele.com/sources/javascript/hopkit">HopKit</a> &#8212; a &#8220;higher order programming kit&#8221;, for constructed chained <span class="caps">API</span>s such as in LzTestKit&#8217;s mocks and expectations; still somewhat buggy and undocumented.<br />
* <a href="http://osteele.com/sources/javascript/concurrent">MVars</a> &#8212; a port of Haskell <a href="http://www.haskell.org/ghc/docs/latest/html/libraries/base/Control-Concurrent-MVar.html">MVar</a>&#8217;s to JavaScript.  I realized that what I actually needed for real-world applications was an implementation of the <a href="http://en.wikipedia.org/wiki/Join_calculus">join calculus</a> (a la <a href="http://jocaml.inria.fr/">JoCaml</a>).  I haven&#8217;t written the join calculus version.<br />
* <a href="http://osteele.com/sources/javascript/protodoc">Protodoc</a> &#8212; I wrote this to extract the docs from the source files and to implement the live examples in <a href="http://osteele.com/sources/javascript/functional">Functional</a> and <a href="http://osteele.com/sources/javascript/sequentially">Sequentially</a>; it includes a version of the wonderful <a href="http://docs.python.org/lib/module-doctest.html">doctest</a>, for JavaScript.  I got part way through refactoring it into something that isn&#8217;t quite put together again.  I haven&#8217;t decided whether to finish it or whether there&#8217;s an existing project that&#8217;s close enough.<br />
* [No link yet] Updates to <a href="http://osteele.com/sources/openlaszlo/json/">OpenLaszlo <span class="caps">JSON</span></a> and <a href="http://ropenlaszlo.rubyforge.org/">ropenlaszlo</a>, from using them over the past year.  (No, those links go to the old versions &#8212; I haven&#8217;t uploaded the updates <img src='http://osteele.com/wp/wp-includes/images/smilies/icon_sad.gif' alt=':-(' class='wp-smiley' /> <br />
* Implementations in <a href="http://osteele.com/sources/ruby/cfdg.rb">Ruby</a> and <a href="http://osteele.com/sources/javascript/cfdg">JavaScript</a>  of the awesome <a href="http://www.chriscoyne.com/cfdg/"><span class="caps">CFDG</span></a>.  I wrote these a couple of years ago, and really want to update and clean them up to point where I can donate them to <a href="http://hacketyhack.net/">Hackety Hack</a>, or someone else who might use them.</p>

<p>Applets:<br />
* <a href="http://osteele.com/applets/tiles.html">Tiles</a> &#8212; <span class="caps">HTML </span>port of my mid-nineties <a href="http://osteele.com/applets/java-tiles.html">Java version</a> (which was a port of my mid-eighties C version), uses the Canvas tag; probably doesn&#8217;t work in <span class="caps">MSIE</span><br />
* <a href="http://osteele.com/applets/ifs.html"><span class="caps">IFS</span></a> &#8212; a few minutes of pair programming with my son to show him some stuff about matrices; probably doesn&#8217;t work in <span class="caps">MSIE</span><br />
* <a href="http://osteele.com/applets/onthisday/">On this day</a> &#8212; iPhone applet; the feed is down right now<br />
* Force-directed layout for my home page &#8212; this was my experiment using <span class="caps">HTML </span>instead of Flash; I got discouraged when I saw how bad the frame rate for full-page animation is was even in Firefox, let alone <span class="caps">MSIE </span>(Safari rocks now, though!)</p>

<p>Plus a couple dozen essays that are half or three-quarters written (programming, software development, math education), a couple of <span class="caps">AJAX </span>presentations, and two workbooks for teaching abstract math at the elementary level.  It takes me longer to write an essay than a program, though!</p>

<p>More weekends, please.</p>]]></content:encoded>
			<wfw:commentRss>http://osteele.com/archives/2007/12/what-i-didnt-get-to/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Sequentially: Temporal and Frequency Adverbs for JavaScript</title>
		<link>http://osteele.com/archives/2007/11/sequentially</link>
		<comments>http://osteele.com/archives/2007/11/sequentially#comments</comments>
		<pubDate>Sat, 24 Nov 2007 18:06:06 +0000</pubDate>
		<dc:creator>Oliver</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Libraries]]></category>
		<category><![CDATA[Projects]]></category>

		<guid isPermaLink="false">http://osteele.com/2007/11/24/sequentially-temporal-and-frequency-adverbs-for-javascript</guid>
		<description><![CDATA[<dfn><a href="/sources/javascript/sequentially">Sequentially</a></dfn> is a JavaScript library for asychronous programming.  It makes it easy to define functions that are called later, or periodically, or that can be called only a certain number of times, or only at a certain frequency.

[code language="javascript"]
// Call a function f five times in a row
f.only(5).repeatedly()
// Call f five times, at one second intervals
f.only(5).periodically()
// Make a new function g that calls through to f at most five times,
// no matter how often g is called
var g = f.only(5)
// Make a new function g that calls f at most once per minute,]]></description>
			<content:encoded><![CDATA[<p><dfn><a href="/sources/javascript/sequentially">Sequentially</a></dfn> is a JavaScript library for asychronous programming.  It makes it easy to define functions that are called later, or periodically, or that can be called only a certain number of times, or only at a certain frequency.</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// Call a function f five times in a row</span>
f.<span style="color: #660066;">only</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">5</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">repeatedly</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #006600; font-style: italic;">// Call f five times, at one second intervals</span>
f.<span style="color: #660066;">only</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">5</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">periodically</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #006600; font-style: italic;">// Make a new function g that calls through to f at most five times,</span>
<span style="color: #006600; font-style: italic;">// no matter how often g is called</span>
<span style="color: #003366; font-weight: bold;">var</span> g <span style="color: #339933;">=</span> f.<span style="color: #660066;">only</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">5</span><span style="color: #009900;">&#41;</span>
<span style="color: #006600; font-style: italic;">// Make a new function g that calls f at most once per minute,</span>
<span style="color: #006600; font-style: italic;">// no matter how frequently g is called</span>
<span style="color: #003366; font-weight: bold;">var</span> g <span style="color: #339933;">=</span> f.<span style="color: #660066;">infrequently</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">60</span><span style="color: #339933;">*</span><span style="color: #CC0000;">1000</span><span style="color: #009900;">&#41;</span>
<span style="color: #006600; font-style: italic;">// Apply a function to each of the elements of an array, at intervals</span>
<span style="color: #006600; font-style: italic;">// of once per second</span>
<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'here'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'are'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'some'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'elements'</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">sequentially</span><span style="color: #009900;">&#40;</span>
  <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>word<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>console.<span style="color: #660066;">info</span><span style="color: #009900;">&#40;</span>word<span style="color: #339933;">,</span> <span style="color: #3366CC;">'-&gt;'</span><span style="color: #339933;">,</span> word.<span style="color: #660066;">length</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
  .<span style="color: #660066;">periodically</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span></pre></div></div>




<p>You can run these examples in your browser (Safari and Firefox only, for now) on this <a href="/sources/javascript/sequentially">page of examples</a>.  Mouse over the source code to see which outputs come from each statement.  Mouse over or click on the outputs to see which statement generates each output.</p>

<p>This is an early version.  Some aspects aren&#8217;t well thought-out; some terminology isn&#8217;t consistent.  Nonetheless, some early readers have urged me to put this out.</p>

<h3>Why?</h3>

<p>Recently I wrote an browser application that did the following:</p>

<p>Ask the content server for an image.  If it&#8217;s not there, ask the application server to queue a request to the image server to create it.  Then check back with the content server again.  If the asset doesn&#8217;t show up after a while, the application server may have been down or overloaded, so ask it again.  But I don&#8217;t want the client applications to mount a DDoS attack on an ailing server, so back off the frequency of the requests, and then give up, after a while.</p>

<p>Why?  I&#8217;d like to be able to run client applications that present data from a cluster of unreliable commodity hardware (the same as Erlang; the same as <a href="http://couchdb.org">CouchDB</a>).   This means these clients must survive component-wise server failure: they should implement retries (when a server is temporarily overloaded), then transition to failover (when it&#8217;s out for the count).</p>

<p>My first pass at this was a tangled mess of domain logic, network requests, and control code.  It was way more complex than it ought to have been, especially for such a general design pattern.</p>

<p>The basic concepts here are simple: repetition (&#8221;keep asking, but not too many times&#8230;&#8221;) and frequency (&#8221;&#8230;and not too frequently&#8221;).  Simple concepts should be simply spelled.</p>

<p>You can think of Sequentially as a tiny little domain-specific extension to JavaScript, that defines words for these concepts.</p>

<h3>Some Analogies</h3>

<p>I use this in a style I call &#8220;adverbial programming&#8221;.  Another example of adverbial programming would be some uses of <span class="caps">AOP.</span></p>

<p>Someday I&#8217;ll post an entry about the analogy between computer languages and natural languages.  For now, simply note that methods such as &#8220;only&#8221; and &#8220;infrequently&#8221; modify a function (a verb) to produce a new function with a related meaning &#8212; this is the same as (one of the senses of) an adverb.</p>

<p>This is in contrast to procedural programming, which assembles statements into paragraphs with aggregate effect; object-oriented (OO) programming, which assembles noun phrases; and functional programming, which is largely about verbs<sup class="footnote"><a href="#fn1">1</a></sup>.  (Closures, which bridge the gap between the functional and OO style, are gerunds.)</p>

<p>Here are some other analogies for thinking about thinking about this.  This is all kind of notional, but I found these useful in suggesting how this relates to other work, and where to take it next.</p>

<p>You could think of Sequentially as doing something like memoization, where instead of just caching the result it modifies <em>when</em> and<em>whether</em> a function is called.  Alternatively (and very loosely), Sequentially is the categorical dual of <a href="http://en.wikipedia.org/wiki/Generator_%28computer_science%29">generators</a> (it builds sinks instead of sources), in a partially <span class="caps">CPS</span>-converted program. Or, if you took the call graph of a program, turned that into a dataflow diagram, and implemented a dataflow interpreter for that diagram, then Sequentially would override some of the pipes.  Or (again, loosely) it&#8217;s a kind of two-way dual of <a href="/sources/javascript/functional">Functional JavaScript</a> in <a href="http://chu.stanford.edu/">Chu space</a> &#8212; instead of <em>collecting</em> <em>values</em> (arguments) across <em>state space</em>, it <em>distributes</em> <em>function calls</em> across <em>time</em> (sequence).</p>

<p>Or maybe that&#8217;s all overkill, and it&#8217;s just a few combinators for frequency, iteration, and time.</p>

<p><hr/></p>

<p>[1] More accurately, functional programming is about saturating the argument positions of both nouns and verbs.  Its closest analogy is to <em>theories</em> of language, such as Montague Grammar and Categorial grammar, rather than to language <em>itself</em>.  This may be why functional programming is for many people less than intuitive.</p>]]></content:encoded>
			<wfw:commentRss>http://osteele.com/archives/2007/11/sequentially/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Functional Javascript 1.0.2</title>
		<link>http://osteele.com/archives/2007/11/functional-javascript-102</link>
		<comments>http://osteele.com/archives/2007/11/functional-javascript-102#comments</comments>
		<pubDate>Sun, 11 Nov 2007 20:34:11 +0000</pubDate>
		<dc:creator>Oliver</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Libraries]]></category>
		<category><![CDATA[Projects]]></category>

		<guid isPermaLink="false">http://osteele.com/2007/11/11/functional-javascript-102</guid>
		<description><![CDATA[Thanks to everyone who has commented or contributed, praised or pitched in --- I've released an update to <a href="/sources/javascript/functional">Functional Javascript</a>, with these changes:

h4. New features

- Rhino compatibility.  I think --- at least it loads now, and a couple of hand tests work; i have yet to port the testing tool.  (Credit: <a href="http://weblog.raganwald.com/">Reginald Braithwaite</a>)

h4. Optimizations

- More efficient Array.slice.  (Credit: <a href="http://dean.edwards.name/">Dean Edwards</a>)
- Memoize Function.lambda.  (Credit: <a href="http://code.google.com/u/henrah/">henrah</a>)]]></description>
			<content:encoded><![CDATA[<p>Thanks to everyone who has commented or contributed, praised or pitched in &#8212; I&#8217;ve released an update to <a href="/sources/javascript/functional">Functional Javascript</a>, with these changes:</p>

<h4>New features</h4>

<p>- Rhino compatibility.  I think &#8212; at least it loads now, and a couple of hand tests work; i have yet to port the testing tool.  (Credit: <a href="http://weblog.raganwald.com/">Reginald Braithwaite</a>)</p>

<h4>Optimizations</h4>

<p>- More efficient Array.slice.  (Credit: <a href="http://dean.edwards.name/">Dean Edwards</a>)<br />
- Memoize Function.lambda.  (Credit: <a href="http://code.google.com/u/henrah/">henrah</a>)</p>

<h4>Packaging changes</h4>

<p>- Added jsmin version.  With jsmin and gzip, the file is 2.5K.<br />
- Moved string lambdas to a separate file, <code>to-string.js</code>.  (Both files are included in the jsmin version.)<br />
- Reformatted for new version of the doc tool.</p>

<h4>Compatibility notes</h4>

<p>If you were including <code>functional.js</code> before, now you need to include both <code>functional.js</code> and <code>to-function.js</code> in order to get the string lambda conversion functions too.  Or you can include <code>functional.min.js</code>, which is smaller and includes them both.</p>

<p>The fact that functional.js itself no longer contains any regular expressions might make it usable in Flash.  I haven&#8217;t actually tried this, because the only Flash I use is OpenLaszlo, which is still at version 8 of the Flash file format (AVM2, no <span class="caps">JIT, </span><del>&lt;25% browser speed</del> <ins>method calls are 10% of Firefox 2 / Safari 3.0 speed</ins>).  I don&#8217;t dare program at too high a level in Flash 8 because of performance concerns.</p>

<h3>Meanwhile, over in Ruby land&#8230;</h3>

<p>I&#8217;ll also put in a plug here for Braithwaite&#8217;s <a href="http://weblog.raganwald.com/2007/10/stringtoproc.html">String#to_proc</a>, which is a port of string lambdas to Ruby:</p>


<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">&#40;</span>1..5<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">map</span> <span style="color:#006600; font-weight:bold;">&amp;</span><span style="color:#996600;">'*2'</span>
  <span style="color:#006600; font-weight:bold;">-&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">2</span>, <span style="color:#006666;">4</span>, <span style="color:#006666;">6</span>, <span style="color:#006666;">8</span>, <span style="color:#006666;">10</span><span style="color:#006600; font-weight:bold;">&#93;</span>
<span style="color:#006600; font-weight:bold;">&#40;</span>1..5<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">inject</span> <span style="color:#006600; font-weight:bold;">&amp;</span><span style="color:#996600;">'+'</span>
  <span style="color:#006600; font-weight:bold;">-&gt;</span> <span style="color:#006666;">15</span>
factorial = <span style="color:#996600;">&quot;(1.._).inject &amp;'*'&quot;</span>.<span style="color:#9900CC;">to_proc</span>
factorial.<span style="color:#9900CC;">call</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">5</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#006600; font-weight:bold;">-&gt;</span> <span style="color:#006666;">120</span></pre></div></div>




<p>I&#8217;ve been a Raganwald fan for a while; and Ruby is my favorite server-side glue language, I look forward to using it there&#8230;</p>]]></content:encoded>
			<wfw:commentRss>http://osteele.com/archives/2007/11/functional-javascript-102/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Functional JavaScript</title>
		<link>http://osteele.com/archives/2007/07/functional-javascript</link>
		<comments>http://osteele.com/archives/2007/07/functional-javascript#comments</comments>
		<pubDate>Sun, 22 Jul 2007 00:54:57 +0000</pubDate>
		<dc:creator>Oliver</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Libraries]]></category>
		<category><![CDATA[Projects]]></category>

		<guid isPermaLink="false">http://osteele.com/2007/07/21/functional-javascript</guid>
		<description><![CDATA[*Functional* is a JavaScript library for "functional programming":http://en.wikipedia.org/wiki/Functional_programming.  It defines the standard higher-order functions (<code>map</code>, <code>reduce</code>, <code>filter</code>) that you can read about elsewhere on the web.  It also defines functions for partial function application and <a href="http://en.wikipedia.org/wiki/Function-level_programming">function-<em>level</em></a> programming: <code>curry</code>, <code>partial</code>, <code>compose</code>, <code>guard</code>, and <code>until</code>.  Finally, it introduces "string lambdas", which let you write <code>'x -> x+1'</code>, <code>'x+1'</code>, or even <code>'+1'</code> as synonyms for <code>function(x) {return x+1}</code>.]]></description>
			<content:encoded><![CDATA[<p><strong>Functional</strong> is a JavaScript library for <a href="http://en.wikipedia.org/wiki/Functional_programming">functional programming</a>.  It defines the standard higher-order functions (<code>map</code>, <code>reduce</code>, <code>filter</code>) that you can read about elsewhere on the web.  It also defines functions for partial function application and <a href="http://en.wikipedia.org/wiki/Function-level_programming">function-<em>level</em></a> programming: <code>curry</code>, <code>partial</code>, <code>compose</code>, <code>guard</code>, and <code>until</code>.  Finally, it introduces &#8220;string lambdas&#8221;, which let you write <code>'x -&gt; x+1'</code>, <code>'x+1'</code>, or even <code>'+1'</code> as synonyms for <code>function(x) {return x+1}</code>.</p>

<p>See the <a href="/sources/javascript/functional"><span class="caps">API </span>and examples page</a> for more examples, <span class="caps">API </span>documentation, and a link to the source.</p>

<h4>String lambdas</h4>

<p>Welcome to functional programming!  You&#8217;ve probably already discovered <code>map</code> and <code>filter</code>.  (If not, curl up with Google for a few minutes.  I&#8217;ll wait.)  Try using them in JavaScript.  Isn&#8217;t it a pain?:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">map<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>x<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #000066; font-weight: bold;">return</span> x<span style="color: #339933;">+</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">2</span><span style="color: #339933;">,</span><span style="color: #CC0000;">3</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #339933;">-&gt;</span> <span style="color: #009900;">&#91;</span><span style="color: #CC0000;">2</span><span style="color: #339933;">,</span><span style="color: #CC0000;">3</span><span style="color: #339933;">,</span><span style="color: #CC0000;">4</span><span style="color: #009900;">&#93;</span>
filter<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>x<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #000066; font-weight: bold;">return</span> x<span style="color: #339933;">&gt;</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">2</span><span style="color: #339933;">,</span><span style="color: #CC0000;">3</span><span style="color: #339933;">,</span><span style="color: #CC0000;">4</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span>
  <span style="color: #339933;">-&gt;</span> <span style="color: #009900;">&#91;</span><span style="color: #CC0000;">3</span><span style="color: #339933;">,</span><span style="color: #CC0000;">4</span><span style="color: #009900;">&#93;</span>
some<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>w<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #000066; font-weight: bold;">return</span> w.<span style="color: #660066;">length</span> <span style="color: #339933;">&lt;</span> <span style="color: #CC0000;">3</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'are there any short words?'</span>.<span style="color: #660066;">split</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">' '</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #339933;">-&gt;</span>false</pre></div></div>




<p>String lambdas let you write these this way, instead:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">map<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'x+1'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">2</span><span style="color: #339933;">,</span><span style="color: #CC0000;">3</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
select<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'x&gt;2'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">2</span><span style="color: #339933;">,</span><span style="color: #CC0000;">3</span><span style="color: #339933;">,</span><span style="color: #CC0000;">4</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
some<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'_.length &lt; 3'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'are there any short words?'</span>.<span style="color: #660066;">split</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">' '</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span></pre></div></div>




<p>Some more examples, using just <code>map</code>, <code>filter</code>, and <code>reduce</code>:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// double the items in a list:</span>
map<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'*2'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">2</span><span style="color: #339933;">,</span><span style="color: #CC0000;">3</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #339933;">-&gt;</span> <span style="color: #009900;">&#91;</span><span style="color: #CC0000;">2</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">4</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">6</span><span style="color: #009900;">&#93;</span>
<span style="color: #006600; font-style: italic;">// find just the odd numbers:</span>
filter<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'%2'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">2</span><span style="color: #339933;">,</span><span style="color: #CC0000;">3</span><span style="color: #339933;">,</span><span style="color: #CC0000;">4</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #339933;">-&gt;</span> <span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">3</span><span style="color: #009900;">&#93;</span>
<span style="color: #006600; font-style: italic;">// or just the evens:</span>
filter<span style="color: #009900;">&#40;</span>not<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'%2'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">2</span><span style="color: #339933;">,</span><span style="color: #CC0000;">3</span><span style="color: #339933;">,</span><span style="color: #CC0000;">4</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #339933;">-&gt;</span> <span style="color: #009900;">&#91;</span><span style="color: #CC0000;">2</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">4</span><span style="color: #009900;">&#93;</span>
<span style="color: #006600; font-style: italic;">// find the length of the longest word:</span>
reduce<span style="color: #009900;">&#40;</span>Math.<span style="color: #660066;">max</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> map<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'_.length'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'how long is the longest word?'</span>.<span style="color: #660066;">split</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">' '</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #339933;">-&gt;</span> <span style="color: #CC0000;">7</span>
<span style="color: #006600; font-style: italic;">// parse a binary array:</span>
reduce<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'2*x+y'</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #339933;">-&gt;</span> <span style="color: #CC0000;">10</span>
<span style="color: #006600; font-style: italic;">// parse a (non-negative) decimal string:</span>
reduce<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'x*10+y'</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> map<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.charCodeAt(0)-48'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'123'</span>.<span style="color: #660066;">split</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/(?=.)/</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #339933;">-&gt;</span> <span style="color: #CC0000;">123</span></pre></div></div>




<h4>Function-level programming</h4>

<p>Value-level programming manipulates values, transforming a sequence of inputs into an output.  <a href="http://en.wikipedia.org/wiki/Function-level_programming"> Function-level programming</a> manipulates functions, applying operations to functions to construct a new function.  It&#8217;s this new function that transforms inputs into outputs.</p>

<p>Here are some examples of function-level programming with <strong>Functional</strong>.  There&#8217;s more in the <a href="http://osteele.com/sources/javascript/functional">documentation</a>.</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// find the reciprocal only ofvalues that test true:</span>
map<span style="color: #009900;">&#40;</span>guard<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'1/'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">2</span><span style="color: #339933;">,</span><span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">,</span><span style="color: #CC0000;">4</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #339933;">-&gt;</span> <span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0.5</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0.25</span><span style="color: #009900;">&#93;</span>
<span style="color: #006600; font-style: italic;">// apply '10+' only to even values, leaving the odd ones alone:</span>
map<span style="color: #009900;">&#40;</span>guard<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'10+'</span><span style="color: #339933;">,</span> not<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'%2'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">2</span><span style="color: #339933;">,</span><span style="color: #CC0000;">3</span><span style="color: #339933;">,</span><span style="color: #CC0000;">4</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #339933;">-&gt;</span> <span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">12</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">3</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">14</span><span style="color: #009900;">&#93;</span>
<span style="color: #006600; font-style: italic;">// write a version of map that only applies to the evens:</span>
<span style="color: #003366; font-weight: bold;">var</span> even <span style="color: #339933;">=</span> not<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'%2'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> mapEvens <span style="color: #339933;">=</span> map.<span style="color: #660066;">prefilterAt</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> guard.<span style="color: #660066;">rcurry</span><span style="color: #009900;">&#40;</span>even<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
mapEvens<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'10+'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">2</span><span style="color: #339933;">,</span><span style="color: #CC0000;">3</span><span style="color: #339933;">,</span><span style="color: #CC0000;">4</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
<span style="color: #006600; font-style: italic;">// find the first power of two that's greater than 100:</span>
until<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&gt;100'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'2*'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span>
  <span style="color: #339933;">-&gt;</span> <span style="color: #CC0000;">128</span>
<span style="color: #006600; font-style: italic;">// or the first three-digit power of two (these are equivalent):</span>
until<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'String(_).length&gt;2'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'2*'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span>
until<span style="color: #009900;">&#40;</span>compose<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&gt;2'</span><span style="color: #339933;">,</span> pluck<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'length'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> String<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'2*'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span>
until<span style="color: #009900;">&#40;</span>sequence<span style="color: #009900;">&#40;</span>String<span style="color: #339933;">,</span> pluck<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'length'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'&gt;2'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'2*'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span></pre></div></div>




<h4>Partial function application</h4>

<p>Partial function application, or specialization, creates a new<br />
function out of an old one.  For example, given a division function:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> div<span style="color: #009900;">&#40;</span>a<span style="color: #339933;">,</span> b<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #000066; font-weight: bold;">return</span> a<span style="color: #339933;">/</span>b<span style="color: #009900;">&#125;</span></pre></div></div>




<p>a partial application of <code>div</code> is a new function that divides its argument by two:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> halve <span style="color: #339933;">=</span> div.<span style="color: #660066;">partial</span><span style="color: #009900;">&#40;</span>_<span style="color: #339933;">,</span> <span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>




<p>Partial application is especially useful as an argument to the higher-order functions such as <code>map</code>, where, given a function <code>div</code>, we can apply it (the first line below) without an explicit wrapper (the second).</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">map<span style="color: #009900;">&#40;</span>div.<span style="color: #660066;">partial</span><span style="color: #009900;">&#40;</span>_<span style="color: #339933;">,</span> <span style="color: #CC0000;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#91;</span><span style="color: #CC0000;">10</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">20</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">30</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
map<span style="color: #009900;">&#40;</span>lambda<span style="color: #009900;">&#40;</span>n<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #000066; font-weight: bold;">return</span> div<span style="color: #009900;">&#40;</span>n<span style="color: #339933;">,</span> <span style="color: #CC0000;">10</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#91;</span><span style="color: #CC0000;">10</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">20</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">30</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span></pre></div></div>




<p>The <code>curry</code> function handles a special case of partial function application, and the previous example could have been handled via <code>curry</code>.  Partial function application in all its generality is only necessary when you&#8217;re specializing not just on all the arguments on the left, or all the arguments on the right, but some distribution of arguments with holes in the middle.  To illustrate this requires a function with more than two parameters.</p>

<p>JavaScript doesn&#8217;t have many functions with more than two parameters. (<code>splice</code> takes three, but <code>splice</code> isn&#8217;t very functional).  Here&#8217;s a contrived example to start (and a real-world example next).</p>

<p>We&#8217;ll borrow one of the few trinary predicates from math: &#8220;between&#8221;.  <code>increasing(a, b, c)</code> tests whether <var>b</var> (the middle argument) lies in the open interval bounded by <var>a</var> and <var>c</var>.  Specialize the first and last arguments to produce a functions that tests whether a number is positive, for example.</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> increasing<span style="color: #009900;">&#40;</span>a<span style="color: #339933;">,</span> b<span style="color: #339933;">,</span> c<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">return</span> a <span style="color: #339933;">&lt;</span> b <span style="color: #339933;">&amp;&amp;</span> b <span style="color: #339933;">&lt;</span> c<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #003366; font-weight: bold;">var</span> positive <span style="color: #339933;">=</span> increasing.<span style="color: #660066;">partial</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> _<span style="color: #339933;">,</span> Infinity<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
map<span style="color: #009900;">&#40;</span>positive<span style="color: #339933;">,</span> <span style="color: #009900;">&#91;</span><span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #339933;">-&gt;</span> <span style="color: #009900;">&#91;</span><span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#93;</span>
<span style="color: #003366; font-weight: bold;">var</span> negative <span style="color: #339933;">=</span> increasing.<span style="color: #660066;">partial</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span>Infinity<span style="color: #339933;">,</span> _<span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
map<span style="color: #009900;">&#40;</span>negative<span style="color: #339933;">,</span> <span style="color: #009900;">&#91;</span><span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #339933;">-&gt;</span> <span style="color: #009900;">&#91;</span><span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#93;</span></pre></div></div>




<p>Here&#8217;s how to use <code>compose</code> and <code>curry</code> to generalize some of the examples from the first section into reusable functions.  (You&#8217;ll probaby like or hate these function definitions to the extent that you like or hate Haskell.)</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> longest <span style="color: #339933;">=</span> compose<span style="color: #009900;">&#40;</span>reduce.<span style="color: #660066;">curry</span><span style="color: #009900;">&#40;</span>Math.<span style="color: #660066;">max</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> map.<span style="color: #660066;">curry</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'_.length'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;_.split(' ')&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
longest<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;how long is the longest word?&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #339933;">-&gt;</span> <span style="color: #CC0000;">7</span>
longest<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;I bet floccinaucinihilipipification is longer.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #339933;">-&gt;</span> <span style="color: #CC0000;">29</span>
<span style="color: #003366; font-weight: bold;">var</span> parseUnsignedInt <span style="color: #339933;">=</span> compose<span style="color: #009900;">&#40;</span>reduce.<span style="color: #660066;">curry</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'x*10+y.charCodeAt(0)-48'</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'_.split(/(?=.)/)'</span><span style="color: #009900;">&#41;</span>
parseUnsignedInt<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'123'</span><span style="color: #009900;">&#41;</span>
  <span style="color: #339933;">-&gt;</span> <span style="color: #CC0000;">123</span></pre></div></div>




<p>Here&#8217;s real-world example:  The following line attaches a <code>sum</code> method to Array.  Note how the <code>'this'</code> string lambda, which is short for <code>function(){return this}</code>, moves the object from object position to argument position so that the curried <code>reduce</code> can apply to it.</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">Array.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">sum</span> <span style="color: #339933;">=</span> reduce.<span style="color: #660066;">curry</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'+'</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">compose</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'this'</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">2</span><span style="color: #339933;">,</span><span style="color: #CC0000;">3</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">sum</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
   <span style="color: #339933;">-&gt;</span> <span style="color: #CC0000;">6</span></pre></div></div>




<p>Here&#8217;s another example:  If you&#8217;re using Prototype, you can replace the first line below by the second:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">Event.<span style="color: #660066;">observe</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'myobj'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>...<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
onclick<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">''</span>myobj<span style="color: #3366CC;">', function() {...})</span></pre></div></div>




<p>by defining a specialized version of Event.observe:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> onclick <span style="color: #339933;">=</span> Event.<span style="color: #660066;">observe</span>.<span style="color: #660066;">bind</span><span style="color: #009900;">&#40;</span>Event<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">partial</span><span style="color: #009900;">&#40;</span>_<span style="color: #339933;">,</span> <span style="color: #3366CC;">'click'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>




<h4>A Question of Taste</h4>

<p>Is this:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> onclick <span style="color: #339933;">=</span> Event.<span style="color: #660066;">observe</span>.<span style="color: #660066;">bind</span><span style="color: #009900;">&#40;</span>Event<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">partial</span><span style="color: #009900;">&#40;</span>_<span style="color: #339933;">,</span> <span style="color: #3366CC;">'click'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>




<p>really better than the following?</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> onclick<span style="color: #009900;">&#40;</span>element<span style="color: #339933;">,</span> handler<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  Event.<span style="color: #660066;">observe</span><span style="color: #009900;">&#40;</span>elenent<span style="color: #339933;">,</span> <span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</span> handler<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>




<p>It&#8217;s a matter of taste, with some performance considerations as well.</p>

<p>The function-level version is less efficient.  To the inexperienced eye, it&#8217;s also harder to read.</p>

<p>On the other hand, the functional version doesn&#8217;t include as much plumbing, with its attendent opportunity for error.  The second definition  of <code>onclick</code>, considered as a general replacement for <code>Event.observer(..., 'click', ...)</code>, has two such errors.  One shows up as soon as you use it; the second is considerably more subtle.</p>

<p>Whether functional programming is appropriate, for reasons of efficiency or readability, in any particular instance, it&#8217;s nice to have it, at least for prototyping, in your arsenal.</p>

<h4>Performance Notes</h4>

<p>In most languages, including JavaScript, invoking a function is one of the slowest things you can do.  The implementations of languages designed for functional programming use a variety of techniques to optimize function calls.  JavaScript is not one of those languages.</p>

<p><strong>Functional</strong> attempts to reduce the cost of higher-order-programming where doing so doesn&#8217;t add to the code complexity or readability too much.  Each higher-order function and method is a small number of lines, and each function-returning method attempts to do as much work as possible outside the function that it returns, to optimize the case where the returned function is called more than once (as an argument to a higher-order function such as <code>map</code>, for example).</p>

<p>Still, using <strong>Functional</strong> is expensive.  Invoking a constructed function results in at least twice as many invocations as invoking the underlying function.  This isn&#8217;t any different from using <code>bind</code> in the Prototype library, say, but, the more of your program you write in a functional style &#8212; and therefore the more method calls you introduce &#8212; the slower it will be.  As with any library, be aware that you may have to hand-compile performance-critical sections of your code into an approximation of you would have written without the library anyway.  If you think you already know what needs to be optimized (or that your whole program does), or you aren&#8217;t comfortable with measuring performance periodically in order to intelligently trade execution time against implementation time, you may want to eschew libraries, especially higher-order ones.</p>


<h4>Compatibility</h4>

<p>Functional is known to work in Firefox 2.0, Safari 3.0, and <span class="caps">MSIE</span> 6.0.  I didn&#8217;t intentionally use any non-standard <span class="caps">ECMAS</span>cript constructs, but meta-programming such as this tends to turn up corners in the browser implementations.</p>

<p>I&#8217;ve used this with Prototype and jQuery.  If you call <code>Functional.install</code>, it will replace Prototype&#8217;s <code>bind</code>, but with a compatible version.  <code>Functional.install</code> defines a number of other top-level functions (all documented), but to my knowledge these all have unusual names (e.g. <code>curry</code>), or standard semantics (e.g. <code>map</code>), so you&#8217;re unlikely to run into any problems unless you try to use this with <em>another</em> library of higher-order functions.  In which case, don&#8217;t call <code>Functional.install</code>.</p>

<p><ins>Update: The current version also defines <code>equal</code> as a functional, for doing things like this: <code>select(equal(pluck('x'), K(1)), [{x:1,y:2}, {x:3,y:4}])</code>.  This is more likely to conflict with a method from your code or another library, so beware!</ins></p>

<p>Defining <code>String.prototype.apply</code> and <code>...call</code> seems potentially skanky, although the <span class="caps">ECMAS</span>cript standard permits it and i haven&#8217;t run into any trouble.  These methods could be removed without breaking anything except a few of the example; internally, the <strong>Functional</strong> functions use <code>Function.toFunction</code> instead.</p>

<p>The implementation of string lambdas uses regular expressions and <code>eval</code>.  The rest of the code doesn&#8217;t.  The intent of this separation is that the code might be portable to environments that don&#8217;t include these features, such as Flash and OpenLaszlo.  I haven&#8217;t tested it against any of these environments, thoug, so I&#8217;ve kept the code in one file for now.</p>

<h4>Future Directions</h4>

<h4>Credits</h4>

<p>(Temporarily deleted while I debug why this text borks WordPress.)</p>]]></content:encoded>
			<wfw:commentRss>http://osteele.com/archives/2007/07/functional-javascript/feed</wfw:commentRss>
		<slash:comments>20</slash:comments>
		</item>
		<item>
		<title>Wide URLs with WideURL.com</title>
		<link>http://osteele.com/archives/2006/04/wideurl</link>
		<comments>http://osteele.com/archives/2006/04/wideurl#comments</comments>
		<pubDate>Sat, 01 Apr 2006 14:36:50 +0000</pubDate>
		<dc:creator>Oliver</dc:creator>
				<category><![CDATA[Amusements]]></category>
		<category><![CDATA[Projects]]></category>

		<guid isPermaLink="false">http://osteele.com/2006/04/01/wide-urls-with-wideurlcom</guid>
		<description><![CDATA[For years now, I've been a great fan of "TinyURL.com":http://tinyurl.com.  That web site allows you to create a short representation of a longer URL, for use in email.

One of the problems with those URLs, though, and with URLs in general, is that they're misleadingly short.  A particular web page may have a lot of significance, but if it doesn't take up much of your message, there's just no way for the recipient to see this at a glance.

"WideURL.com":http://wideurl.com fixes this.  It creates an URL with more visual impact.

For example, here's the WideURL for this post: <tt><a href="http://wideurl.com/aitch-tee-tee-pea-colon-double-slash-oh-ess-tee-double-ee-ell-ee-dot-see-oh-em-slash-aye-are-see-aitch-eye-vee-ee-ess-slash-two-double-zero-six-slash-zero-four-slash-doubleyou-eye-dee-ee-you-are-ell">http://wideurl.com/aitch-tee-tee-pea-colon-double-slash-oh-ess-tee-double-ee-ell-ee-dot-see-oh-em-slash-aye-are-see-aitch-eye-vee-ee-ess-slash-two-double-zero-six-slash-zero-four-slash-doubleyou-eye-dee-ee-you-are-ell</a></tt>.]]></description>
			<content:encoded><![CDATA[<p>For years now, I&#8217;ve been a great fan of <a href="http://tinyurl.com">TinyURL.com</a>.  That web site allows you to create a short representation of a longer <span class="caps">URL, </span>for use in email.</p>

<p>One of the problems with those <span class="caps">URL</span>s, though, and with <span class="caps">URL</span>s in general, is that they&#8217;re misleadingly short.  A particular web page may have a lot of significance, but if it doesn&#8217;t take up much of your message, there&#8217;s just no way for the recipient to see this at a glance.</p>

<p><a href="http://wideurl.com">WideURL.com</a> fixes this.  It creates an <span class="caps">URL </span>with more visual impact.</p>

<p>For example, here&#8217;s the WideURL for this post: <tt><a href="http://wideurl.com/aitch-tee-tee-pea-colon-double-slash-oh-ess-tee-double-ee-ell-ee-dot-see-oh-em-slash-aye-are-see-aitch-eye-vee-ee-ess-slash-two-double-zero-six-slash-zero-four-slash-doubleyou-eye-dee-ee-you-are-ell">http://wideurl.com/aitch-tee-tee-pea-colon-double-slash-oh-ess-tee-double-ee-ell-ee-dot-see-oh-em-slash-aye-are-see-aitch-eye-vee-ee-ess-slash-two-double-zero-six-slash-zero-four-slash-doubleyou-eye-dee-ee-you-are-ell</a></tt>.  That&#8217;s much more significant-looking than simply <a href="http://osteele.com/archives/2006/04/wideurl"><tt>http://osteele.com/archives/2006/04/wideurl</tt></a>, I think you can agree.</p>]]></content:encoded>
			<wfw:commentRss>http://osteele.com/archives/2006/04/wideurl/feed</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>A Functional Diversion</title>
		<link>http://osteele.com/archives/2006/03/foldr</link>
		<comments>http://osteele.com/archives/2006/03/foldr#comments</comments>
		<pubDate>Fri, 24 Mar 2006 00:36:11 +0000</pubDate>
		<dc:creator>Oliver</dc:creator>
				<category><![CDATA[Amusements]]></category>
		<category><![CDATA[Projects]]></category>

		<guid isPermaLink="false">http://osteele.com/2006/03/23/a-functional-diversion</guid>
		<description><![CDATA[Even my friends who aren't into functional programming find something curously relaxing about "this":http://foldr.com.  (And the companion site "here":http://foldl.com.)

I bought foldr.com a year ago when I thought I might do something like Flickr for other types of information.  I didn't realize until last week what I was sitting on. :-)

<b>Update:</b> The use of the infinity symbol sparked a lively <a href="http://lambda-the-ultimate.org/node/1395">discussion on LtU<a>.]]></description>
			<content:encoded><![CDATA[<p>Even my friends who aren&#8217;t into functional programming find something curously relaxing about <a href="http://foldr.com">this</a>.  (And the companion site <a href="http://foldl.com">here</a>.)</p>

<p>I bought foldr.com a year ago when I thought I might do something like Flickr for other types of information.  I didn&#8217;t realize until last week what I was sitting on. <img src='http://osteele.com/wp/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>

<p><b>Update:</b> The use of the infinity symbol sparked a lively <a href="http://lambda-the-ultimate.org/node/1395">discussion on LtU<a>.</p>]]></content:encoded>
			<wfw:commentRss>http://osteele.com/archives/2006/03/foldr/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>JavaScript Gradient Roundrects</title>
		<link>http://osteele.com/archives/2006/03/javascript-gradient-roundrects</link>
		<comments>http://osteele.com/archives/2006/03/javascript-gradient-roundrects#comments</comments>
		<pubDate>Thu, 23 Mar 2006 17:52:35 +0000</pubDate>
		<dc:creator>Oliver</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Libraries]]></category>
		<category><![CDATA[Projects]]></category>

		<guid isPermaLink="false">http://osteele.com/2006/03/23/javascript-gradient-roundrects</guid>
		<description><![CDATA["JavaScript Gradient Roundrects":/sources/javascript/docs/gradients adds gradient roundrects to an HTML page, without images.  It uses the WHATWG canvas tag if it's available.  Otherwise it uses a stack of divs, whose heights are adaptively chosen according to the height of the graded element, the color components, and the radius curvature.  There's a demo <a href="/sources/javascript/demos/gradients.html">here</a>.

I also wrote a "JavaScript CSS parser":/sources/javascript/docs/divstyle that lets you attach gradients to an element without writing code.  You do this by including CSS inside a div tag whose class is 'style'.]]></description>
			<content:encoded><![CDATA[<p><a href="/sources/javascript/docs/gradients">JavaScript Gradient Roundrects</a> adds gradient roundrects to an <span class="caps">HTML </span>page, without images.  It uses the <span class="caps">WHATWG </span>canvas tag if it&#8217;s available.  Otherwise it uses a stack of divs, whose heights are adaptively chosen according to the height of the graded element, the color components, and the radius curvature.  There&#8217;s a demo <a href="/sources/javascript/demos/gradients.html">here</a>.</p>

<p>I also wrote a <a href="/sources/javascript/docs/divstyle">JavaScript <span class="caps">CSS </span>parser</a> that lets you attach gradients to an element without writing code.  You do this by including <span class="caps">CSS </span>inside a div tag whose class is &#8217;style&#8217;.  View the source of the <a href="/sources/javascript/demos/gradients.html">demo page</a> for an example.</p>]]></content:encoded>
			<wfw:commentRss>http://osteele.com/archives/2006/03/javascript-gradient-roundrects/feed</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Inline JavaScript Console</title>
		<link>http://osteele.com/archives/2006/03/inline-console</link>
		<comments>http://osteele.com/archives/2006/03/inline-console#comments</comments>
		<pubDate>Sat, 04 Mar 2006 04:10:37 +0000</pubDate>
		<dc:creator>Oliver</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Libraries]]></category>
		<category><![CDATA[Projects]]></category>

		<guid isPermaLink="false">http://osteele.com/2006/03/04/inline-javascript-console</guid>
		<description><![CDATA[Last week for the first time I did <a href="/tools/rework">some serious browser JavaScript programming</a>.  I put the following tools to good use, but ran against limits with each of them:
* "fvLogger":http://www.alistapart.com/articles/jslogging
 is terrific, but doesn't include an evaluator.  You have to reload your page each time you want to query a new value.
* "Rhino":http://www.mozilla.org/rhino/ is great for pure logic, but you can't use it with anything that use a browser API.  In fact, you can't use it with anything *that uses anything* that uses a browser API.  This means, for example, that you can't use it with a library that uses Prototype, without writing some mock objects first.]]></description>
			<content:encoded><![CDATA[<p>Last week for the first time I did <a href="/tools/rework">some serious browser JavaScript programming</a>.  I put the following tools to good use, but ran against limits with each of them:<br />
* <a href="http://www.alistapart.com/articles/jslogging">fvLogger</a><br />
 is terrific, but doesn&#8217;t include an evaluator.  You have to reload your page each time you want to query a new value.<br />
* <a href="http://www.mozilla.org/rhino/">Rhino</a> is great for pure logic, but you can&#8217;t use it with anything that use a browser <span class="caps">API. </span> In fact, you can&#8217;t use it with anything <strong>that uses anything</strong> that uses a browser <span class="caps">API. </span> This means, for example, that you can&#8217;t use it with a library that uses Prototype, without writing some mock objects first.<br />
* The <a href="http://www.squarefree.com/shell/">JavaScript Shell</a> is pretty amazing, but I wanted something a bit lighter weight (within the same window), and that works in Safari.</p>

<p>What I came up with is <a href="/sources/javascript/inline-console.js"><tt>inline-console.js</tt></a>.  This file adds an output console, and a text field with an &#8220;Eval&#8221; button, to the bottom of your application.  It also defines some logging functions &#8212; <tt>info</tt>, <tt>debug</tt>, <tt>warn</tt>, and <tt>error</tt> &#8212; that append text to the console.  (If you include <a href="http://www.alistapart.com/articles/jslogging">fvlogger</a>, it will use it instead.)</p>

<p>The point of this is to be as lightweight as possible.  Add<br />
script type=&#8221;text/javascript&#8221; src=&#8221;inline-console.js&#8221; (appropriately tagged) to the document head, and the script will take care of adding the <span class="caps">UI.</span></p>

<p>For more fun, include <a href="/archives/2006/03/readable-javascript-values">readable.js</a> after <tt>inline-console.js</tt>.  Then <code>{a: 1}</code> will print as <tt>{a: 1}</tt> instead of <code>[object Object]</code>.</p>

<p>Here&#8217;s an example.  Try entering some JavaScript expressions, such as <code>2*3</code>, <code>Math.sqrt(2)</code>, or <code>document.body</code>, and then pressing &#8220;Eval&#8221;.  (Click <a href="/sources/javascript/demos/inline-console.html" target="_blank">here</a> to open the example in a separate window, where you can view source.)</p>

<p><iframe src="/sources/javascript/demos/inline-console.html" width="300" height="300"></iframe></p>

<p>Files:<br />
* <a href="/sources/javascript/inline-console.js">inline-console.js</a> &#8212; adds the inline console<br />
* <a href="/sources/javascript/readable.js">readable.js</a> &#8212; adds readable representations for JavaScript objects (optional)<br />
* <a href="http://www.alistapart.com/articles/jslogging">fvlogger</a> &#8212; a nicer console UI with more control over which log levels are displayed (optional)</p>

<p>My other JavaScript libraries are <a href="/sources/javascript">here</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://osteele.com/archives/2006/03/inline-console/feed</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Readable JavaScript Values</title>
		<link>http://osteele.com/archives/2006/03/readable-javascript-values</link>
		<comments>http://osteele.com/archives/2006/03/readable-javascript-values#comments</comments>
		<pubDate>Fri, 03 Mar 2006 22:15:56 +0000</pubDate>
		<dc:creator>Oliver</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Libraries]]></category>
		<category><![CDATA[Projects]]></category>

		<guid isPermaLink="false">http://osteele.com/2006/03/03/readable-javascript-values</guid>
		<description><![CDATA[One problem with JavaScript development is that the string representation of a value doesn't tell you much about the value.  For example, <code>[null]</code>, <code>[undefined]</code>, and <code>''</code> all display as the empty string.  <code>[1,2}</code>, <code>[[1,2]]</code>, and <code>[[1],[2]]</code> all display as <code>1,2</code> (and so does <code>"1,2"</code>).  And <code>({a: 1})</code>, <code>({b: 2})</code>, and <code>new MySwankyNewObject()</code> all display as <code>[object Object]</code>.

If you "use an IDE":/archives/2004/11/ides for development, this may not be a problem.  Probably the IDE has its own string representation; even if it doesn't, you can generally drill into objects by clicking on them.  This doesn't help those us of who prefer REPL development or printf-style debugging.  When you display a debugging value (to the browser status line, to the <code>alert()</code> dialog, or to the Rhino console), you'd like some indication of what it actually <em>is</em>.  And JavaScript doesn't generally tell you, at least when the value is more complex than a string, number, or boolean.]]></description>
			<content:encoded><![CDATA[<p>One problem with JavaScript development is that the string representation of a value doesn&#8217;t tell you much about the value.  For example, <code>[null]</code>, <code>[undefined]</code>, and <code>''</code> all display as the empty string.  <code>[1,2}</code>, <code>[[1,2]]</code>, and <code>[[1],[2]]</code> all display as <code>1,2</code> (and so does <code>&quot;1,2&quot;</code>).  And <code>({a: 1})</code>, <code>({b: 2})</code>, and <code>new MySwankyNewObject()</code> all display as <code>[object Object]</code>.</p>

<p>If you <a href="/archives/2004/11/ides">use an <span class="caps">IDE</span></a> for development, this may not be a problem.  Probably the <span class="caps">IDE </span>has its own string representation; even if it doesn&#8217;t, you can generally drill into objects by clicking on them.  This doesn&#8217;t help those us of who prefer <span class="caps">REPL </span>development or printf-style debugging.  When you display a debugging value (to the browser status line, to the <code>alert()</code> dialog, or to the Rhino console), you&#8217;d like some indication of what it actually <em>is</em>.  And JavaScript doesn&#8217;t generally tell you, at least when the value is more complex than a string, number, or boolean.</p>

<p>Hence, <a href="/sources/javascript/docs/readable"><dfn>readable.js</dfn></a>.  <em>Readable</em> adds a <tt>Readable</tt> class that can stringify a JavaScript value readably, for debugging purposes.  <tt>Readable.toReadable([1,'', null, [3, 4]])</tt> evaluates to <tt>[1, '', null, [3, 4]]</tt>, not <tt>1,,,3,4</tt>.  And so on.</p>

<p>To make it easier to <strong>use</strong> the <tt>Readable</tt> class, <strong>Readable</strong> comes with a couple of hooks.  First of all, it defines defines <tt>info</tt>, <tt>warn</tt>, <tt>error</tt>, and <tt>debug</tt> functions<sup class="footnote"><a href="#fn1">1</a></sup> that display their arguments to the user.  In Rhino, these functions call through to <code>print</code>.  In a browser, they use <code>alert()</code> &#8212; unless <a href="http://www.alistapart.com/articles/jslogging">fvlogger</a> has been loaded first, in which case they use it instead[].  You can also replace <tt>Readable.log(level, message)</tt> or<br />
<tt>Readable.display(message)</tt> to add your own behavior; for example,<br />
to display the message in the status line, or <span class="caps">AJAX </span>it up to the server.</p>

<p>Secondly, Readable can add <tt>toString</tt> methods to <tt>Array.prototype</tt> and <tt>Object.prototype</tt>.  Do this, and evaluating an expression in Rhino writes a readable representation to the console, without your having to wrap it in <code>info(&lt;var&gt;...&lt;/var&gt;)</code> or <code>Readable.toString(&lt;var&gt;...&lt;/var&gt;)</code>.  Doing this has the consequence that iterating over the properties of an Object or Array will yield an extra one (<tt>toString</tt>), so this is off by default.  But define <span class="caps">READABLE</span>_REPLACE_TOSTRING <strong>before</strong> loading the file, or invoke <code>Readable.replaceToString()</code> <strong>after</strong> loading it, and you&#8217;ll get this behavior.</p>

<p>Files:<br />
* <a href="/sources/javascript/readable.js">readable.js</a><br />
* <a href="/sources/javascript/docs/readable">documentation</a></p>

<p><strong>Update</strong>: Fixed for Internet Explorer.</p>

<p><hr/></p>

<p class="footnote" id="fn1"><sup>1</sup> The reason there&#8217;s more than one function is that this is intended to be consistent with <a href="http://www.alistapart.com/articles/jslogging">fvlogger</a>.  It&#8217;s also handy to be able to search your sources for one logging function, and not the other.</p>

<p class="footnote" id="fn2"><sup>2</sup>  One advantage of including <strong>Readable</strong> even if you&#8217;re already using fvlogger is that now <code>info([1,2])</code> prints something different from <code>info([[1],[2]])</code>.  Another is that <strong>Readable</strong> extends the fvlogger functions with variadicity: <code>info(key, '-&gt;', value)</code> works now.  (Without <strong>Readable</strong>, it&#8217;s equivalent to <code>info(key)</code>, except that <code>value</code> is also evaluated for effect.)  Finally, you can use <strong>Readable</strong> to extend Rhino with the same logging <span class="caps">API. </span> I use this to write modules &#8212; such as <a href="/archives/2005/02/javascript-beziers">paths and beziers</a> &#8212; that I test with Rhino and integrate into a UI in the browser.</p>]]></content:encoded>
			<wfw:commentRss>http://osteele.com/archives/2006/03/readable-javascript-values/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Canvas with Text</title>
		<link>http://osteele.com/archives/2006/02/textcanvas</link>
		<comments>http://osteele.com/archives/2006/02/textcanvas#comments</comments>
		<pubDate>Tue, 28 Feb 2006 04:13:35 +0000</pubDate>
		<dc:creator>Oliver</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Libraries]]></category>
		<category><![CDATA[OpenLaszlo]]></category>
		<category><![CDATA[Projects]]></category>

		<guid isPermaLink="false">http://osteele.com/2006/02/28/canvas-with-text</guid>
		<description><![CDATA[The two times that I've used the WHATWG canvas element recently, I've wanted a canvas with string rendering.  The most recent time that I've used the OpenLaszlo drawview class (which has substantially the same API), I've wanted string rendering too.

The graph in "reAnimator":/tools/reanimator is a drawview, but with text labels for the edges.  And the graph and parse tree in the Graph and Parse tabs of "reMatch":/tools/rematch both use WHATWG canvas for lines, but text for labels.  (These tabs are only visible in Firefox, for now.)

"TextCanvas.js":/sources/javascript/textcanvas.js implements the canvas context extended with labels, for DHTML.  And "textdrawview.lzx" implements drawview extended with labels.  They share the same API, so that I can write graphics libraries (such as graph drawing) that work with both DHTML and OpenLaszlo.  That API is described "here":/sources/javascript/textcanvas-api.]]></description>
			<content:encoded><![CDATA[<p>The two times that I&#8217;ve used the <span class="caps">WHATWG </span>canvas element recently, I&#8217;ve wanted a canvas with string rendering.  The most recent time that I&#8217;ve used the OpenLaszlo drawview class (which has substantially the same <span class="caps">API</span>), I&#8217;ve wanted string rendering too.</p>

<p>The graph in <a href="/tools/reanimator">reAnimator</a> is a drawview, but with text labels for the edges.  And the graph and parse tree in the Graph and Parse tabs of <a href="/tools/rematch">reMatch</a> both use <span class="caps">WHATWG </span>canvas for lines, but text for labels.  (These tabs are only visible in Firefox, for now.)</p>

<p><a href="/sources/javascript/textcanvas.js">TextCanvas.js</a> implements the canvas context extended with labels, for <span class="caps">DHTML. </span> And &#8220;textdrawview.lzx&#8221; implements drawview extended with labels.  They share the same <span class="caps">API, </span>so that I can write graphics libraries (such as graph drawing) that work with both <span class="caps">DHTML </span>and OpenLaszlo.  That <span class="caps">API </span>is described <a href="/sources/javascript/textcanvas-api">here</a>.</p>

<p>The first example below is an OpenLaszlo application that uses textdrawview; view source <a href="/sources/openlaszlo/textdrawview-example.lzx">here</a>.</p>

<p><object width="340" height="140" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0"><param name="movie" value="/sources/openlaszlo/textdrawview-example.swf"><param name="quality" value="high"><param name="controller" value=""><embed src="/sources/openlaszlo/textdrawview-example.swf" width="340" height="140" quality="high" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"></object></p>

<p>If you&#8217;re using Firefox, you can also view the <span class="caps">DHTML </span>example.  This uses TextCanvas; open it in a separate page <a href="/sources/javascript/textcanvas-example.html" target="_blank">here</a>.</p>

<p><iframe src="/sources/javascript/textcanvas-example.html?inline=true" width="340" height="140" style="border: 0" scrolling="no"></iframe></p>

<p>Files:<br />
* <a href="/sources/javascript/textcanvas.js">textcanvas.js</a> &#8212; <span class="caps">DHTML </span>implementation<br />
* <a href="/sources/javascript/textcanvas-example.html">textcanvas-example.html</a> &#8212; <span class="caps">DHTML </span>example (shown running above)<br />
* <a href="/sources/openlaszlo/textdrawview.lzx">textdrawview.lzx</a> &#8212; OpenLaszlo implementation<br />
* <a href="/sources/openlaszlo/textdrawview-example.lzx">textdrawview-example.lzx</a> &#8212; OpenLaszlo example (shown running above)<br />
* <a href="/sources/javascript/textcanvas-api">textcanvas-api</a> &#8212; <span class="caps">API </span>documentation</p>

<p>Some example code:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// &lt;div id=&quot;canvasContainer&quot;&gt;&lt;/div&gt;</span>
<span style="color: #003366; font-weight: bold;">var</span> container <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'canvasContainer'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> textCanvasController <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> TextCanvasController<span style="color: #009900;">&#40;</span>container<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> ctx <span style="color: #339933;">=</span> textCanvasController.<span style="color: #660066;">getContext</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'2d'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
ctx.<span style="color: #660066;">moveTo</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
ctx.<span style="color: #660066;">lineTo</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">100</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">100</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
ctx.<span style="color: #660066;">stringStyle</span>.<span style="color: #660066;">color</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'red'</span><span style="color: #339933;">;</span>
ctx.<span style="color: #660066;">drawString</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;red&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
ctx.<span style="color: #660066;">stringStyle</span>.<span style="color: #660066;">color</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'blue'</span><span style="color: #339933;">;</span>
ctx.<span style="color: #660066;">drawString</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">100</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">100</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;blue&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>
]]></content:encoded>
			<wfw:commentRss>http://osteele.com/archives/2006/02/textcanvas/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
