<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-GB">
  <title>Haycorn</title>
  <subtitle>Technical posts from beebo.org</subtitle>
  <link href="https://beebo.org/haycorn/atom.xml" rel="self" />
  <link href="https://beebo.org/haycorn/" />
  <id>https://beebo.org/haycorn/</id>
  <updated>2016-02-24T00:00:00.000Z</updated>
  <author>
    <name>Michael Stillwell</name>
  </author>
  <entry>
    <title>How Knuth Brushes His Teeth</title>
    <link href="https://beebo.org/haycorn/2016-02-24_how-knuth-brushes-his-teeth.html" />
    <id>https://beebo.org/haycorn/2016-02-24_how-knuth-brushes-his-teeth.html</id>
    <updated>2016-02-24T00:00:00.000Z</updated>
    <content type="html">&lt;p&gt;A premature optimization?&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Q&lt;/strong&gt;: Do you have any other insights to offer into how you use maths in your work?&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;A&lt;/strong&gt;: For instance, when I brush my teeth I’ve got eight areas to cover,
namely Left and right, upper and lower, inside and outside. It’s most efficient
to follow a “Hamiltonian path” or “Gray code”:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;left upper outside&lt;/li&gt;
&lt;li&gt;right upper outside&lt;/li&gt;
&lt;li&gt;right upper inside&lt;/li&gt;
&lt;li&gt;left upper inside&lt;/li&gt;
&lt;li&gt;left lower inside&lt;/li&gt;
&lt;li&gt;right lower inside&lt;/li&gt;
&lt;li&gt;right lower outside&lt;/li&gt;
&lt;li&gt;left lower outside&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;
&lt;p&gt;(&lt;a href=&quot;http://authenticinquirymaths.blogspot.co.uk/2015/11/maths-in-science-interview-with-don.html&quot;&gt;source&lt;/a&gt;)&lt;/p&gt;
</content>
  </entry>
  <entry>
    <title>AppleScript Tips for Shell Scripting</title>
    <link href="https://beebo.org/haycorn/2015-10-03_applescript-tips.html" />
    <id>https://beebo.org/haycorn/2015-10-03_applescript-tips.html</id>
    <updated>2015-10-03T00:00:00.000Z</updated>
    <content type="html">&lt;p&gt;OS X is surprisingly scriptable, including OS X applications. (Especially Apple’s applications). This is the good news. The bad news is that said scripts are difficult to write and debug, and they don’t play very well with Unix-style shell scripts. (Manipulating Unix-style filenames is especially painful.)&lt;/p&gt;
&lt;p&gt;Here’s an example of what can be done: &lt;a href=&quot;https://github.com/ithinkihaveacat/dotfiles/blob/master/bin/select&quot;&gt;&lt;code&gt;select&lt;/code&gt;&lt;/a&gt; works like &lt;code&gt;open&lt;/code&gt; except that it opens a Finder window with the arguments selected:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-console&quot;&gt;&lt;span class=&quot;hljs-meta prompt_&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;language-bash&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;select&lt;/span&gt; *.gif&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&quot;https://beebo.org/haycorn/images/select.png&quot; alt=&quot;select *.gif output&quot;&gt;&lt;/p&gt;
&lt;p&gt;The AppleScript &lt;a href=&quot;https://github.com/ithinkihaveacat/dotfiles/blob/master/bin/select&quot;&gt;code&lt;/a&gt; is complicated and involves far more workarounds than it really should but, hey, it does work.&lt;/p&gt;
&lt;h4&gt;Tips&lt;/h4&gt;
&lt;p&gt;Some tips and tricks in no particular order.&lt;/p&gt;
&lt;h5&gt;Basics&lt;/h5&gt;
&lt;ul&gt;
&lt;li&gt;Use &lt;code&gt;&amp;amp;&lt;/code&gt; for string concatenation: &lt;code&gt;return &amp;quot;Hello, &amp;quot; &amp;amp; name&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;as [type]&lt;/code&gt; to coerce to a string (you’ll need this for sane debugging): &lt;code&gt;return foo as string&lt;/code&gt; would be written in C-like languages as &lt;code&gt;return (string) foo&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Built-in properties can contain spaces: &lt;code&gt;track number of current track&lt;/code&gt; is equivalent to something like &lt;code&gt;currentTrack['trackNumber']&lt;/code&gt; in JavaScript&lt;/li&gt;
&lt;li&gt;AppleScript tries to be English-like (never a good idea): assignment is via &lt;code&gt;set foo to bar&lt;/code&gt; (not &lt;code&gt;set foo = bar&lt;/code&gt;) and you see things like &lt;code&gt;count of every media item of container src&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h5&gt;Debugging&lt;/h5&gt;
&lt;p&gt;&lt;code&gt;log&lt;/code&gt; to dump output for debugging, though note that some types are coerced into strings for output–compare the output of &lt;code&gt;log obj&lt;/code&gt; and &lt;code&gt;log class of obj&lt;/code&gt; if not sure.&lt;/p&gt;
&lt;h5&gt;Script Editor&lt;/h5&gt;
&lt;p&gt;&lt;code&gt;osascript&lt;/code&gt;’s error messages are pretty awful; for a slightly (slightly!) better development experience, use the Script Editor application. This gets you syntax highlighting, and slightly better &lt;code&gt;log&lt;/code&gt; messages.&lt;/p&gt;
&lt;p&gt;For best results click on the “page” icon in the bottom of the window to reveal the “messages” tab:&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://beebo.org/haycorn/images/applescript2.png&quot; alt=&quot;Script Editor screenshot&quot;&gt;&lt;/p&gt;
&lt;p&gt;(Unfortunately Script Editor can’t open files that start with a shebang (see below)–you’ll need to copy and paste AppleScript to and from a text editor into Script Editor.)&lt;/p&gt;
&lt;h5&gt;Shell scripting&lt;/h5&gt;
&lt;p&gt;There’s a few different ways you can run AppleScript from the command line. First of all, you can use the standard &lt;code&gt;#!&lt;/code&gt; shebang with &lt;code&gt;osascript&lt;/code&gt; as the interpreter:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-console&quot;&gt;&lt;span class=&quot;hljs-meta prompt_&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;language-bash&quot;&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;cat&lt;/span&gt; hello.sh&lt;/span&gt;
&lt;span class=&quot;hljs-meta prompt_&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;language-bash&quot;&gt;!/usr/bin/osascript&lt;/span&gt;

return &amp;quot;Hello, World&amp;quot;
&lt;span class=&quot;hljs-meta prompt_&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;language-bash&quot;&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;chmod&lt;/span&gt; +x hello.sh&lt;/span&gt;
&lt;span class=&quot;hljs-meta prompt_&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;language-bash&quot;&gt;./hello.sh&lt;/span&gt;
Hello, World
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;To read arguments, use the &lt;code&gt;run&lt;/code&gt; method:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-console&quot;&gt;&lt;span class=&quot;hljs-meta prompt_&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;language-bash&quot;&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;cat&lt;/span&gt; hello.sh&lt;/span&gt;
&lt;span class=&quot;hljs-meta prompt_&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;language-bash&quot;&gt;!/usr/bin/osascript&lt;/span&gt;

on run argv
  return &amp;quot;Hello, &amp;quot; &amp;amp; item 1 of argv
end run
&lt;span class=&quot;hljs-meta prompt_&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;language-bash&quot;&gt;./hello.sh Clem&lt;/span&gt;
Hello, Clem
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;You might often find that you often want to do some pre-processing via &lt;code&gt;bash&lt;/code&gt;, especially for anything that involves filenames. This can be achieved (as in &lt;a href=&quot;https://github.com/ithinkihaveacat/dotfiles/blob/master/bin/select&quot;&gt;the &lt;code&gt;select&lt;/code&gt; example&lt;/a&gt;) via a &lt;code&gt;bash&lt;/code&gt; “heredoc”:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-sh&quot;&gt;&lt;span class=&quot;hljs-meta&quot;&gt;#!/bin/bash&lt;/span&gt;

&lt;span class=&quot;hljs-comment&quot;&gt;# bash code&lt;/span&gt;
&lt;span class=&quot;hljs-keyword&quot;&gt;if&lt;/span&gt; [ &lt;span class=&quot;hljs-variable&quot;&gt;$#&lt;/span&gt; == 0 ]; &lt;span class=&quot;hljs-keyword&quot;&gt;then&lt;/span&gt;
    &lt;span class=&quot;hljs-built_in&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;hljs-string&quot;&gt;&amp;quot;usage: &lt;span class=&quot;hljs-subst&quot;&gt;$(basename $0)&lt;/span&gt; args&amp;quot;&lt;/span&gt;
    &lt;span class=&quot;hljs-built_in&quot;&gt;exit&lt;/span&gt;
&lt;span class=&quot;hljs-keyword&quot;&gt;fi&lt;/span&gt;

&lt;span class=&quot;hljs-comment&quot;&gt;# AppleScript code&lt;/span&gt;
/usr/bin/osascript - &lt;span class=&quot;hljs-string&quot;&gt;&amp;quot;&lt;span class=&quot;hljs-variable&quot;&gt;$@&lt;/span&gt;&amp;quot;&lt;/span&gt; &amp;lt;&amp;lt; &lt;span class=&quot;hljs-string&quot;&gt;END

on run argv
  # ...
end run

END&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;h5&gt;Applications API&lt;/h5&gt;
&lt;p&gt;Use Script Editor’s “File | Open Dictionary…” to figure out what different applications let you do via AppleScript. Apple applications often expose a reasonably large API; for others you might get something minimal, or even nothing at all.&lt;/p&gt;
&lt;p&gt;Spotify, for example, &lt;a href=&quot;https://developer.spotify.com/applescript-api/&quot;&gt;only lets you play/pause/next/prev&lt;/a&gt;. You can also get some information about the currently-playing track. One the other hand Apple Photos has a fairly rich API. (&lt;a href=&quot;https://github.com/ithinkihaveacat/dotfiles/blob/master/applescript/photos-stacks-fixer.applescript&quot;&gt;Example script&lt;/a&gt;.)&lt;/p&gt;
&lt;h5&gt;Working with filenames&lt;/h5&gt;
&lt;p&gt;Need the current working directory?&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-applescript&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;set&lt;/span&gt; pwd &lt;span class=&quot;hljs-keyword&quot;&gt;to&lt;/span&gt; &lt;span class=&quot;hljs-built_in&quot;&gt;do shell script&lt;/span&gt; &lt;span class=&quot;hljs-string&quot;&gt;&amp;quot;pwd&amp;quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Create file object from Unix-style path (with “/”):&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-applescript&quot;&gt;(&lt;span class=&quot;hljs-keyword&quot;&gt;POSIX file&lt;/span&gt; unixpath)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Get Unix-style path from a file object:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-applescript&quot;&gt;(&lt;span class=&quot;hljs-keyword&quot;&gt;POSIX path&lt;/span&gt; &lt;span class=&quot;hljs-built_in&quot;&gt;file&lt;/span&gt;)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For more information on handling filenames, see:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://developer.apple.com/library/mac/documentation/AppleScript/Conceptual/AppleScriptLangGuide/conceptual/ASLR_fundamentals.html#//apple_ref/doc/uid/TP40000983-CH218-SW28&quot;&gt;Aliases and Files&lt;/a&gt; (from Apple’s documentation)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://www.satimage.fr/software/en/smile/external_codes/file_paths.html&quot;&gt;AppleScript and POSIX paths&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</content>
  </entry>
  <entry>
    <title>Tabs and Makefile</title>
    <link href="https://beebo.org/haycorn/2015-04-20_tabs-and-makefiles.html" />
    <id>https://beebo.org/haycorn/2015-04-20_tabs-and-makefiles.html</id>
    <updated>2015-04-20T00:00:00.000Z</updated>
    <content type="html">&lt;p&gt;The computing world is littered with unfortunate mistakes that managed to make
the leap from small-scale inconvenience to a source of grief to millions or
billions–at which point so many people and systems have been become accustomed
to the error that fixing it is impossible.&lt;/p&gt;
&lt;p&gt;One notorious example is the spelling of HTTP header &lt;code&gt;referer&lt;/code&gt;, which escaped
the Netscape Corporation embedded in the bowels of their web browser without
anyone noticing the error. From there it spread to every other web browser (they
needed to be compatible), and finally
&lt;a href=&quot;https://tools.ietf.org/html/rfc1945#section-10.13&quot;&gt;RFC1945&lt;/a&gt;, the Hypertext
Transfer Protocol standard itself. (At least, I’m pretty sure how things
happened, but &lt;a href=&quot;https://en.wikipedia.org/wiki/HTTP_referer&quot;&gt;Wikipedia is
inconclusive&lt;/a&gt;. Does anyone know?)&lt;/p&gt;
&lt;p&gt;Another is the fateful decision of the author of &lt;code&gt;make&lt;/code&gt; to have the space
character and the (visually identical) tab character do very different things.
Very early on, the story goes, the author realized that this was a mistake, but
by that point &lt;em&gt;dozens&lt;/em&gt; of people were relying on it—backward-compatibility
needed to be preserved—and so the tabs had to stay.&lt;/p&gt;
&lt;p&gt;Unfortunately I couldn’t find a good source for this story online, so I emailed
the author, &lt;a href=&quot;https://en.wikipedia.org/wiki/Stuart_Feldman&quot;&gt;Stuart Feldman&lt;/a&gt;, and
asked him if it was true.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-text&quot;&gt;From: Michael Stillwell &amp;lt;...@google.com&amp;gt;
Date: Mon, 20 Apr 2015 at 15:33
Subject: make versus tabs
To: Stuart Feldman &amp;lt;...@google.com&amp;gt;

Once upon a time I heard a story that soon after make was
released, one of its first users complained that the requirement
that each command line begin with a tab was suboptimal, and that
some alternative approach should be used. As the story goes, the
author was receptive to this criticism, but explained that sadly
it was now too late to change the way things worked, because too
many people were used to current behavior. The punchline, of
course, was that at this point make had perhaps dozens of users.

I was Googling a bit to try and figure out whether my memory was
correct (it&amp;#x27;s surprisingly resistant to search), when I
discovered that make&amp;#x27;s author works at Google, and so it might be
possible to get a definitive answer by emailing you directly.

So, to satisfy my curiosity, perhaps you could tell me: is the
story true? Are there any other details you can recall and share?
And how do you feel about tabs versus spaces versus whitespace
now?
&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-text&quot;&gt;From: Stuart Feldman &amp;lt;...@google.com&amp;gt;
Date: Mon, 20 Apr 2015 at 15:51
Subject: Re: make versus tabs
To: Michael Stillwell &amp;lt;...@google.com&amp;gt;

Story is only partly true.

I used tabs because I was trying to use Lex (still in first
version) and had trouble with some other patterns.

(Make was written over a weekend, rewritten the next weekend ...)

So I gave up on being smart and just used a fixed pattern (^\t)
to indicate rules.

Within a few weeks of writing Make, I already had a dozen friends
who were using it.

So even though I knew that &amp;quot;tab in column 1&amp;quot; was a bad idea, I
didn&amp;#x27;t want to disrupt my user base.

So instead I wrought havoc on tens of millions.

I have used that example in software engineering lectures.

Side note: I was awarded the ACM Software Systems Award for Make
a decade ago.  In my one minute talk on stage, I began &amp;quot;I would
like to apologize&amp;quot;. The audience then split in two - half started
laughing, the other half looked at the laughers.

A perfect bipartite graph of programmers and non-programmers.
&lt;/code&gt;&lt;/pre&gt;
</content>
  </entry>
  <entry>
    <title>Old Code &amp; Large Numbers</title>
    <link href="https://beebo.org/haycorn/2015-04-06_old-code.html" />
    <id>https://beebo.org/haycorn/2015-04-06_old-code.html</id>
    <updated>2015-04-06T00:00:00.000Z</updated>
    <content type="html">&lt;p&gt;Found an old passive aggressive (or maybe just aggressive) letter of mine that
was published in an Australian science magazine for teens called &lt;em&gt;Double Helix&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;Horrible photo. It includes code, in BASIC. Numbered lines. There is a “clear screen” command though I seem to have avoided &lt;code&gt;GOTO&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://beebo.org/haycorn/images/Michael1991_sm.jpg&quot;&gt;&lt;img src=&quot;https://beebo.org/haycorn/images/Michael1991_tiny.jpg&quot;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Not really related, but while looking through Wikipedia for more information on
this problem (it’s an an example of a &lt;a href=&quot;https://en.wikipedia.org/wiki/Tag_system&quot;&gt;Tag
System&lt;/a&gt;), I came across some
&lt;a href=&quot;https://en.wikipedia.org/wiki/Collatz_conjecture#Experimental_evidence&quot;&gt;fascinating examples of conjectures that have extremely large (numeric)
counter-examples&lt;/a&gt;.
(i.e. conjectures resistant to computer-derived counter-examples, and common
sense.)&lt;/p&gt;
&lt;p&gt;They are:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/P%C3%B3lya_conjecture&quot;&gt;Pólya conjecture&lt;/a&gt; - initial counterexample was 1.845 × 10&lt;sup&gt;361&lt;/sup&gt;, though now reduced to 906,150,257.&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/Mertens_conjecture&quot;&gt;Mertens conjecture&lt;/a&gt; - upper bound is &lt;em&gt;e&lt;/em&gt;&lt;sup&gt;1.59×10&lt;sup&gt;40&lt;/sup&gt;&lt;/sup&gt;.&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/Skewes%27_number&quot;&gt;Skewes’ number&lt;/a&gt; - somewhere around &lt;em&gt;e&lt;/em&gt;&lt;sup&gt;727.95133&lt;/sup&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Scott Aaronson has an intriguing blog post on &lt;a href=&quot;http://www.scottaaronson.com/writings/bignumbers.html&quot;&gt;the biggest number you can write down in 15 seconds using standard math notation&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;See also &lt;a href=&quot;https://en.wikipedia.org/wiki/Graham%27s_number&quot;&gt;Graham’s number&lt;/a&gt;.&lt;/p&gt;
</content>
  </entry>
  <entry>
    <title>Promise Patterns</title>
    <link href="https://beebo.org/haycorn/2015-03-29_promise-patterns.html" />
    <id>https://beebo.org/haycorn/2015-03-29_promise-patterns.html</id>
    <updated>2015-03-29T00:00:00.000Z</updated>
    <content type="html">&lt;p&gt;Promises are still pretty new, and whilst their place in the Javascript world is
by this point pretty secure (most of the newer JS APIs are Promise based), the
ways in which they’re used are still evolving: the clunky syntax and nested
&lt;code&gt;.then()&lt;/code&gt; structures often recall async “callback hell”, there’s no consistency
to APIs that return promises, and there’s no design patterns to introduce some
order. (If working with push notifications, for example, you’re likely to end up
with at least three Promise-returning constructions: a static property
(&lt;code&gt;.ready&lt;/code&gt;), a getter (&lt;code&gt;.getSubscription()&lt;/code&gt;) and a verb (&lt;code&gt;.subscribe()&lt;/code&gt;).)&lt;/p&gt;
&lt;p&gt;One approach to improving this situation is to lean on &lt;a href=&quot;http://www.html5rocks.com/en/tutorials/es6/promises/#toc-parallelism-sequencing&quot;&gt;ES6’s
&lt;code&gt;yield&lt;/code&gt;&lt;/a&gt;
or &lt;a href=&quot;https://github.com/google/traceur-compiler/wiki/LanguageFeatures#async-functions-experimental&quot;&gt;ES7’s
&lt;code&gt;await&lt;/code&gt;&lt;/a&gt;
and use &lt;a href=&quot;https://babeljs.io/&quot;&gt;Babel&lt;/a&gt; or
&lt;a href=&quot;https://github.com/google/traceur-compiler&quot;&gt;Traceur&lt;/a&gt; to transpile future
Javascript into the Javascript of today, but there’s a few things you can do
without leaving browser-compatible Javascript.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Use the same name for the promise and the name of the value the promise resolves to&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Since you never really need the promise and the value the promise resolves to at
the same time, give them the same name–they represent the same thing anyway,
and giving them completely separate names makes no sense. (Annotating the types
Hungarian-notation style might help, but it turns out that prepending or
appending &lt;code&gt;p&lt;/code&gt; or &lt;code&gt;promise&lt;/code&gt; to variable names looks silly.)&lt;/p&gt;
&lt;p&gt;That is, if your promise is &lt;code&gt;foo&lt;/code&gt;, then the name of the argument should also be
&lt;code&gt;foo&lt;/code&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-js&quot;&gt;foo.&lt;span class=&quot;hljs-title function_&quot;&gt;then&lt;/span&gt;(&lt;span class=&quot;hljs-keyword&quot;&gt;function&lt;/span&gt; (&lt;span class=&quot;hljs-params&quot;&gt;foo&lt;/span&gt;) {
  &lt;span class=&quot;hljs-comment&quot;&gt;// ...&lt;/span&gt;
});
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Similarly, a function like &lt;code&gt;fetch()&lt;/code&gt; does not return a &lt;code&gt;fetchPromise&lt;/code&gt;; it
returns a &lt;code&gt;response&lt;/code&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-js&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;var&lt;/span&gt; response = &lt;span class=&quot;hljs-title function_&quot;&gt;fetch&lt;/span&gt;(...);
response.&lt;span class=&quot;hljs-title function_&quot;&gt;then&lt;/span&gt;(&lt;span class=&quot;hljs-keyword&quot;&gt;function&lt;/span&gt; (&lt;span class=&quot;hljs-params&quot;&gt;response&lt;/span&gt;) {
  &lt;span class=&quot;hljs-comment&quot;&gt;// ...&lt;/span&gt;
});
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Eliminate Promise chaining with functions, objects–any which way you can&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Promise hell usually isn’t quite as bad as callback hell, but it can certainly
be hell-&lt;em&gt;ish&lt;/em&gt;. For example, some Promise-based APIs you may be able to encapsulate common operations on a&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Separate Promise code from non-Promise code&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;A lot of code &lt;a href=&quot;https://github.com/GoogleChrome/samples/blob/gh-pages/service-worker/selective-caching/service-worker.js#L58-L112&quot;&gt;mixes together logic and the boilerplate that goes with the
Promise
syntax&lt;/a&gt;.
Try to avoid mixing the two by moving all the boilerplate somewhere else.
(Encapsulating it inside an object might a viable choice.)&lt;/p&gt;
&lt;p&gt;It’s possible that it will help to convert simple values into Promises:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Try to write the code you’d write if Promises were built in to the language&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;When working with Promises, try to imagine what the code would look like if
Promises were built in to the language as first class values. (That is, if you
could use Promises wherever you would otherwise use values.) Is it possible to
actually write code like this, by moving move the Promise-manipulation code
somewhere else? If this is possible, is it otherwise a good idea?&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;printf&lt;/code&gt;-style debugging of Promises&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Here’s a simple function for &lt;code&gt;console.log&lt;/code&gt;-style debugging of Promises:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-js&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;hljs-title function_&quot;&gt;DEBUG&lt;/span&gt;(&lt;span class=&quot;hljs-params&quot;&gt;s, p&lt;/span&gt;) {
  p.&lt;span class=&quot;hljs-title function_&quot;&gt;then&lt;/span&gt;(
    &lt;span class=&quot;hljs-variable language_&quot;&gt;console&lt;/span&gt;.&lt;span class=&quot;hljs-property&quot;&gt;log&lt;/span&gt;.&lt;span class=&quot;hljs-title function_&quot;&gt;bind&lt;/span&gt;(&lt;span class=&quot;hljs-variable language_&quot;&gt;console&lt;/span&gt;, s, &lt;span class=&quot;hljs-string&quot;&gt;&amp;quot;RESOLVED&amp;quot;&lt;/span&gt;),
    &lt;span class=&quot;hljs-variable language_&quot;&gt;console&lt;/span&gt;.&lt;span class=&quot;hljs-property&quot;&gt;warn&lt;/span&gt;.&lt;span class=&quot;hljs-title function_&quot;&gt;bind&lt;/span&gt;(&lt;span class=&quot;hljs-variable language_&quot;&gt;console&lt;/span&gt;, s, &lt;span class=&quot;hljs-string&quot;&gt;&amp;quot;REJECTED&amp;quot;&lt;/span&gt;),
  );
  &lt;span class=&quot;hljs-keyword&quot;&gt;return&lt;/span&gt; p;
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Use it like this:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-js&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;var&lt;/span&gt; user = foo.&lt;span class=&quot;hljs-title function_&quot;&gt;getUser&lt;/span&gt;(); &lt;span class=&quot;hljs-comment&quot;&gt;// returns a Promise&lt;/span&gt;
&lt;span class=&quot;hljs-title function_&quot;&gt;DEBUG&lt;/span&gt;(&lt;span class=&quot;hljs-string&quot;&gt;&amp;quot;user = &amp;quot;&lt;/span&gt;, user);
user.&lt;span class=&quot;hljs-title function_&quot;&gt;then&lt;/span&gt;(...)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Remember that &lt;code&gt;.then()&lt;/code&gt; lambdas can return promises&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Resolving a list of promises to arguments&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Remember that you can attache multiple “listeners” to a Promise&lt;/strong&gt;&lt;/p&gt;
</content>
  </entry>
  <entry>
    <title>BBC News Goes Responsive</title>
    <link href="https://beebo.org/haycorn/2015-03-23_responsive-news.html" />
    <id>https://beebo.org/haycorn/2015-03-23_responsive-news.html</id>
    <updated>2015-03-23T00:00:00.000Z</updated>
    <content type="html">&lt;p&gt;Today, BBC News &lt;a href=&quot;http://responsivenews.co.uk/post/114413142693/weve-made-it&quot;&gt;stopped redirecting&lt;/a&gt; mobiles users from &lt;code&gt;www.bbc.co.uk/news&lt;/code&gt; to
&lt;code&gt;m.bbc.co.uk/news&lt;/code&gt;. Now everyone gets the same thing: we are all responsive now, and no &lt;a href=&quot;https://beebo.org/haycorn/2012-03-13_mobile-specific-urls-theyre-breaking-the-web.html&quot;&gt;shitty redirects&lt;/a&gt;. Congratulations to
&lt;a href=&quot;https://twitter.com/jcleveley&quot;&gt;@jcleveley&lt;/a&gt;,
&lt;a href=&quot;https://twitter.com/sthulb&quot;&gt;@sthulb&lt;/a&gt;,
&lt;a href=&quot;https://twitter.com/sfrench001&quot;&gt;@sfrench001&lt;/a&gt; and loads of other good and
talented folk!&lt;/p&gt;
&lt;p&gt;I worked on the BBC News codebase a bit as part of a BBC World Service posting,
and it was one of the best projects I’ve ever worked on. I learnt a lot about
developing software, but also got to appreciate a bit more strongly the needs of
the more marginalised and vulnerable (in terms of bandwidth, hardware, language,
freedom of expression) internet users. Not to get all serious or anything but
some people out there have a pretty shitty experience and from what I saw the
BBC World Service does a pretty good job of trying to make things a little bit
better and a little bit clearer.&lt;/p&gt;
&lt;p&gt;The language aspect was pretty educational, though. The World Service is
&lt;a href=&quot;http://www.bbc.co.uk/ws/languages&quot;&gt;available in 27 languages&lt;/a&gt;, and for the most
part the choice comes down to a combination of usage and need: in many places
people would be stuck with only one source of “news” if it weren’t for the World
Service. So there’s no French or German news (no French news for people living
in France, that is–there’s most definitely definitely &lt;a href=&quot;http://www.bbc.co.uk/afrique&quot;&gt;news for French speakers
living in Africa&lt;/a&gt;), but there’s news in
&lt;a href=&quot;http://www.bbc.co.uk/ukrainian&quot;&gt;Ukrainian&lt;/a&gt;,
&lt;a href=&quot;http://www.bbc.co.uk/persian&quot;&gt;Persian&lt;/a&gt; and
&lt;a href=&quot;http://www.bbc.co.uk/kyrgyz&quot;&gt;Kyrgyz&lt;/a&gt;. Did you know that Uzbek &lt;a href=&quot;http://www.bbc.co.uk/uzbek&quot;&gt;has three
written scripts&lt;/a&gt;? (The page is mostly Cyrillic on
this page, but there’s also a Latin section, as well as an Arabic tab.) Or that
&lt;a href=&quot;http://www.bbc.co.uk/hausa&quot;&gt;Hausa&lt;/a&gt; (34m native speakers!) has apparently not
settled on the spelling of the month “April”? (If you know why, please answer
&lt;a href=&quot;https://www.quora.com/Localization-interface-design/How-do-you-spell-April-in-Hausa&quot;&gt;this
question&lt;/a&gt;
on Quora!) Or that Burmese is written in &lt;a href=&quot;http://www.bbc.co.uk/burmese&quot;&gt;such a pretty
script&lt;/a&gt;?&lt;/p&gt;
&lt;p&gt;Anyway, the point is: thank-you and congratulations!&lt;/p&gt;
</content>
  </entry>
  <entry>
    <title>Securing a Personal Website</title>
    <link href="https://beebo.org/haycorn/2015-03-14_ssl-tls-personal-website.html" />
    <id>https://beebo.org/haycorn/2015-03-14_ssl-tls-personal-website.html</id>
    <updated>2015-03-14T00:00:00.000Z</updated>
    <content type="html">&lt;p&gt;So a while ago I decided that SSL/TLS (&lt;a href=&quot;https://thoughtstreams.io/glyph/there-is-no-ssl/&quot;&gt;why it’s really TLS, not
SSL&lt;/a&gt;) was the
&lt;a href=&quot;http://googlewebmastercentral.blogspot.co.uk/2014/08/https-as-ranking-signal.html&quot;&gt;right&lt;/a&gt;
&lt;a href=&quot;http://www.html5rocks.com/en/tutorials/service-worker/introduction/&quot;&gt;thing&lt;/a&gt; to
do, but &lt;a href=&quot;https://blog.cloudflare.com/&quot;&gt;CloudFlare’s blog&lt;/a&gt;, the
seemingly endless &lt;a href=&quot;https://en.wikipedia.org/wiki/Transport_Layer_Security#Attacks_against_TLS.2FSSL&quot;&gt;run of TLS/SSL
vulnerabilities&lt;/a&gt;,
and the various blog posts explaining just how complicated it is to &lt;a href=&quot;https://wingolog.org/archives/2014/10/17/ffs-ssl&quot;&gt;set up
TLS&lt;/a&gt; (let alone get an A+
rating on SSL Labs’ SSL Test
[&lt;a href=&quot;https://scotthelme.co.uk/a-plus-rating-qualys-ssl-test/&quot;&gt;1&lt;/a&gt;][&lt;a href=&quot;https://sethvargo.com/getting-an-a-plus-on-qualys-ssl-labs-tester/&quot;&gt;2&lt;/a&gt;][&lt;a href=&quot;https://blog.surfnet.nl/en/pimp-your-ssllabs-rating/&quot;&gt;3&lt;/a&gt;]), got me all
depressed.&lt;/p&gt;
&lt;p&gt;For a little while I considered holding out for the EFF-supported &lt;a href=&quot;https://letsencrypt.org/&quot;&gt;Let’s
Encrypt&lt;/a&gt; project, which is supposed to make securing a
website as easy as &lt;a href=&quot;https://letsencrypt.org/howitworks/&quot;&gt;running two commands&lt;/a&gt;
but: (a) it’s not available yet (it’s arriving “Mid-2015”); and (b) I’m
skeptical that they can actually make it as easy as all that unless you’re
running a very popular Linux distribution in a very default way, and I kinda
wanted to get out of the business of running my own HTTP server anyway.&lt;/p&gt;
&lt;p&gt;Sadly it seems that these days, outsourcing TLS is the only simple, secure and
performant way to do it, as it is with DNS and SMTP. (This also provides a few
other bells and whistles that would otherwise be days or hours of work to
configure and maintain at the same time too, like abuse protection, a reverse
proxy and maybe even a CDN.)&lt;/p&gt;
&lt;p&gt;I’ve investigated two approaches in a little bit of detail—Gandi’s &lt;a href=&quot;https://www.gandi.net/hosting/simple?language=nodejs&amp;amp;db=mongodb&quot;&gt;Simple
Hosting&lt;/a&gt; and
CloudFlare’s &lt;a href=&quot;https://www.cloudflare.com/plans&quot;&gt;Free Plan&lt;/a&gt; in &lt;a href=&quot;https://support.cloudflare.com/hc/en-us/articles/200170416-What-do-the-SSL-options-Off-Flexible-SSL-Full-SSL-Full-SSL-Strict-mean-&quot;&gt;Flexible SSL
mode&lt;/a&gt;—and
eventually went with Gandi. CloudFlare has more features, is likely to be faster
for anyone not in Paris (especially for the first page load), and is incredibly
easy to set up and configure (you &lt;a href=&quot;https://blog.cloudflare.com/introducing-universal-ssl/&quot;&gt;don’t even need to buy a
certificate&lt;/a&gt;), but in
the end it’s another piece of infrastructure to configure, if not exactly
“manage”, and I already had a Gandi account, and a happy relationship with them.
A web site also seems like a fairly important bit of infrastructure to be
handled by a free service, especially since you also need to use their DNS
servers.&lt;/p&gt;
&lt;p&gt;Anyway, the upshot of all this is that this site now runs on Gandi’s PaaS
infrastructure, and I have an &lt;a href=&quot;https://www.ssllabs.com/ssltest/analyze.html?d=beebo.org&quot;&gt;A+ rating on SSL Labs’s SSL
Test&lt;/a&gt;. (Buying a
certificate through Gandi got me to an A; the A+ comes from enabling &lt;a href=&quot;https://en.wikipedia.org/wiki/HTTP_Strict_Transport_Security&quot;&gt;HTTP
Strict Transport
Security&lt;/a&gt;, which
is a little bit scary since I’m now committed to running &lt;code&gt;https://beebo.org&lt;/code&gt; for
at least a year.)&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://www.ssllabs.com/ssltest/analyze.html?d=beebo.org&quot;&gt;&lt;img src=&quot;https://beebo.org/img/misc/qualys-ssl-test.png&quot; alt=&quot;A+ SSL Test screenshot&quot;&gt;&lt;/a&gt;&lt;/p&gt;
</content>
  </entry>
  <entry>
    <title>Mountain Lion's Complicated Document Model</title>
    <link href="https://beebo.org/haycorn/2012-08-07_mountain-lions-complicated-document-model.html" />
    <id>https://beebo.org/haycorn/2012-08-07_mountain-lions-complicated-document-model.html</id>
    <updated>2012-08-07T00:00:00.000Z</updated>
    <content type="html">&lt;p&gt;A few thousand words on the &lt;a href=&quot;http://tidbits.com/article/13187&quot;&gt;Mountain Lion document model&lt;/a&gt;, the
auto save model, how it differs from previous versions of OS X, and so on.  Does
this really need to be so complicated?  I very much prefer the way &lt;a href=&quot;http://www.sublimetext.com/&quot;&gt;Sublime Text&lt;/a&gt; handles this problem: a
document is continuously auto-saved to some secret location, with the changes
only written to disk when you explicitly hit save.  As soon as you make some
changes to a document, the &amp;ldquo;unsaved&amp;rdquo; indicator in the tab changed to
&amp;ldquo;unsaved&amp;rdquo;, but if you quit the editor at this point: (a)
there&amp;rsquo;s no prompt to get you to save &amp;ldquo;unsaved&amp;rdquo; changes;  and
(b) if you restart the editor, you get everything back the way it was before you
quit, including the &amp;ldquo;unsaved&amp;rdquo; indicator.  (It doesn&amp;rsquo;t save the
undo stack, I just noticed, so it&amp;rsquo;s not perfect, but it&amp;rsquo;s so much
better than the confusing muddle of Mountain Lion.)&lt;/p&gt;
</content>
  </entry>
  <entry>
    <title>Sugru Tips</title>
    <link href="https://beebo.org/haycorn/2012-06-09_sugru-tips.html" />
    <id>https://beebo.org/haycorn/2012-06-09_sugru-tips.html</id>
    <updated>2012-06-09T00:00:00.000Z</updated>
    <content type="html">&lt;p&gt;Some tips from a booklet that came with my packet of Sugru.  I was hoping
this would be online, so I could just throw the booklet out (the suggestion that
you clean you hands by wiping them with tissue paper is excellent&amp;mdash;water
and soap is surprisingly and frustratingly ineffective), but it&amp;rsquo;s not, and
so I&amp;rsquo;m typing it out here&amp;hellip;&lt;/p&gt;
&lt;p&gt;Master Your Craft&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;mix sugru to create great new colours&lt;/li&gt;
&lt;li&gt;or just marble two colours for a funky effect&lt;/li&gt;
&lt;li&gt;if you don&amp;rsquo;t want sugru to stick, use soapy water (it&amp;rsquo;s a release agent)&lt;/li&gt;
&lt;li&gt;to remove sugru, carve most of it off with a sharp knife, then
remove the rest with your finger nail and a tissue&lt;/li&gt;
&lt;li&gt;carve sugru for a super sharp finish (use a scalpel when it is cured
/ semi-cured)&lt;/li&gt;
&lt;li&gt;add texture, it feels lovely and looks awesome&lt;/li&gt;
&lt;li&gt;if you want a super smooth surface&amp;mdash;use soapy water and rub gently&lt;/li&gt;
&lt;li&gt;build up your hack&amp;mdash;sugru sticks to sugru&lt;/li&gt;
&lt;li&gt;blend&amp;mdash;add a bit of something special&amp;mdash;wool, glitter, sand&amp;hellip;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;In case you didn&amp;rsquo;t know&amp;hellip;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;the best way to clean your hands during / after using sugru is to
&lt;strong&gt;wipe them with tissue paper&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;sugru cures faster at room temp (21°C)&lt;/li&gt;
&lt;/ul&gt;
</content>
  </entry>
  <entry>
    <title>What SQL Query &quot;Optimisation&quot; Has Done For Me Lately</title>
    <link href="https://beebo.org/haycorn/2012-03-19_what-sql-query-optimisation-has-done-for-me-l.html" />
    <id>https://beebo.org/haycorn/2012-03-19_what-sql-query-optimisation-has-done-for-me-l.html</id>
    <updated>2012-03-19T00:00:00.000Z</updated>
    <content type="html">&lt;p&gt;So I assumed databases would have a pretty good SQL optimisation story
by now, but no&amp;hellip;  Two recent disappointments:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;MySQL failed to notice that &lt;code&gt;SELECT ... FROM ... WHERE ? IN (foo,
bar)&lt;/code&gt; was equivalent to &lt;code&gt;... WHERE foo = ? OR bar = 2&lt;/code&gt; and/or that the
latter would be much faster.&lt;/li&gt;
&lt;li&gt;Copying a database from one server to another led to a query that
took 20 seconds on the source timing out (after 10 minutes) on the
copy.  Even more bizarrely, the fix was to tell the copy to ignore an
index.  (The master and copy weren&amp;rsquo;t exactly the same version of
MySQL, but they were both varieties of 5.1.x.)&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The first time it sees a query, MongoDB &lt;a href=&quot;http://www.mongodb.org/display/DOCS/Query+Optimizer&quot;&gt;runs multiple query plans in
parallel&lt;/a&gt;,
remembers which one worked best, and uses that the next time.  Is
there any relational database that does this?  Seems like a good idea,
given recent experiences&amp;hellip;  Are there any web pages showing what
clever things query optimisers get up to, especially optimisations
that aren&amp;rsquo;t immediately obvious to a human being?  (For a &lt;code&gt;gcc&lt;/code&gt;
example, see &lt;a href=&quot;http://ridiculousfish.com/blog/posts/will-it-optimize.html&quot;&gt;Will it
Optimize?&lt;/a&gt;)&lt;/p&gt;
</content>
  </entry>
  <entry>
    <title>Mobile specific URLs? They're breaking the web</title>
    <link href="https://beebo.org/haycorn/2012-03-13_mobile-specific-urls-theyre-breaking-the-web.html" />
    <id>https://beebo.org/haycorn/2012-03-13_mobile-specific-urls-theyre-breaking-the-web.html</id>
    <updated>2012-03-13T00:00:00.000Z</updated>
    <content type="html">&lt;p&gt;Those mobile specific URLs&amp;mdash;&lt;code&gt;m.*&lt;/code&gt;, &lt;code&gt;mobile.*&lt;/code&gt; and
the like&amp;mdash;they&amp;rsquo;ve got to go.  There&amp;rsquo;s just too many
mobile-specific URLs popping up on Twitter and similar, and since for some
reason the browser detection only only pushes people from desktop to mobile, if
you&amp;rsquo;re actually on a desktop browser, you end up stuck there.  A poor user
experience, and it doesn&amp;rsquo;t sell many ads, either.  People are just going
to have to do that shit right&amp;mdash;the same URL (i.e. resource) for everyone,
and content negotiation (or &amp;ldquo;responsive design&amp;rdquo;) thereafter.  (Set a
cookie summarising what&amp;rsquo;s in the User-Agent header, if necessary.)
It&amp;rsquo;s not that hard!&lt;/p&gt;
</content>
  </entry>
  <entry>
    <title>State-of-the-art JS compilers: no bytecode, no intermediate representation other than source</title>
    <link href="https://beebo.org/haycorn/2011-07-19_state-of-the-art-js-compilers-no-bytecode-no.html" />
    <id>https://beebo.org/haycorn/2011-07-19_state-of-the-art-js-compilers-no-bytecode-no.html</id>
    <updated>2011-07-19T00:00:00.000Z</updated>
    <content type="html">&lt;p&gt;A brief note; something I discovered recently found surprising:
JavaScript compilers, which these days end up essentially compiling the
same source code into machine code multiple times (the same function
might sometimes take ints and sometimes take floats, for example), do it
by going back to the source code and re-parsing the source. In
particular, they don’t start from an intermediate representation like an
AST or bytecode. This applies both the
&lt;a href=&quot;http://wingolog.org/archives/2011/07/05/v8-a-tale-of-two-compilers&quot;&gt;v8&lt;/a&gt;
and
&lt;a href=&quot;http://www.hanselman.com/blog/JavaScriptisAssemblyLanguagefortheWebPart2MadnessorjustInsanity.aspx&quot;&gt;Firefox&lt;/a&gt;.
(Actually it’s not clear that Firefox actually operates this way, but
Eich says that it could.) I wonder if they have essentially two versions
of the parser, one for unverified source code (better debugging
information, but slower) and another for source code known to be
syntactically correct? And what does this mean for
&lt;a href=&quot;http://www.parrot.org/&quot;&gt;Parrot&lt;/a&gt;?&lt;/p&gt;
</content>
  </entry>
  <entry>
    <title>Modifying protected properties in PHP</title>
    <link href="https://beebo.org/haycorn/2011-07-06_modifying-protected-properties-in-php.html" />
    <id>https://beebo.org/haycorn/2011-07-06_modifying-protected-properties-in-php.html</id>
    <updated>2011-07-06T00:00:00.000Z</updated>
    <content type="html">&lt;p&gt;In a &lt;a href=&quot;http://weierophinney.net/matthew/archives/263-Proxies-in-PHP.html&quot;&gt;blog
post&lt;/a&gt;,
Matthew Weier O’Phinney pointed out that if class &lt;code&gt;Derived&lt;/code&gt; extends
class &lt;code&gt;Base&lt;/code&gt;, &lt;code&gt;Derived&lt;/code&gt; can access protected properties and methods of
&lt;code&gt;Base&lt;/code&gt; &lt;strong&gt;even in the case where an instance of &lt;code&gt;Base&lt;/code&gt; is merely the
argument to some method&lt;/strong&gt;. (I expected &lt;code&gt;Derived&lt;/code&gt; to be able to access
&lt;code&gt;$this-&amp;gt;foo&lt;/code&gt; where &lt;code&gt;$foo&lt;/code&gt; is protected, but I didn’t expect that
&lt;code&gt;Derived&lt;/code&gt; would be able to access &lt;code&gt;$obj-&amp;gt;foo&lt;/code&gt; where &lt;code&gt;$obj&lt;/code&gt; is an
argument.)&lt;/p&gt;
&lt;p&gt;This works even if the functions are static, leading to a generic way to
modify protected properties:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-php&quot;&gt;&lt;span class=&quot;hljs-class&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;hljs-title&quot;&gt;Foo&lt;/span&gt; &lt;/span&gt;{

    &lt;span class=&quot;hljs-keyword&quot;&gt;protected&lt;/span&gt; &lt;span class=&quot;hljs-variable&quot;&gt;$message&lt;/span&gt; = &lt;span class=&quot;hljs-string&quot;&gt;&amp;quot;jjj&amp;quot;&lt;/span&gt;;

}

&lt;span class=&quot;hljs-class&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;hljs-title&quot;&gt;Foo_Wrapper&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;extends&lt;/span&gt; &lt;span class=&quot;hljs-title&quot;&gt;Foo&lt;/span&gt; &lt;/span&gt;{

    &lt;span class=&quot;hljs-comment&quot;&gt;// Allows you to set both public and protected (!) properties&lt;/span&gt;

    &lt;span class=&quot;hljs-built_in&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;hljs-function&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;hljs-title&quot;&gt;set&lt;/span&gt;(&lt;span class=&quot;hljs-params&quot;&gt;&lt;span class=&quot;hljs-variable&quot;&gt;$obj&lt;/span&gt;, &lt;span class=&quot;hljs-variable&quot;&gt;$property&lt;/span&gt;, &lt;span class=&quot;hljs-variable&quot;&gt;$value&lt;/span&gt;&lt;/span&gt;) &lt;/span&gt;{
        &lt;span class=&quot;hljs-variable&quot;&gt;$obj&lt;/span&gt;-&amp;gt;{&lt;span class=&quot;hljs-variable&quot;&gt;$property&lt;/span&gt;} = &lt;span class=&quot;hljs-variable&quot;&gt;$value&lt;/span&gt;;
    }

    &lt;span class=&quot;hljs-comment&quot;&gt;// Allows you to get both public and protected (!) properties&lt;/span&gt;

    &lt;span class=&quot;hljs-built_in&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;hljs-function&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;hljs-title&quot;&gt;get&lt;/span&gt;(&lt;span class=&quot;hljs-params&quot;&gt;&lt;span class=&quot;hljs-variable&quot;&gt;$obj&lt;/span&gt;, &lt;span class=&quot;hljs-variable&quot;&gt;$property&lt;/span&gt;&lt;/span&gt;) &lt;/span&gt;{
        &lt;span class=&quot;hljs-keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;hljs-variable&quot;&gt;$obj&lt;/span&gt;-&amp;gt;{&lt;span class=&quot;hljs-variable&quot;&gt;$property&lt;/span&gt;};
    }

}

&lt;span class=&quot;hljs-variable&quot;&gt;$obj&lt;/span&gt; = &lt;span class=&quot;hljs-keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;hljs-title class_&quot;&gt;Foo&lt;/span&gt;();

&lt;span class=&quot;hljs-title class_&quot;&gt;Foo_Wrapper&lt;/span&gt;::&lt;span class=&quot;hljs-title function_ invoke__&quot;&gt;set&lt;/span&gt;(&lt;span class=&quot;hljs-variable&quot;&gt;$obj&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&amp;quot;message&amp;quot;&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&amp;quot;kkk&amp;quot;&lt;/span&gt;);

&lt;span class=&quot;hljs-keyword&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;hljs-title class_&quot;&gt;Foo_Wrapper&lt;/span&gt;::&lt;span class=&quot;hljs-title function_ invoke__&quot;&gt;get&lt;/span&gt;(&lt;span class=&quot;hljs-variable&quot;&gt;$obj&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&amp;quot;message&amp;quot;&lt;/span&gt;), &lt;span class=&quot;hljs-string&quot;&gt;&amp;quot;\n&amp;quot;&lt;/span&gt;;
&lt;/code&gt;&lt;/pre&gt;
</content>
  </entry>
  <entry>
    <title>The difference between GPL and Apache-licensed code for web developers: not so much, actually</title>
    <link href="https://beebo.org/haycorn/2011-05-20_the-difference-between-gpl-and-apache-license.html" />
    <id>https://beebo.org/haycorn/2011-05-20_the-difference-between-gpl-and-apache-license.html</id>
    <updated>2011-05-20T00:00:00.000Z</updated>
    <content type="html">&lt;p&gt;For most web developers, there is no practical difference between using
GPL’d code (version 2) and code with a more permissive license such as
Apache or BSD. For some reason, a lot of people don’t seem to get this.
(One situation in which there is difference: if you’re distributing the
actual code itself—if people download your code and run it on their own
servers, for example—in this case you need to make the source code
available. Another is where your code is “distributed” by being embedded
on a hardware device, such as a phone.)&lt;/p&gt;
&lt;p&gt;If the code runs on your servers only, you do not need to publicly
release your changes. If you’ve written the code for a customer, you
need to give them the source code, but there’s no legal obligation for
the customer to make the code publicly available to anyone else.  (As an
example, Google has written a lot of patches to MySQL and Linux, which
have been deployed onto their machines.  They haven’t had to make any of
these patches publicly available—although at least in the case of MySQL,
they’ve subsequently &lt;a href=&quot;http://code.google.com/p/google-mysql-tools/wiki/Mysql5Patches&quot;&gt;made some of these changes
public&lt;/a&gt;.)&lt;/p&gt;
&lt;p&gt;This is more or less a loophole in the GPL—it was written at a time when
interactions with software were mostly conducted with software running
on your own machine, and the loophole is the reason the
&lt;a href=&quot;http://en.wikipedia.org/wiki/Affero_General_Public_License&quot;&gt;AGPL&lt;/a&gt;
exists. The AGPL has the properties many people seem to think the GPL
has: in the case of AGPL-licensed code, if you make modifications and
run the modified code on your server, you do need to make the
modifications public. (&lt;a href=&quot;http://www.mongodb.org/&quot;&gt;MongoDB&lt;/a&gt; is perhaps the
most prominent example of software licensed in this way; they have a
helpful &lt;a href=&quot;http://www.mongodb.org/display/DOCS/Licensing&quot;&gt;licensing page&lt;/a&gt;
describing the circumstances under which you do and don’t have to
release any changes you might make.)&lt;/p&gt;
&lt;p&gt;A few useful entries from the FSF’s GPL 2 FAQ:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;http://www.gnu.org/licenses/old-licenses/gpl-2.0-faq.html#UnreleasedMods&quot;&gt;A company is running a modified version of a GPL’ed program on a
web site. Does the GPL say they must release their modified
sources?&lt;/a&gt;
– No&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://www.gnu.org/licenses/old-licenses/gpl-2.0-faq.html#DevelopChangesUnderNDA&quot;&gt;Does the GPL allow me to develop a modified version under a
nondisclosure
agreement?&lt;/a&gt;
– Yes&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://www.gnu.org/licenses/old-licenses/gpl-2.0-faq.html#WMS&quot;&gt;What license should I use for CMS
templates?&lt;/a&gt;
– “Templates are minor enough that it is not worth using copyleft to
protect them.”  Also, calling GPL’d JavaScript code from HTML does
not “infect” the HTML with the GPL, but you might want to add a
comment making it clear that it does not.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;(In reading the FAQ entries, note that “distribution” is &lt;em&gt;not&lt;/em&gt; what
you’re doing if you deploy software to web servers, or your customers’
web servers.  Distribution is where customers get a copy of your
software to run on their servers.)&lt;/p&gt;
</content>
  </entry>
  <entry>
    <title>Why Aspect-Oriented Programming? What's wrong with pub/sub (or hooks)?</title>
    <link href="https://beebo.org/haycorn/2011-05-08_why-aspect-oriented-programming-whats-wrong-w.html" />
    <id>https://beebo.org/haycorn/2011-05-08_why-aspect-oriented-programming-whats-wrong-w.html</id>
    <updated>2011-05-08T00:00:00.000Z</updated>
    <content type="html">&lt;p&gt;Is there any substantive difference between &lt;a href=&quot;http://en.wikipedia.org/wiki/Aspect-oriented_programming&quot;&gt;Aspect-Oriented
Programming&lt;/a&gt;
and the
&lt;a href=&quot;http://en.wikipedia.org/wiki/Publish/subscribe&quot;&gt;publish/subscribe&lt;/a&gt;
messaging pattern, where you explicitly publish a message at join
points? (I consider &lt;a href=&quot;http://en.wikipedia.org/wiki/Hooking&quot;&gt;hooks&lt;/a&gt; and
the &lt;a href=&quot;http://en.wikipedia.org/wiki/Observer_pattern&quot;&gt;observer pattern&lt;/a&gt; to
be pretty equivalent to pub/sub, too.)&lt;/p&gt;
&lt;p&gt;To ask the question another way, is there really any point pushing for
&lt;a href=&quot;http://en.wikipedia.org/wiki/AspectJ&quot;&gt;existing languages to be enhanced to support
AOP&lt;/a&gt; when you could just use
pub/sub and get nearly all the benefits? With pub/sub you avoid all the
syntax weirdness, the source code transformation weirdness (if you’re
doing it that way), the cultural resistance from introducing AOP to
language that doesn’t already have it, the need to learn a new
syntax/DSL to express join points…&lt;/p&gt;
&lt;p&gt;The main disadvantage of hooks seems to be that you need to be able to
modify existing code to use them. They’re also ugly if you want hooks
absolutely everywhere, but against this they’re explicit, they can be
triggered at absolutely any point, and they don’t need language support.&lt;/p&gt;
&lt;p&gt;(Some examples: &lt;a href=&quot;http://codex.wordpress.org/Plugin_API&quot;&gt;WordPress&lt;/a&gt;,
&lt;a href=&quot;http://apidock.com/rails/ActiveRecord/Observer&quot;&gt;Rails&lt;/a&gt;.)&lt;/p&gt;
</content>
  </entry>
  <entry>
    <title>Recommendation: &quot;Writing Testable Code&quot;</title>
    <link href="https://beebo.org/haycorn/2011-04-17_recommendation-writing-testable-code.html" />
    <id>https://beebo.org/haycorn/2011-04-17_recommendation-writing-testable-code.html</id>
    <updated>2011-04-17T00:00:00.000Z</updated>
    <content type="html">&lt;p&gt;A few folks at Google have put together a very readable PDF book called &lt;a href=&quot;http://misko.hevery.com/attachments/Guide-Writing%20Testable%20Code.pdf&quot;&gt;Writing
Testable
Code&lt;/a&gt;.
It covers four flaws, and explains, in admirable detail and great clarity, why
they are a bad thing, and how to fix them. I don’t usually like articles like
this—I find them too vague and non-specific—but this is a brilliant exception.
The four flaws are:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Constructor does real work&lt;/li&gt;
&lt;li&gt;Digging into collaborators&lt;/li&gt;
&lt;li&gt;Brittle global state and singletons&lt;/li&gt;
&lt;li&gt;Class does too much&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;(It’s not just about testable code; it’s about good code in general.
Testable code is much easier to reason about than untestable code.)&lt;/p&gt;
</content>
  </entry>
  <entry>
    <title>spl_object_hash: what is it good for??</title>
    <link href="https://beebo.org/haycorn/2011-04-16_splobjecthash-what-is-it-good-for.html" />
    <id>https://beebo.org/haycorn/2011-04-16_splobjecthash-what-is-it-good-for.html</id>
    <updated>2011-04-16T00:00:00.000Z</updated>
    <content type="html">&lt;p&gt;Is it possible to do anything useful with PHP’s
&lt;a href=&quot;http://php.net/manual/en/function.spl-object-hash.php&quot;&gt;&lt;code&gt;spl_object_hash()&lt;/code&gt;&lt;/a&gt;
function? It returns an identifier when passed an object, but:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;If the object is destroyed, the identifier can be re-used.&lt;/li&gt;
&lt;li&gt;If the object is changed, the identifier stays the same.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;(In having these two properties, it seems as though the identifier is
essentially equivalent to the address of the object in memory.) What on
earth is this for?&lt;/p&gt;
</content>
  </entry>
  <entry>
    <title>JavaScript CDNs: for effective caching be specific about the version you want</title>
    <link href="https://beebo.org/haycorn/2011-04-16_javascript-cdns-for-effective-caching-be-spec.html" />
    <id>https://beebo.org/haycorn/2011-04-16_javascript-cdns-for-effective-caching-be-spec.html</id>
    <updated>2011-04-16T00:00:00.000Z</updated>
    <content type="html">&lt;p&gt;If you use Google’s CDN, if at all possible, you really want to be specific
about the version of the library you want. Otherwise, if you say you want 1.7.x,
the library will need to be sent to the browser with instructions to check back
every so often to see if a newer release in the 1.7 series is available, which
in Google’s case is just 1 hour. On the other hand, if you are able to specify
that you want Prototype 1.7.0.0—no more and no less—then the browser is able to
cache the library for 1 year.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-console&quot;&gt;boom:~$ wget --quiet --save-headers -O - \
http://ajax.googleapis.com/ajax/libs/prototype/1.7/prototype.js | grep ^Cache\-Control
Cache-Control: public, must-revalidate, proxy-revalidate, max-age=3600
boom:~$ wget --quiet --save-headers -O - \
http://ajax.googleapis.com/ajax/libs/prototype/1.7.0.0/prototype.js | grep ^Cache\-Control
Cache-Control: public, max-age=31536000
&lt;/code&gt;&lt;/pre&gt;
</content>
  </entry>
  <entry>
    <title>ZendMail and Amazon's Kindle Converter</title>
    <link href="https://beebo.org/haycorn/2011-02-05_zendmail-and-amazons-kindle-document-converte.html" />
    <id>https://beebo.org/haycorn/2011-02-05_zendmail-and-amazons-kindle-document-converte.html</id>
    <updated>2011-02-05T00:00:00.000Z</updated>
    <content type="html">&lt;p&gt;If you’re trying to use
&lt;a href=&quot;http://framework.zend.com/manual/en/zend.mail.html&quot;&gt;Zend_Mail&lt;/a&gt; to send
email to Amazon’s free &lt;a href=&quot;http://www.amazon.co.uk/kindlepersonaldocuments/&quot;&gt;PDF-to-Kindle
converter&lt;/a&gt;, you might
like to know that for some reason two headers that &lt;code&gt;Zend_Mail&lt;/code&gt; adds to
the message—&lt;code&gt;Content-Transfer-Encoding&lt;/code&gt; and &lt;code&gt;Content-Disposition&lt;/code&gt;—trick
Amazon’s converter into thinking that there are no attachments. (These
emails are interpreted correctly by pretty much everything else as far
as I can tell so I’m not sure if this is &lt;code&gt;Zend_Mail&lt;/code&gt;’s fault, or
Amazon’s, or both.)&lt;/p&gt;
&lt;p&gt;There’s probably several ways to fix this, but I fixed it by creating a
new version of &lt;code&gt;Zend_Mail_Transport_Smtp&lt;/code&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-php&quot;&gt;&lt;span class=&quot;hljs-class&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;hljs-title&quot;&gt;Zend_Mail_Transport_Smtp_Kindle&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;extends&lt;/span&gt; &lt;span class=&quot;hljs-title&quot;&gt;Zend_Mail_Transport_Smtp&lt;/span&gt;
&lt;/span&gt;{
    &lt;span class=&quot;hljs-keyword&quot;&gt;protected&lt;/span&gt; &lt;span class=&quot;hljs-function&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;hljs-title&quot;&gt;_prepareHeaders&lt;/span&gt;(&lt;span class=&quot;hljs-params&quot;&gt;&lt;span class=&quot;hljs-variable&quot;&gt;$headers&lt;/span&gt;&lt;/span&gt;)
    &lt;/span&gt;{
        &lt;span class=&quot;hljs-comment&quot;&gt;// Remove some headers from the mail enclosure--these somehow confuse&lt;/span&gt;
        &lt;span class=&quot;hljs-comment&quot;&gt;// Amazon&amp;#x27;s Kindle converter, leading it to conclude that there are&lt;/span&gt;
        &lt;span class=&quot;hljs-comment&quot;&gt;// no attachments.  (Even though every other mail client seems to be&lt;/span&gt;
        &lt;span class=&quot;hljs-comment&quot;&gt;// able to cope.)&lt;/span&gt;

        &lt;span class=&quot;hljs-keyword&quot;&gt;if&lt;/span&gt; (&lt;span class=&quot;hljs-title function_ invoke__&quot;&gt;array_key_exists&lt;/span&gt;(&lt;span class=&quot;hljs-string&quot;&gt;&amp;quot;Content-Transfer-Encoding&amp;quot;&lt;/span&gt;, &lt;span class=&quot;hljs-variable&quot;&gt;$headers&lt;/span&gt;)) {
            &lt;span class=&quot;hljs-keyword&quot;&gt;unset&lt;/span&gt;(&lt;span class=&quot;hljs-variable&quot;&gt;$headers&lt;/span&gt;[&lt;span class=&quot;hljs-string&quot;&gt;&amp;quot;Content-Transfer-Encoding&amp;quot;&lt;/span&gt;]);
        }
        &lt;span class=&quot;hljs-keyword&quot;&gt;if&lt;/span&gt; (&lt;span class=&quot;hljs-title function_ invoke__&quot;&gt;array_key_exists&lt;/span&gt;(&lt;span class=&quot;hljs-string&quot;&gt;&amp;quot;Content-Disposition&amp;quot;&lt;/span&gt;, &lt;span class=&quot;hljs-variable&quot;&gt;$headers&lt;/span&gt;)) {
            &lt;span class=&quot;hljs-keyword&quot;&gt;unset&lt;/span&gt;(&lt;span class=&quot;hljs-variable&quot;&gt;$headers&lt;/span&gt;[&lt;span class=&quot;hljs-string&quot;&gt;&amp;quot;Content-Disposition&amp;quot;&lt;/span&gt;]);
        }

        &lt;span class=&quot;hljs-keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;hljs-built_in&quot;&gt;parent&lt;/span&gt;::&lt;span class=&quot;hljs-title function_ invoke__&quot;&gt;_prepareHeaders&lt;/span&gt;(&lt;span class=&quot;hljs-variable&quot;&gt;$headers&lt;/span&gt;);
    }
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Anyway, with this “bug” worked around, I can now do this:&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;http://beebo.org/img/stuff/print.png&quot; alt=&quot;OS X print dialog, with &amp;quot;Send to Kindle&amp;quot;
option&quot;&gt;&lt;/p&gt;
&lt;p&gt;My &lt;code&gt;~/Library/PDF Services/Send to Kindle&lt;/code&gt; reads:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-sh&quot;&gt;&lt;span class=&quot;hljs-meta&quot;&gt;#!/bin/bash&lt;/span&gt;

TITLE=$(perl -MURI::Escape -e &lt;span class=&quot;hljs-string&quot;&gt;&amp;#x27;print uri_escape(join(&amp;quot; &amp;quot;, @ARGV))&amp;#x27;&lt;/span&gt; &lt;span class=&quot;hljs-variable&quot;&gt;$1&lt;/span&gt;)

&lt;span class=&quot;hljs-built_in&quot;&gt;cat&lt;/span&gt; &lt;span class=&quot;hljs-string&quot;&gt;&amp;quot;&lt;span class=&quot;hljs-variable&quot;&gt;$3&lt;/span&gt;&amp;quot;&lt;/span&gt; | curl --data-binary @- &lt;span class=&quot;hljs-string&quot;&gt;&amp;quot;http://beebo.org/api/kindle/?username=ithinkihaveacat&amp;amp;title=&lt;span class=&quot;hljs-variable&quot;&gt;$TITLE&lt;/span&gt;&amp;quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;(See “&lt;a href=&quot;http://developer.apple.com/library/mac/#documentation/Printing/Conceptual/PDF_Workflow/pdfwf_concepts/pdfwf_concepts.html&quot;&gt;Providing PDF Workflow Options in the Print
Dialog&lt;/a&gt;”
for more information.)&lt;/p&gt;
</content>
  </entry>
  <entry>
    <title>Understanding the Y Combinator by writing about it</title>
    <link href="https://beebo.org/haycorn/2011-01-01_understanding-the-y-combinator-by-writing-abo.html" />
    <id>https://beebo.org/haycorn/2011-01-01_understanding-the-y-combinator-by-writing-abo.html</id>
    <updated>2011-01-01T00:00:00.000Z</updated>
    <content type="html">&lt;h2&gt;Motivation&lt;/h2&gt;
&lt;p&gt;This post is written for those who, like me, have tried and failed to
understand the Y Combinator despite the help of Wikipedia, blog posts
constructing the Y Combinator in multiple languages, stories on Hacker
News, comments on Hacker News… I think I mostly get it now (this comes
courtesy of a night in Helsinki where I was up at a strange time due to
the fallout from a Helsinki-Stockholm-Helsinki booze cruise)—and so
perhaps these notes will help someone else. If nothing else, writing
them up helped me! (Maybe the only way to understand it is to write a
blog post about it.)&lt;/p&gt;
&lt;p&gt;I think a large part of the problem understanding the Y Combinator is
that the various bits of information available online are written from
several different perspectives, which means that they emphasise
different aspects of the Y Combinator story, to wit:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;The properties of the Y Combinator function itself.&lt;/strong&gt; Technically,
the Y Combinator is one example of a &lt;a href=&quot;http://en.wikipedia.org/wiki/Fixed_point_combinator&quot;&gt;fixed point
combinator&lt;/a&gt;,
meaning that it’s a higher order function with a particular property
(to be described later). Sadly it seems this property is very hard
to grok unless you’ve recently spent some time with &lt;a href=&quot;http://en.wikipedia.org/wiki/Lambda_calculus&quot;&gt;lambda
calculus&lt;/a&gt; (or at least
it was for me), so discussion is usually motivated by one particular
application of the Y Combinator, which is that…&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;The Y Combinator allows you to write “recursive” functions without
explicit recursion.&lt;/strong&gt; (“Without explicit recursion” means
“recursion” without explicitly calling a function named &lt;code&gt;foo&lt;/code&gt; in the
body of a function that itself is called &lt;code&gt;foo&lt;/code&gt;.) Thus, discussion of
the Y Combinator is tied up with anonymous (i.e. unnamed) functions.
(Note that the Y Combinator-powered version of a function will
almost certainly not be any faster than the recursive version, and
it’s going to need just as much stack space.) Finally, Y Combinator
posts and articles often have an extended section on…&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;The implementation of the Y Combinator in some language.&lt;/strong&gt; The Y
Combinator is an important part of (theoretical) computer science,
and its implementation typically stretches a language’s support for
first class functions—it’s often instructive to compare how
elegantly (or not) it can be implemented in various languages. (A
few examples:
&lt;a href=&quot;http://igstan.ro/posts/2010-12-01-deriving-the-y-combinator-in-7-easy-steps.html&quot;&gt;JavaScript&lt;/a&gt;,
&lt;a href=&quot;http://use.perl.org/~Aristotle/journal/30896&quot;&gt;Perl&lt;/a&gt;,
&lt;a href=&quot;http://nerderati.com/2009/11/the-php-5-3-y-combinator/&quot;&gt;PHP&lt;/a&gt;.)&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;Deriving the Y Combinator&lt;/h2&gt;
&lt;h3&gt;Step 1&lt;/h3&gt;
&lt;p&gt;As I mentioned above, discussion of the Y Combinator is typically
motivated by a particular problem: how to write “recursive” functions
without explicit recursion. I don’t know any better way of going about
it, so let’s first consider that standard exemplar of recursion, the
factorial function, and transform it into a version that uses the Y
Combinator. The standard implementation:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-javascript&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;var&lt;/span&gt; fact = &lt;span class=&quot;hljs-keyword&quot;&gt;function&lt;/span&gt; (&lt;span class=&quot;hljs-params&quot;&gt;n&lt;/span&gt;) {
  &lt;span class=&quot;hljs-keyword&quot;&gt;if&lt;/span&gt; (n &amp;lt; &lt;span class=&quot;hljs-number&quot;&gt;2&lt;/span&gt;) &lt;span class=&quot;hljs-keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;;
  &lt;span class=&quot;hljs-keyword&quot;&gt;return&lt;/span&gt; n * &lt;span class=&quot;hljs-title function_&quot;&gt;fact&lt;/span&gt;(n - &lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;);
};
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;(I’m using JavaScript in these examples because its widely known and has
good support for first class functions.)&lt;/p&gt;
&lt;h3&gt;Step 2&lt;/h3&gt;
&lt;p&gt;How might you go about removing the self-referential call to &lt;code&gt;fact()&lt;/code&gt;
within the function body? (The exact reasons for wanting to do this are
not important, but for example suppose that you want to inline the
entire call.) One obvious way is to use a keyword provided by the
language that refers to the function itself such as JavaScript’s
&lt;a href=&quot;http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Objects:Function:arguments:callee&quot;&gt;arguments.callee&lt;/a&gt;,
but, well, this is &lt;em&gt;cheating&lt;/em&gt; and we can do better!&lt;/p&gt;
&lt;h3&gt;Step 3&lt;/h3&gt;
&lt;p&gt;Our first attempt is to transform the &lt;code&gt;fact()&lt;/code&gt; function slightly, so
that &lt;code&gt;fact&lt;/code&gt; becomes an &lt;em&gt;argument&lt;/em&gt; to a wrapper function, instead of a
free variable referring to the factorial function. We can then use this
argument in the “recursive” call:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-javascript&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;var&lt;/span&gt; fact_wrapper1 = &lt;span class=&quot;hljs-keyword&quot;&gt;function&lt;/span&gt; (&lt;span class=&quot;hljs-params&quot;&gt;fact&lt;/span&gt;) {
  &lt;span class=&quot;hljs-keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;function&lt;/span&gt; (&lt;span class=&quot;hljs-params&quot;&gt;n&lt;/span&gt;) {
    &lt;span class=&quot;hljs-keyword&quot;&gt;if&lt;/span&gt; (n &amp;lt; &lt;span class=&quot;hljs-number&quot;&gt;2&lt;/span&gt;) &lt;span class=&quot;hljs-keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;;
    &lt;span class=&quot;hljs-keyword&quot;&gt;return&lt;/span&gt; n * &lt;span class=&quot;hljs-title function_&quot;&gt;fact&lt;/span&gt;(n - &lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;);
  };
};
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;At this point, the function contains no references to variables declared
outside the scope of the function itself—though overall we haven’t much
progress because we also don’t have a working &lt;code&gt;fact()&lt;/code&gt; function… If only
there were some way to call &lt;code&gt;fact_wrapper1()&lt;/code&gt; passing it its own return
value as an argument! If we could get this to work, the return value of
&lt;code&gt;fact_wrapper1()&lt;/code&gt; would be the factorial function we want.&lt;/p&gt;
&lt;h3&gt;Step 4&lt;/h3&gt;
&lt;p&gt;Okay, so that attempt didn’t quite work out; let’s try a slightly
different approach. Perhaps it’s possible to modify &lt;code&gt;fact_wrapper1()&lt;/code&gt; so
that it can be passed as an argument to itself. (This currently isn’t
possible because &lt;code&gt;fact_wrapper1()&lt;/code&gt;’s argument (&lt;code&gt;fact&lt;/code&gt;) is a simple
function that takes an integer argument and returns an integer; by
contrast &lt;code&gt;fact_wrapper1()&lt;/code&gt; itself is a &lt;a href=&quot;http://en.wikipedia.org/wiki/Higher-order_function&quot;&gt;higher-order
function&lt;/a&gt; that
receives a &lt;em&gt;function&lt;/em&gt; as an argument, returning a simple function that
takes an integer argument and returning an integer.) If we can pass the
function to itself, we eliminate the need to figure out how to extract
just its return value. &lt;code&gt;fact_wrapper1()&lt;/code&gt; can be transformed into such a
function as follows:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-javascript&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;var&lt;/span&gt; fact_wrapper2 = &lt;span class=&quot;hljs-keyword&quot;&gt;function&lt;/span&gt; (&lt;span class=&quot;hljs-params&quot;&gt;f&lt;/span&gt;) {
  &lt;span class=&quot;hljs-keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;function&lt;/span&gt; (&lt;span class=&quot;hljs-params&quot;&gt;n&lt;/span&gt;) {
    &lt;span class=&quot;hljs-keyword&quot;&gt;if&lt;/span&gt; (n &amp;lt; &lt;span class=&quot;hljs-number&quot;&gt;2&lt;/span&gt;) &lt;span class=&quot;hljs-keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;;
    &lt;span class=&quot;hljs-keyword&quot;&gt;return&lt;/span&gt; n * &lt;span class=&quot;hljs-title function_&quot;&gt;f&lt;/span&gt;(f)(n - &lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;);
  };
};
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Note that the only real change is that &lt;code&gt;f(f)(n - 1)&lt;/code&gt; replaces
&lt;code&gt;fact(n - 1)&lt;/code&gt;. This is because &lt;code&gt;f(f)&lt;/code&gt; returns a function, which we then
apply to &lt;code&gt;n - 1&lt;/code&gt;.&lt;/p&gt;
&lt;h3&gt;Step 5&lt;/h3&gt;
&lt;p&gt;Let’s just check to see if &lt;code&gt;fact_wrapper2&lt;/code&gt; can really be used as an
argument to itself:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-node-repl&quot;&gt;&lt;span class=&quot;hljs-meta prompt_&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;language-javascript&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;var&lt;/span&gt; fact = &lt;span class=&quot;hljs-title function_&quot;&gt;fact_wrapper2&lt;/span&gt;(fact_wrapper2);&lt;/span&gt;
&lt;span class=&quot;hljs-meta prompt_&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;language-javascript&quot;&gt;&lt;span class=&quot;hljs-title function_&quot;&gt;fact&lt;/span&gt;(&lt;span class=&quot;hljs-number&quot;&gt;5&lt;/span&gt;);&lt;/span&gt;
120
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Okay, that works: so far, so good! (You can copy-and-paste all these
examples into a &lt;a href=&quot;http://nodejs.org/&quot;&gt;NodeJS&lt;/a&gt; session.)&lt;/p&gt;
&lt;p&gt;We can also make &lt;code&gt;fact_wrapper2()&lt;/code&gt; look a bit more like
&lt;code&gt;fact_wrapper1()&lt;/code&gt; by moving the weird &lt;code&gt;f(f)&lt;/code&gt; call into a helper
function:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-javascript&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;var&lt;/span&gt; fact_wrapper2 = &lt;span class=&quot;hljs-keyword&quot;&gt;function&lt;/span&gt; (&lt;span class=&quot;hljs-params&quot;&gt;f&lt;/span&gt;) {
  &lt;span class=&quot;hljs-keyword&quot;&gt;var&lt;/span&gt; g = &lt;span class=&quot;hljs-keyword&quot;&gt;function&lt;/span&gt; (&lt;span class=&quot;hljs-params&quot;&gt;n&lt;/span&gt;) {
    &lt;span class=&quot;hljs-keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;hljs-title function_&quot;&gt;f&lt;/span&gt;(f)(n);
  };
  &lt;span class=&quot;hljs-keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;function&lt;/span&gt; (&lt;span class=&quot;hljs-params&quot;&gt;n&lt;/span&gt;) {
    &lt;span class=&quot;hljs-keyword&quot;&gt;if&lt;/span&gt; (n &amp;lt; &lt;span class=&quot;hljs-number&quot;&gt;2&lt;/span&gt;) &lt;span class=&quot;hljs-keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;;
    &lt;span class=&quot;hljs-keyword&quot;&gt;return&lt;/span&gt; n * &lt;span class=&quot;hljs-title function_&quot;&gt;g&lt;/span&gt;(n - &lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;);
  };
};
&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;Step 6&lt;/h3&gt;
&lt;p&gt;By adding another helper function, we can avoid the need for
&lt;code&gt;fact_wrapper2&lt;/code&gt; to appear twice, and if we inline its definition, we can
avoid the need to give the wrapper function a name at all:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-javascript&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;var&lt;/span&gt; recur = &lt;span class=&quot;hljs-keyword&quot;&gt;function&lt;/span&gt; (&lt;span class=&quot;hljs-params&quot;&gt;f&lt;/span&gt;) {
  &lt;span class=&quot;hljs-keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;hljs-title function_&quot;&gt;f&lt;/span&gt;(f);
};

&lt;span class=&quot;hljs-keyword&quot;&gt;var&lt;/span&gt; fact = &lt;span class=&quot;hljs-title function_&quot;&gt;recur&lt;/span&gt;(&lt;span class=&quot;hljs-keyword&quot;&gt;function&lt;/span&gt; (&lt;span class=&quot;hljs-params&quot;&gt;f&lt;/span&gt;) {
  &lt;span class=&quot;hljs-keyword&quot;&gt;var&lt;/span&gt; g = &lt;span class=&quot;hljs-keyword&quot;&gt;function&lt;/span&gt; (&lt;span class=&quot;hljs-params&quot;&gt;n&lt;/span&gt;) {
    &lt;span class=&quot;hljs-keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;hljs-title function_&quot;&gt;f&lt;/span&gt;(f)(n);
  };
  &lt;span class=&quot;hljs-keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;function&lt;/span&gt; (&lt;span class=&quot;hljs-params&quot;&gt;n&lt;/span&gt;) {
    &lt;span class=&quot;hljs-keyword&quot;&gt;if&lt;/span&gt; (n &amp;lt; &lt;span class=&quot;hljs-number&quot;&gt;2&lt;/span&gt;) &lt;span class=&quot;hljs-keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;;
    &lt;span class=&quot;hljs-keyword&quot;&gt;return&lt;/span&gt; n * &lt;span class=&quot;hljs-title function_&quot;&gt;g&lt;/span&gt;(n - &lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;);
  };
});
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We still get the same result:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-node-repl&quot;&gt;&lt;span class=&quot;hljs-meta prompt_&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;language-javascript&quot;&gt;&lt;span class=&quot;hljs-title function_&quot;&gt;fact&lt;/span&gt;(&lt;span class=&quot;hljs-number&quot;&gt;5&lt;/span&gt;);&lt;/span&gt;
120
&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;Step 7&lt;/h3&gt;
&lt;p&gt;Next, let’s transform Step 6 by eliminating the local variable &lt;code&gt;g&lt;/code&gt;,
instead passing it in as an argument of a new inlined function:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-javascript&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;var&lt;/span&gt; recur = &lt;span class=&quot;hljs-keyword&quot;&gt;function&lt;/span&gt; (&lt;span class=&quot;hljs-params&quot;&gt;f&lt;/span&gt;) {
  &lt;span class=&quot;hljs-keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;hljs-title function_&quot;&gt;f&lt;/span&gt;(f);
};

&lt;span class=&quot;hljs-keyword&quot;&gt;var&lt;/span&gt; fact = &lt;span class=&quot;hljs-title function_&quot;&gt;recur&lt;/span&gt;(&lt;span class=&quot;hljs-keyword&quot;&gt;function&lt;/span&gt; (&lt;span class=&quot;hljs-params&quot;&gt;f&lt;/span&gt;) {
  &lt;span class=&quot;hljs-keyword&quot;&gt;return&lt;/span&gt; (&lt;span class=&quot;hljs-keyword&quot;&gt;function&lt;/span&gt; (&lt;span class=&quot;hljs-params&quot;&gt;g&lt;/span&gt;) {
    &lt;span class=&quot;hljs-keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;function&lt;/span&gt; (&lt;span class=&quot;hljs-params&quot;&gt;n&lt;/span&gt;) {
      &lt;span class=&quot;hljs-comment&quot;&gt;// unchanged from above&lt;/span&gt;
      &lt;span class=&quot;hljs-keyword&quot;&gt;if&lt;/span&gt; (n &amp;lt; &lt;span class=&quot;hljs-number&quot;&gt;2&lt;/span&gt;) &lt;span class=&quot;hljs-keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;;
      &lt;span class=&quot;hljs-keyword&quot;&gt;return&lt;/span&gt; n * &lt;span class=&quot;hljs-title function_&quot;&gt;g&lt;/span&gt;(n - &lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;);
    };
  })(&lt;span class=&quot;hljs-keyword&quot;&gt;function&lt;/span&gt; (&lt;span class=&quot;hljs-params&quot;&gt;n&lt;/span&gt;) {
    &lt;span class=&quot;hljs-keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;hljs-title function_&quot;&gt;f&lt;/span&gt;(f)(n);
  });
});
&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;Step 8&lt;/h3&gt;
&lt;p&gt;This lets us move the factorial function itself out of the helper code:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-javascript&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;var&lt;/span&gt; fact_core = &lt;span class=&quot;hljs-keyword&quot;&gt;function&lt;/span&gt; (&lt;span class=&quot;hljs-params&quot;&gt;g&lt;/span&gt;) {
  &lt;span class=&quot;hljs-keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;function&lt;/span&gt; (&lt;span class=&quot;hljs-params&quot;&gt;n&lt;/span&gt;) {
    &lt;span class=&quot;hljs-keyword&quot;&gt;if&lt;/span&gt; (n &amp;lt; &lt;span class=&quot;hljs-number&quot;&gt;2&lt;/span&gt;) &lt;span class=&quot;hljs-keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;;
    &lt;span class=&quot;hljs-keyword&quot;&gt;return&lt;/span&gt; n * &lt;span class=&quot;hljs-title function_&quot;&gt;g&lt;/span&gt;(n - &lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;);
  };
};

&lt;span class=&quot;hljs-keyword&quot;&gt;var&lt;/span&gt; recur = &lt;span class=&quot;hljs-keyword&quot;&gt;function&lt;/span&gt; (&lt;span class=&quot;hljs-params&quot;&gt;f&lt;/span&gt;) {
  &lt;span class=&quot;hljs-keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;hljs-title function_&quot;&gt;f&lt;/span&gt;(f);
};

&lt;span class=&quot;hljs-keyword&quot;&gt;var&lt;/span&gt; fact = &lt;span class=&quot;hljs-title function_&quot;&gt;recur&lt;/span&gt;(&lt;span class=&quot;hljs-keyword&quot;&gt;function&lt;/span&gt; (&lt;span class=&quot;hljs-params&quot;&gt;f&lt;/span&gt;) {
  &lt;span class=&quot;hljs-keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;hljs-title function_&quot;&gt;fact_core&lt;/span&gt;(&lt;span class=&quot;hljs-keyword&quot;&gt;function&lt;/span&gt; (&lt;span class=&quot;hljs-params&quot;&gt;n&lt;/span&gt;) {
    &lt;span class=&quot;hljs-keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;hljs-title function_&quot;&gt;f&lt;/span&gt;(f)(n);
  });
});
&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;Step 9&lt;/h3&gt;
&lt;p&gt;Almost there! At this point we can also inline &lt;code&gt;recur()&lt;/code&gt; itself:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-javascript&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;var&lt;/span&gt; fact_core = &lt;span class=&quot;hljs-keyword&quot;&gt;function&lt;/span&gt; (&lt;span class=&quot;hljs-params&quot;&gt;g&lt;/span&gt;) {
  &lt;span class=&quot;hljs-comment&quot;&gt;// same as above&lt;/span&gt;
  &lt;span class=&quot;hljs-keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;function&lt;/span&gt; (&lt;span class=&quot;hljs-params&quot;&gt;n&lt;/span&gt;) {
    &lt;span class=&quot;hljs-keyword&quot;&gt;if&lt;/span&gt; (n &amp;lt; &lt;span class=&quot;hljs-number&quot;&gt;2&lt;/span&gt;) &lt;span class=&quot;hljs-keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;;
    &lt;span class=&quot;hljs-keyword&quot;&gt;return&lt;/span&gt; n * &lt;span class=&quot;hljs-title function_&quot;&gt;g&lt;/span&gt;(n - &lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;);
  };
};

&lt;span class=&quot;hljs-keyword&quot;&gt;var&lt;/span&gt; fact = (&lt;span class=&quot;hljs-keyword&quot;&gt;function&lt;/span&gt; (&lt;span class=&quot;hljs-params&quot;&gt;g&lt;/span&gt;) {
  &lt;span class=&quot;hljs-comment&quot;&gt;// the inlined recur() function&lt;/span&gt;
  &lt;span class=&quot;hljs-keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;hljs-title function_&quot;&gt;g&lt;/span&gt;(g);
})(&lt;span class=&quot;hljs-keyword&quot;&gt;function&lt;/span&gt; (&lt;span class=&quot;hljs-params&quot;&gt;f&lt;/span&gt;) {
  &lt;span class=&quot;hljs-comment&quot;&gt;// same as above; now an argument to the inlined recur()&lt;/span&gt;
  &lt;span class=&quot;hljs-keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;hljs-title function_&quot;&gt;fact_core&lt;/span&gt;(&lt;span class=&quot;hljs-keyword&quot;&gt;function&lt;/span&gt; (&lt;span class=&quot;hljs-params&quot;&gt;n&lt;/span&gt;) {
    &lt;span class=&quot;hljs-keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;hljs-title function_&quot;&gt;f&lt;/span&gt;(f)(n);
  });
});
&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;Step 10&lt;/h3&gt;
&lt;p&gt;Finally, we can turn the helper code into a standalone function (i.e.
eliminating the reference to &lt;code&gt;fact_core&lt;/code&gt; in the body of Step 9’s &lt;code&gt;fact&lt;/code&gt;
by converting it into an argument), which we’ll call &lt;code&gt;Y()&lt;/code&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-javascript&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;var&lt;/span&gt; Y = &lt;span class=&quot;hljs-keyword&quot;&gt;function&lt;/span&gt; (&lt;span class=&quot;hljs-params&quot;&gt;h&lt;/span&gt;) {
  &lt;span class=&quot;hljs-keyword&quot;&gt;return&lt;/span&gt; (&lt;span class=&quot;hljs-keyword&quot;&gt;function&lt;/span&gt; (&lt;span class=&quot;hljs-params&quot;&gt;f&lt;/span&gt;) {
    &lt;span class=&quot;hljs-keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;hljs-title function_&quot;&gt;f&lt;/span&gt;(f);
  })(&lt;span class=&quot;hljs-keyword&quot;&gt;function&lt;/span&gt; (&lt;span class=&quot;hljs-params&quot;&gt;f&lt;/span&gt;) {
    &lt;span class=&quot;hljs-keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;hljs-title function_&quot;&gt;h&lt;/span&gt;(&lt;span class=&quot;hljs-keyword&quot;&gt;function&lt;/span&gt; (&lt;span class=&quot;hljs-params&quot;&gt;n&lt;/span&gt;) {
      &lt;span class=&quot;hljs-keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;hljs-title function_&quot;&gt;f&lt;/span&gt;(f)(n);
    });
  });
};

&lt;span class=&quot;hljs-keyword&quot;&gt;var&lt;/span&gt; fact_core = &lt;span class=&quot;hljs-keyword&quot;&gt;function&lt;/span&gt; (&lt;span class=&quot;hljs-params&quot;&gt;g&lt;/span&gt;) {
  &lt;span class=&quot;hljs-comment&quot;&gt;// same as above&lt;/span&gt;
  &lt;span class=&quot;hljs-keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;function&lt;/span&gt; (&lt;span class=&quot;hljs-params&quot;&gt;n&lt;/span&gt;) {
    &lt;span class=&quot;hljs-keyword&quot;&gt;if&lt;/span&gt; (n &amp;lt; &lt;span class=&quot;hljs-number&quot;&gt;2&lt;/span&gt;) &lt;span class=&quot;hljs-keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;;
    &lt;span class=&quot;hljs-keyword&quot;&gt;return&lt;/span&gt; n * &lt;span class=&quot;hljs-title function_&quot;&gt;g&lt;/span&gt;(n - &lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;);
  };
};

&lt;span class=&quot;hljs-keyword&quot;&gt;var&lt;/span&gt; fact = &lt;span class=&quot;hljs-title function_&quot;&gt;Y&lt;/span&gt;(fact_core);
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Hurrah! We have finally achieved Y Combinator:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-node-repl&quot;&gt;&lt;span class=&quot;hljs-meta prompt_&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;language-javascript&quot;&gt;&lt;span class=&quot;hljs-title function_&quot;&gt;fact&lt;/span&gt;(&lt;span class=&quot;hljs-number&quot;&gt;5&lt;/span&gt;)&lt;/span&gt;
120
&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;Notes and Observations&lt;/h2&gt;
&lt;p&gt;Neither the function &lt;code&gt;Y()&lt;/code&gt; nor &lt;code&gt;fact_core()&lt;/code&gt; are self-referential—these
names do not appear anywhere in the code. A lot of other functions are
involved, but since they’re all anonymous, they can’t refer to
themselves either. Hence, the Y Combinator can be used to implement
self-referential (i.e. recursive) programs in languages that don’t
otherwise support it.&lt;/p&gt;
&lt;p&gt;The function &lt;code&gt;fact()&lt;/code&gt; is technically the “&lt;a href=&quot;http://en.wikipedia.org/wiki/Fixed_point_combinator&quot;&gt;fixed
point&lt;/a&gt;” of the
function &lt;code&gt;fact_core()&lt;/code&gt; and the Y Combinator is a function for finding
such fixed points. The fixed point &lt;em&gt;p&lt;/em&gt; of a function &lt;em&gt;f&lt;/em&gt; is a function
that satisfies the condition &lt;em&gt;f(p) = p&lt;/em&gt;. What this actually means is
somewhat hard to get one’s head around (see the &lt;a href=&quot;http://en.wikipedia.org/wiki/Fixed_point_combinator&quot;&gt;Wikipedia
page&lt;/a&gt; for more),
but it’s easy to check that &lt;code&gt;fact_core()&lt;/code&gt; and &lt;code&gt;fact()&lt;/code&gt; do have these
properties:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-node-repl&quot;&gt;&lt;span class=&quot;hljs-meta prompt_&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;language-javascript&quot;&gt;&lt;span class=&quot;hljs-title function_&quot;&gt;fact_core&lt;/span&gt;(fact)(&lt;span class=&quot;hljs-number&quot;&gt;5&lt;/span&gt;);&lt;/span&gt;
120
&lt;span class=&quot;hljs-meta prompt_&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;language-javascript&quot;&gt;&lt;span class=&quot;hljs-title function_&quot;&gt;fact&lt;/span&gt;(&lt;span class=&quot;hljs-number&quot;&gt;5&lt;/span&gt;)&lt;/span&gt;
120
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This means that the following expressions are all (mathematically)
equivalent to the function &lt;code&gt;fact&lt;/code&gt;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;Y(fact_core)&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;fact_core(Y(fact_core))&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;fact_core(fact)&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;fact_core(fact_core(fact_core(Y(fact_core))))&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;References&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Wikipedia’s page on the &lt;a href=&quot;http://en.wikipedia.org/wiki/Fixed_point_combinator&quot;&gt;fixed point
combinator&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://igstan.ro/posts/2010-12-01-deriving-the-y-combinator-in-7-easy-steps.html&quot;&gt;Deriving the Y Combinator in 7 Easy
Steps&lt;/a&gt;
– JavaScript&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://blog.jcoglan.com/2008/01/10/deriving-the-y-combinator/&quot;&gt;Deriving the Y
Combinator&lt;/a&gt;
– JavaScript&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://www.dreamsongs.com/NewFiles/WhyOfY.pdf&quot;&gt;The Why of Y&lt;/a&gt; (PDF)
– Richard P. Gabriel; Scheme&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://matt.might.net/articles/implementation-of-recursive-fixed-point-y-combinator-in-javascript-for-memoization/&quot;&gt;Fixed-point combinators in JavaScript: Memoizing recursive
functions&lt;/a&gt;
– JavaScript&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://php100.wordpress.com/2009/04/13/php-y-combinator/&quot;&gt;Y-Combinator in
PHP&lt;/a&gt; – PHP&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://nerderati.com/2009/11/the-php-5-3-y-combinator/&quot;&gt;The PHP 5.3
Y-Combinator&lt;/a&gt;
– PHP&lt;/li&gt;
&lt;/ul&gt;
</content>
  </entry>
</feed>
