<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">

  <title>fmdkdd</title>
  <link href="https://0xc0de.fr/atom.xml" rel="self"/>
  <link href="https://0xc0de.fr/"/>
  <updated>2023-01-11T21:14:01+00:00</updated>
  <id>https://0xc0de.fr/</id>
  <author>
	 <name>Florent Marchand de Kerchove de Denterghem</name>
	 <email>fmdkdd@0xc0de.fr</email>
  </author>

  
  <entry>
	 <title>A Few Emacs Tricks</title>
	 <link href="https://0xc0de.fr/2019/07/23/a-few-emacs-tricks.html"/>
	 <updated>2019-07-23T00:00:00+00:00</updated>
	 <id>https://0xc0de.fr/2019/07/23/a-few-emacs-tricks</id>
	 <content type="html">&lt;p&gt;Here are some Emacs Lisp stuff I have in my configuration, which others may find
useful.  I don’t want to publish them on MELPA, because I have no intention
to maintain them.  They probably contain bugs, haven’t been tested thoroughly
etc.  Feel free to grab them if they fit your needs, but don’t complain if they
don’t work!&lt;/p&gt;

&lt;h3 id=&quot;automatically-reload-the-browser-when-editing-html&quot;&gt;Automatically reload the browser when editing HTML&lt;/h3&gt;

&lt;p&gt;When I do web programming in HTML/CSS/JS, I like to have a browser to test my
changes quickly.  Switching to the browser and refreshing it got old rather
quickly, so I scripted it.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/fmdkdd/dotfiles/blob/master/emacs/.emacs.d/elisp/reload-browser.el&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;reload-browser.el&lt;/code&gt;&lt;/a&gt; provides a function which reloads multiple browser
windows.  The first time you call it, it turns your cursor into a box, asking
you to click in the window you want to reload.  After that, calling the function
again will reload the window you selected.  You can even select multiple windows
(e.g., Firefox and Chrome) if you pass a numeric prefix argument.  With a
keybinding to save the current buffer and reload all browser windows, I don’t
need a more fancy setup.&lt;/p&gt;

&lt;p&gt;Behind the scenes it simply calls &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;xdotool&lt;/code&gt; to do the heavy lifting.  It’s
mostly straightforward, but it turns out that Chromium requires to be in focus
to be reloaded, whereas Firefox does not, so the script has to handle that as
well.&lt;/p&gt;

&lt;h3 id=&quot;one-sentence-per-line-for-editing-shared-latex&quot;&gt;One sentence per line for editing shared LaTeX&lt;/h3&gt;

&lt;p&gt;I usually write text with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;auto-fill-mode&lt;/code&gt;, which wraps lines at 80 characters
so the result is readable in any text editor.  When writing papers in LaTeX
though, I want to keep one sentence per line, as it makes reviewing the diffs
and handling conflicts much easier when working with other people.  Of course, I
want my editor to fill that way automatically.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/fmdkdd/dotfiles/blob/master/emacs/.emacs.d/elisp/ospl.el&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ospl.el&lt;/code&gt;&lt;/a&gt; redefines &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;auto-fill-function&lt;/code&gt; to break after a sentence, and
provides an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ospl-paragraph&lt;/code&gt; function which refills the current paragraph to one
sentence per line.  The trick here is to detect where sentences end so you can
insert newlines.  Emacs can recognize sentences by looking for a colon, but note
that abbreviated acronyms also end by colons (“e.g.”, “i.e.”).  The best way to
handle this unambiguously is to use a double space after a sentence, and a
single space after other colons (see the variable &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sentence-end-double-space&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;Unfortunately, not everyone has recognized that the double space is the superior
form, so &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ospl&lt;/code&gt; assumes you use a single space, and avoids breaking after common
acronyms, which makes it slightly more complex than it should be.&lt;/p&gt;

&lt;h3 id=&quot;preview-definitions-when-editing-assembly&quot;&gt;Preview definitions when editing assembly&lt;/h3&gt;

&lt;p&gt;Lately I’ve been dabbling in 6502 assembly, and when navigating code, I often
found the need to look at the definition for the label under point.  So I wrote
an xref backend for assembly (&lt;a href=&quot;https://github.com/fmdkdd/dotfiles/blob/master/emacs/.emacs.d/elisp/xref-asm.el&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;xref-asm.el&lt;/code&gt;&lt;/a&gt;), so that I could jump to the
definition and back using the standard keybindings.  But for most labels, I
didn’t really need to jump to the definition, I only needed to see a couple of
lines at or below the label, so I could remember what it was doing.&lt;/p&gt;

&lt;p&gt;So I wrote &lt;a href=&quot;https://github.com/fmdkdd/dotfiles/blob/master/emacs/.emacs.d/elisp/xref-posframe.el&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;xref-posframe.el&lt;/code&gt;&lt;/a&gt;, another package which &lt;em&gt;previews&lt;/em&gt; the
definition using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;posframe&lt;/code&gt;.  Posframe lets you create one-shot Emacs frames
without window decorations, which is more convenient and configurable than
overlays for displaying stuff on top of the current buffer, but still fast
enough to be usable.  With xref-posframe, I can preview a definition in one
key:&lt;/p&gt;

&lt;video autoplay=&quot;&quot; loop=&quot;&quot;&gt;
  &lt;source src=&quot;/img/posts/xref-asm.webm&quot; type=&quot;video/webm&quot; /&gt;
&lt;/video&gt;

&lt;p&gt;Pressing the key a second time, I can even jump to the definition.&lt;/p&gt;

&lt;p&gt;And it works everywhere xref is supported, not just for assembly.&lt;/p&gt;

&lt;h3 id=&quot;highlight-todo-keywords&quot;&gt;Highlight TODO keywords&lt;/h3&gt;

&lt;p&gt;This one is pretty simple.  I want keywords in comments like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TODO:&lt;/code&gt; and
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FIXME:&lt;/code&gt; to use a custom font, so I can see them pop out at me, screaming to be
fixed.  This can be done by adding keywords to font lock:&lt;/p&gt;

&lt;div class=&quot;language-emacs-lisp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;defun&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;add-watchwords&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
  &lt;span class=&quot;s&quot;&gt;&quot;Add TODO: words to font-lock keywords.&quot;&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;font-lock-add-keywords&lt;/span&gt;
   &lt;span class=&quot;no&quot;&gt;nil&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;\\(\\&amp;lt;TODO\\|\\&amp;lt;FIXME\\|\\&amp;lt;HACK\\|@.+\\):&quot;&lt;/span&gt;
          &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;font-lock-warning-face&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))))&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;add-hook&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;'prog-mode-hook&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;#'&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;add-watchwords&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;img src=&quot;/img/posts/todo-highlight.png&quot; alt=&quot;Rust snippet with a TODO keyword in a comment highlighted in bold&quot; /&gt;&lt;/p&gt;

&lt;p&gt;This method has one drawback: it highlights these words &lt;em&gt;anywhere&lt;/em&gt; they appear
in a programming buffer, even outside of comments.  There are complex packages
that do this in a more robust way, making sure to only highlight these words in
comments.  I don’t think these are worth the added complexity.  For me, 99.99%
of the time the method above works fine.  When there is a false positive, I just
ignore it.&lt;/p&gt;

&lt;p&gt;Lately I’ve been using more descriptive keywords like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@Correctness:&lt;/code&gt; or
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@Optimize:&lt;/code&gt;, so I’ve added a wildcard at the end to handle all words between
“&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@&lt;/code&gt;” and “&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:&lt;/code&gt;”.  Even then, false positives are not an issue.&lt;/p&gt;

&lt;h3 id=&quot;insert-screenshots-into-org-documents&quot;&gt;Insert screenshots into Org documents&lt;/h3&gt;

&lt;p&gt;I take a lot of notes on what I’m working on in Org.  Whenever I’m working on
something that can be visualized, I prefer to include screenshots as they are
often more helpful in conveying what’s going on.  To reduce the friction of
taking the screenshot, cropping it, and inserting the link into the Org file,
I’ve written a bit of Elisp to do it for me:&lt;/p&gt;

&lt;div class=&quot;language-emacs-lisp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;defun&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;insert-screenshot&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;file-name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;s&quot;&gt;&quot;Save screenshot to FILE-NAME and insert an Org link at point.

This calls the `import' from ImageMagick to take the screenshot,
and `optipng' to reduce the file size if the program is present.&quot;&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;interactive&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;FSave to file: &quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;;; Get absolute path&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;file&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;expand-file-name&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;file-name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;;; Create the directory if necessary&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;make-directory&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;file-name-directory&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;file&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;'parents&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;;; Still, make sure to signal if the screenshot was in fact not created&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;unless&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;call-process&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;import&quot;&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;nil&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;nil&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;nil&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;file&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;user-error&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;`import' failed to create screenshot %s&quot;&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;file&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;executable-find&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;optipng&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;start-process&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;optipng&quot;&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;nil&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;optipng&quot;&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;file&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;insert&lt;/span&gt;
     &lt;span class=&quot;c1&quot;&gt;;; A link relative to the buffer where it is inserted is more portable&lt;/span&gt;
     &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;format&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;[[file:%s]]&quot;&lt;/span&gt;
             &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;file-relative-name&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;file&lt;/span&gt;
                                 &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;file-name-directory&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;buffer-file-name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))))&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;when&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;eq&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;major-mode&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;'org-mode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;org-redisplay-inline-images&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This snippet asks for a filename, then calls &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;import&lt;/code&gt; to select just the area I
want to screenshot, saves it and then insert an org-format image link at point.
In org-mode, it redisplays inline images so I can see the result right away.  If
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;optipng&lt;/code&gt; is installed, it even compresses the screenshot to save space.&lt;/p&gt;

&lt;h3 id=&quot;insert-delete-or-change-delimiters&quot;&gt;Insert, delete or change delimiters&lt;/h3&gt;

&lt;p&gt;In Spacemacs and Vim, there’s a nifty way to quickly surround a part of text
with quotes, braces, and other delimiters.  When I went back to vanilla Emacs,
adding quotes by hand seemed tedious.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/fmdkdd/dotfiles/blob/master/emacs/.emacs.d/elisp/delimiter.el&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delimiter.el&lt;/code&gt;&lt;/a&gt; solves this.  It’s very straightforward: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delimiter-surround&lt;/code&gt;
will prompt for a delimiter char and surround the current region with it.  If no
region is active, it surrounds the current sexp.  So in two quick key presses, I
can quote a word.  Some delimiters go in pairs, so giving an opening brace “&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{&lt;/code&gt;”
will use the matching closing brace “&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;}&lt;/code&gt;” as closing delimiter.&lt;/p&gt;

&lt;p&gt;There is also &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delimiter-change&lt;/code&gt; to change an existing delimiter pair to
something else, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delimiter-delete&lt;/code&gt; which is self-explanatory.&lt;/p&gt;

&lt;p&gt;Note that the deletion is purposefully &lt;em&gt;not&lt;/em&gt; clever: if you have nested
delimiters, it will just delete the ones closest to point, so in the following
situation (the caret &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;|&lt;/code&gt; marks point):&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;(he|re (is) a list)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;invoking &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;M-x delimiter-delete ( RET&lt;/code&gt; will yield:&lt;/p&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;he|re (is a list)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;where one may expect to eat the outermost parens:&lt;/p&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;he|re (is) a list
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;If you want clever behavior, you should look elsewhere.&lt;/p&gt;

&lt;h3 id=&quot;auto-revert-without-the-lag&quot;&gt;Auto-revert without the lag&lt;/h3&gt;

&lt;p&gt;Just yesterday I wanted to explore the assembly output of GCC for some simple C
file.  I opened two windows: one with the C code, and one with the output
assembly.  I put the latter in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;auto-revert-mode&lt;/code&gt; so I could quickly see the
result after recompilation.&lt;/p&gt;

&lt;p&gt;Turns out, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;auto-revert-mode&lt;/code&gt; had some weird lag on my machine, where it could
take a few seconds to revert the buffer so I could see the result.  Opening a
terminal with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;watch -n 0.1 cat a.s&lt;/code&gt; was better.&lt;/p&gt;

&lt;p&gt;I wrote a small minor mode that reverts the buffer after getting change events
from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;inotify&lt;/code&gt;, and it was constantly instantaneous.&lt;/p&gt;

&lt;p&gt;Later, I checked out the source to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;auto-revert-mode&lt;/code&gt;, and found that it does
more or less the same as my minor mode, using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;filenotify&lt;/code&gt; watchers first, and
falling back on polling if filenotify isn’t supported or fails.  Yet, it can
still take seconds to refresh whereas my minor mode just works.&lt;/p&gt;

&lt;p&gt;If you have the same issue with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;auto-revert-mode&lt;/code&gt;, you may want to try
&lt;a href=&quot;https://github.com/fmdkdd/dotfiles/blob/master/emacs/.emacs.d/elisp/inotify-revert.el&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;inotify-revert.el&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;

</content>
  </entry>
  
  <entry>
	 <title>Ludum Dare 43 Post-Mortem</title>
	 <link href="https://0xc0de.fr/2018/12/21/ld43-post-mortem.html"/>
	 <updated>2018-12-21T00:00:00+00:00</updated>
	 <id>https://0xc0de.fr/2018/12/21/ld43-post-mortem</id>
	 <content type="html">&lt;p&gt;&lt;img src=&quot;/img/posts/ld43-cover.png&quot; alt=&quot;Queltris&quot; /&gt;&lt;/p&gt;

&lt;p&gt;We made another game for the latest Ludum Dare game jam.  It’s called QUELTRIS,
and you can play it &lt;a href=&quot;https://0xc0de.fr/ld43&quot;&gt;right here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;In this post I want to describe the game design process: how we came up with the
idea, how the initial idea wasn’t fun at all, and how we turned that around to
find something that was worth playing.&lt;/p&gt;

&lt;h3 id=&quot;the-idea&quot;&gt;The idea&lt;/h3&gt;
&lt;p&gt;The theme this time was &lt;em&gt;Sacrifices must be made&lt;/em&gt;.  We started by brainstorming
around that theme, trying to find an interesting setting and a gameplay idea
that would fit.  We took the theme rather literally and quickly gravitated
around the idea of an Aztec god asking you to sacrifice pawns.&lt;/p&gt;

&lt;p&gt;On the gameplay side, I suggested to do a match-3 (think &lt;em&gt;Bejeweled&lt;/em&gt;).  Partly
because we had been playing &lt;em&gt;PuyoPuyo vs. Tetris&lt;/em&gt; the night before, and partly
because I thought it was an easier target for the allotted 72 hours.  In
&lt;a href=&quot;/yatm&quot;&gt;Münster&lt;/a&gt;, not only the game was &lt;em&gt;much shorter&lt;/em&gt; than envisioned, but the
gameplay felt wonky due to falling short on time.  For &lt;a href=&quot;/ld39&quot;&gt;Bright Plateau&lt;/a&gt; we did
manage to make a decent-sized game (for a Ludum Dare), but as I designed the
levels the last day, we didn’t know how fun the game would end up being until a
few hours before the deadline.  This time, we focused on getting a good gameplay
first.&lt;/p&gt;

&lt;div style=&quot;margin: 1rem auto&quot;&gt;
&lt;img src=&quot;/img/posts/ld43-bejeweled.png&quot; style=&quot;width:323px; display: inline; margin: 0;&quot; alt=&quot;Bejeweled&quot; /&gt;
&lt;img src=&quot;/img/posts/ld43-fps-tetris.png&quot; style=&quot;width:323px; display: inline; margin: 0;&quot; alt=&quot;Block Out&quot; /&gt;
&lt;img src=&quot;/img/posts/ld43-qbqbqb.png&quot; style=&quot;display: inline; margin: 0;&quot; alt=&quot;QbQbQb&quot; /&gt;
&lt;/div&gt;

&lt;p&gt;Of course, we didn’t want to just do a &lt;em&gt;Bejeweled&lt;/em&gt; clone, so we sketched some
ideas on how to iterate on the formula in an original way: maybe using a
different swap mechanism?  Or changing the grid?  We looked at &lt;em&gt;QbQbQb&lt;/em&gt;, a game
where you put pieces around a planet, and at &lt;em&gt;Block Out&lt;/em&gt;, a first-person
&lt;em&gt;Tetris&lt;/em&gt; that has you looking down the well in 3D as a tetromino.&lt;/p&gt;

&lt;p&gt;Trying to sketch how a circle grid would work for us, Merwan had an idea: what
if we had a round, bottomless well, and the player would sacrifice pieces by
push them down the well?  We both instantly loved that idea: it was a simple
mechanism (push) that perfectly fitted the theme (sacrifice).&lt;/p&gt;

&lt;p&gt;Now, at this point we didn’t know if the idea was enough.  We had a good idea,
but needed to see it in motion.  I started making a playable prototype while
Merwan began working on the visuals.&lt;/p&gt;

&lt;h3 id=&quot;the-prototype&quot;&gt;The prototype&lt;/h3&gt;
&lt;p&gt;At 21:19 on the first day we had a playable prototype:&lt;/p&gt;

&lt;video controls=&quot;&quot;&gt;
  &lt;source src=&quot;/img/posts/ld43-push-proto.webm&quot; type=&quot;video/webm&quot; /&gt;
&lt;/video&gt;

&lt;p&gt;The player moves the white triangle at the top to switch columns, and can push
the current column down in order to match three of more pieces of the same
color.  Every move consumes blood from the gauge, while making matches fills it
back.  When the gauge is empty, you lose.&lt;/p&gt;

&lt;p&gt;The first thing to note is that it is nearly a complete game.  In that sense,
our focus on prototyping was a success.  Adding a little polish here and there,
and we could have submitted it!  Needless to say, we felt pretty good about our
progress.&lt;/p&gt;

&lt;p&gt;Except for one thing: the game wasn’t fun.  &lt;em&gt;At all&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;In the video above I’m just pressing buttons at random, without thinking about
whether my move will make a match.  I don’t need to: most moves &lt;em&gt;will&lt;/em&gt; make a
match.  Since we are moving a whole column down, 7 pieces change position,
making 7 places for a potential match.  As I keep pressing the down button, I
make many matches, just at random.  The game does not challenge me, so I’m not
&lt;em&gt;engaging&lt;/em&gt; with the game.  It’s just not fun.&lt;/p&gt;

&lt;p&gt;The blood gauge was actually an addition I made after finding out that the push
mechanism was simply not enough.  Adding the gauge was supposed to force the
player to consider each move, but as matches were easily made, the balance was
still off.&lt;/p&gt;

&lt;p&gt;We were in a crisis.  We had a good idea, or a least we &lt;em&gt;thought&lt;/em&gt; we did, but it
wasn’t fun.  How could we salvage it?&lt;/p&gt;

&lt;h3 id=&quot;finding-the-fun&quot;&gt;Finding the fun&lt;/h3&gt;
&lt;p&gt;We can feel when a game isn’t fun, but how do we add fun back in?
How do we improve the mechanisms?&lt;/p&gt;

&lt;p&gt;A first step is to understand why the game isn’t fun in the first place.  Here,
I feel the game is lacking meaningful choices.  At any given time, the player
has three moves at his disposal: move left, move right, and push the column
down.  Now, a low number of low-level moves isn’t necessarily a bad thing; some
games manage with as many, or even fewer.  What is more important is what
&lt;em&gt;decisions&lt;/em&gt; these moves enable.  There are seven columns, and the player has to
choose one column to push down in order to progress; that’s exactly seven
choices at a given time.  Seven is a low number of alternatives to weight, which
might make the game trivial.  But worse: actually weighing these alternatives is
pointless, since matches come so easily.  You can pick any column, push it down
a few times without thinking about it, and you will probably make a match.  If
you don’t, just move to the next column.  Even if you try to play thoughtfully,
looking for matches is trivial.  Since we have too few interesting decisions to
make, the game ends up feeling boring.&lt;/p&gt;

&lt;p&gt;Doubt cast over our enthusiasm.  After grabbing a bite, we looked at how other
games with similar mechanisms managed to be fun.  &lt;em&gt;Tetris&lt;/em&gt; mixes two levels of
decisions: at the tactical level you have to move the falling pieces where they
won’t create too many holes, and at the strategical level you have to think
about leaving space for an eventual ‘I’ piece.  &lt;em&gt;PuyoPuyo&lt;/em&gt; has the same depth,
but here the strategy is about scaffolding for combos long, juicy combos.  In
both these games, the player is constantly trying to clear the well while the
game keeps throwing more stuff to handle, which creates a nice tension.  In
&lt;em&gt;Bejeweled&lt;/em&gt; and &lt;em&gt;Hexic&lt;/em&gt; the tension arises by creating matches while managing
the random pieces coming from the top.  Both also have strategic aspects:
&lt;em&gt;Bejeweled&lt;/em&gt; lets you setup combos for more points, and &lt;em&gt;Hexic&lt;/em&gt; requires you to
build “flowers” patterns in order to win.&lt;/p&gt;

&lt;div style=&quot;margin: 1rem auto&quot;&gt;
&lt;img src=&quot;/img/posts/ld43-tetris-nes.png&quot; style=&quot;width:323px; display: inline; margin: 0;&quot; alt=&quot;Tetris&quot; /&gt;
&lt;img src=&quot;/img/posts/ld43-candy-crisis.jpg&quot; style=&quot;width:323px; display: inline; margin: 0;&quot; alt=&quot;Candy Crisis&quot; /&gt;
&lt;img src=&quot;/img/posts/ld43-hexic.jpg&quot; style=&quot;width: 323px; margin: 0 auto;&quot; alt=&quot;Hexic&quot; /&gt;
&lt;/div&gt;

&lt;p&gt;Other games succeed at using match-3 mechanism on the tactical level, but mixing
that with another genre altogether at the strategic level.  In &lt;em&gt;Might and Magic:
Clash of Heroes&lt;/em&gt; the strategic level is about managing your pieces to launch
attacks at your opponent while keeping enough pieces around to defend yourself.
In &lt;em&gt;Puzzle Quest&lt;/em&gt;, the match-3 mechanism is greatly enhanced by the
character-building aspects taken from role-playing games.  &lt;em&gt;Henry Hatsworth in
the Puzzling Adventure&lt;/em&gt; mixes match-3 and platformers, while &lt;em&gt;10000000&lt;/em&gt; uses
match-3 at the tactical level to power a dungeon crawler.  Although combining
genres this way can make for an excellent game, we wanted a simpler game .
After all, &lt;em&gt;Tetris&lt;/em&gt; is fun enough on its own!&lt;/p&gt;

&lt;div style=&quot;margin: 1rem auto&quot;&gt;
&lt;img src=&quot;/img/posts/ld43-clash-of-heroes.jpg&quot; style=&quot;width:214px; display: inline; margin: 0;&quot; alt=&quot;Might and Magic: Clash of Heroes&quot; /&gt;
&lt;img src=&quot;/img/posts/ld43-hatsworth.jpg&quot; style=&quot;width: 214px; display: inline; margin: 0&quot; alt=&quot;Henry Hatsworth in the Puzzling Adventure&quot; /&gt;
&lt;img src=&quot;/img/posts/ld43-10000000.jpg&quot; style=&quot;width: 214px; display: inline; margin: 0&quot; alt=&quot;10000000&quot; /&gt;
&lt;img src=&quot;/img/posts/ld43-puzzle-quest.png&quot; style=&quot;margin: 0&quot; alt=&quot;Puzzle Quest&quot; /&gt;
&lt;/div&gt;

&lt;p&gt;To make the game more fun, we tried to expand the decisions the player had to
make by allowing pushing from the sides in order to match.  To avoid committing
too much time pursuing these variants, we built paper prototypes.  But even with
more options, the game still felt too easy and uninteresting.&lt;/p&gt;

&lt;p&gt;Feeling we had exhausted the search space, Merwan suggested we rotate pieces
instead of pushing them.  This was an intriguing twist, but it also meant that
we would lose the “push pawns down a well” aspect of the push mechanism which so
lovely tied into the theme.  Still, it’s better to have a fun game that loosely
fits the theme than the reverse, so we pursued it.&lt;/p&gt;

&lt;h3 id=&quot;turning-it-around&quot;&gt;Turning it around&lt;/h3&gt;
&lt;p&gt;I went back to add rotation to the prototype.  After two hours, I had rotation
working, complete with animations.  There was no matching; just rotation.  But
the rotation already felt &lt;em&gt;fun&lt;/em&gt;.  I spent quite a bit of time just toying with
it, which is always a good sign:&lt;/p&gt;

&lt;video controls=&quot;&quot;&gt;
  &lt;source src=&quot;/img/posts/ld43-rotate-proto.webm&quot; type=&quot;video/webm&quot; /&gt;
&lt;/video&gt;

&lt;p&gt;One reason it’s fun is that you have many more options at your disposal: you can
go anywhere in the grid and rotate four pieces right or left.  It turns out that
this is enough to re-arrange pieces in any shape (except for the fringe pieces).
This observation led us to the idea of matching “runes”: patterns of colors.
You would rotate pieces in order to build these patterns and satisfy the angry
god.&lt;/p&gt;

&lt;p&gt;Initially we envisioned large runes: letters-like shapes around 4x5 or even 6x6.
We could have made the grid larger to accomodate that, but even before
implementing the pattern matching we thought it would be more satisfying to
chain smaller patterns rather than build towards larger ones.  Large patterns
require more moves, and felt more tedious to build.&lt;/p&gt;

&lt;p&gt;Now, there only so many shapes you can make in a 2x3 grid, so we ended up with
the familiar tetrominoes.  At first, I tried to be original and had the green
pattern be the following:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;OO.
O.O
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;But it turns out that a disconnected pattern like this is harder to recognize
on the board.  It also made sense to have shapes with the same number of
squares, in order to make sure they have the same probability being assembled on
the grid.  This also cut down on the possibilities.  In the end, the game leans
on the player’s familiarity with the tetrominoes, but you still have to get used
to recognize situations that can make a pattern in one or two moves.&lt;/p&gt;

&lt;p&gt;When I tried out the prototype, it had a great feel.  Just at the tactical
level, the player has interesting options.  Do I go for the blue shape that’s a
bit further, or do I try to assemble these four yellow squares lying around?  Oh
wait, now I’m seeing that this pattern of red squares is actually one move away
from a match.  It was fun just to look for near-match configurations.  As you
learn to recognize these configurations for all the patterns, you see potential
matches everywhere.&lt;/p&gt;

&lt;video controls=&quot;&quot;&gt;
  &lt;source src=&quot;/img/posts/ld43-combos.webm&quot; type=&quot;video/webm&quot; /&gt;
&lt;/video&gt;

&lt;h3 id=&quot;emergent-gameplay&quot;&gt;Emergent gameplay&lt;/h3&gt;
&lt;p&gt;I’m always fascinated when the combination of simple mechanisms creates
unanticipated and interesting gameplay opportunities.  So when I discovered that
our game had some, I was rather excited.&lt;/p&gt;

&lt;p&gt;There is balance between the colors that naturally emerges from the rules.  If
you focus on matching the red shapes, you will have less red and more of the
other three colours.  Thus, you are forced to match other shapes, pulling you
out of your comfort zone.  This balance arises in other match-3 games as well,
as long as all colors have equal spawning chances.&lt;/p&gt;

&lt;p&gt;But playing with the prototype also revealed rich opportunities at the strategic
layer.  I categorized four of them: multi-matches, overlaps, combos and chains.&lt;/p&gt;

&lt;p&gt;A multi-pattern match (or multi-match) is when one move matches two or more
different patterns:&lt;/p&gt;

&lt;video style=&quot;width: 300px&quot; controls=&quot;&quot; loop=&quot;&quot;&gt;
  &lt;source src=&quot;/img/posts/ld43-multi-pattern.webm&quot; type=&quot;video/webm&quot; /&gt;
&lt;/video&gt;

&lt;p&gt;A multi-match can be tricky to setup, as you have to be careful of not matching
the pattern prematurely while you move pieces around.  Though messing up a
multi-match can be comical, pulling one off is rewarding.  It’s also fun to
figure out the different ways to build them, and to learn the configurations
leading to them.&lt;/p&gt;

&lt;p&gt;An overlap is when one move matches two or more instances of the same
pattern:&lt;/p&gt;

&lt;video style=&quot;width: 300px&quot; controls=&quot;&quot; loop=&quot;&quot;&gt;
  &lt;source src=&quot;/img/posts/ld43-overlap.webm&quot; type=&quot;video/webm&quot; /&gt;
&lt;/video&gt;

&lt;p&gt;Overlaps are quite rewarding to build for the same reasons that make
multi-matches fun.  Overlaps are perhaps easier to setup due to requiring fewer
pieces, and focusing on only one color.&lt;/p&gt;

&lt;p&gt;Combos are pretty much a given, due to borrowing the random top row of pieces
from match-3 games:&lt;/p&gt;

&lt;video style=&quot;width: 300px&quot; controls=&quot;&quot; loop=&quot;&quot;&gt;
  &lt;source src=&quot;/img/posts/ld43-combos2.webm&quot; type=&quot;video/webm&quot; /&gt;
&lt;/video&gt;

&lt;p&gt;Though in the submitted game you don’t really have the time to arrange the grid
in order to build elaborate combos, it’s still satisfying to pull a small one.
Having a combo happen due to the random pieces falling is always nice as well.
Had we had more time, we might have added a puzzle mode where you have to build
the largest combo for a given board.&lt;/p&gt;

&lt;p&gt;Finally, one of the more interesting ways to play the game is to aim for chains.
A chain is a sequence of moves where every rotation you make is a match.  Here
is a 5-chain (starting with a multi-match and ending with a combo):&lt;/p&gt;

&lt;video style=&quot;width: 300px&quot; controls=&quot;&quot; loop=&quot;&quot;&gt;
  &lt;source src=&quot;/img/posts/ld43-chain.webm&quot; type=&quot;video/webm&quot; /&gt;
&lt;/video&gt;

&lt;p&gt;Unfortunately, in the final game we did not have time to signal these different
trick moves to the player explicitly.  Though combos, overlaps and multi-matches
award you bonus points, and the god face reacts to combos, the connection is not
obvious.&lt;/p&gt;

&lt;p&gt;The timer forces you to play fast and safe, which tends to reward chains, but at
the detriment of large overlaps and clever combo scaffolding.  I would have
liked to encourage the player to aim for these tricks more, as just matching the
base patterns feels stale rather quickly.  It’s a flaw that I would like to
address in a remake, but I haven’t found the perfect solution so far.&lt;/p&gt;

&lt;h3 id=&quot;the-takeaway&quot;&gt;The takeaway&lt;/h3&gt;
&lt;p&gt;We initially pursued a simpler ‘push’ mechanism that was a great fit for the
theme, but ended up being too trivial to be fun.  By looking at other games for
inspiration, and by trying different ideas out, we iterated to find a core
mechanism that, although not obviously tied to the them, at least felt good to
play.  From then on, we added visuals and animations to polish the game and
better fit the theme.&lt;/p&gt;

&lt;p&gt;The lesson here is: don’t be afraid to prototype a lot, and don’t be too
attached to seemingly good ideas.&lt;/p&gt;

&lt;p&gt;In the end, we aimed to build a fun take on match-3, and although the game could
be improved to be more engaging, I think we succeeded.&lt;/p&gt;

</content>
  </entry>
  
  <entry>
	 <title>A Personal History of Reading</title>
	 <link href="https://0xc0de.fr/2018/06/19/personal-history-reading.html"/>
	 <updated>2018-06-19T00:00:00+00:00</updated>
	 <id>https://0xc0de.fr/2018/06/19/personal-history-reading</id>
	 <content type="html">&lt;p&gt;I love learning new things, potentially more than &lt;em&gt;knowing&lt;/em&gt; things.  I’m talking
about big, meaningful things: concepts, systems, algorithms.  I’m interested in
understanding how the universe and its pieces work, but not necessarily what you
had for dinner yesterday.&lt;/p&gt;

&lt;p&gt;Much of that precious knowledge I seek, I get from reading.  Nowadays, reading
comes in many different flavors; books of course, but also journals, magazines,
comics, websites, pages, blogs, forums… While some of these differ in purpose,
it’s still pretty much text.  For this particular discussion, it doesn’t matter
if the rays of light that carry the words to your retina come from a computer
monitor, a phone, or a candle.  So, for this post, I will use the word &lt;em&gt;book&lt;/em&gt; to
refer to any word source, because it rings better than “text”.  Book.  Boo-ook.
BOOK!&lt;/p&gt;

&lt;p&gt;This post is about reading books.&lt;/p&gt;

&lt;h3 id=&quot;a-naive-start&quot;&gt;A naive start&lt;/h3&gt;

&lt;p&gt;When I was growing up, I did not have many friends (well I still don’t, but that
may be a topic for another day).  Part of the reason was a shyness bordering on
the pathological, but maybe another cause was that I &lt;em&gt;did&lt;/em&gt; (and still do) enjoy
a quiet solitude.  While other children gleefully sportsballed outside, I
favored playing &lt;em&gt;in my mind&lt;/em&gt; rather than in the physical world.  And until I
found a computer keyboard in my hands, reading was certainly my favorite
activity.&lt;/p&gt;

&lt;p&gt;For the longest time, I believed that the sole act of reading books would make
me smarter.  The reasoning might have went a bit like this:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;books are written by smart folks;&lt;/li&gt;
  &lt;li&gt;their writing distills smart-stuff into words;&lt;/li&gt;
  &lt;li&gt;reading their prose is like drinking smart-full nutrients;&lt;/li&gt;
  &lt;li&gt;like absorbed nutrients fuel my body, absorbing words with my eyes fuels my
brain;&lt;/li&gt;
  &lt;li&gt;thus, reading their words makes me smarter.  QED.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;But, mainly, it might just have been the social stigma that being a book worm is
a prerequisite for a career path into knowledge-oriented jobs.  Being paid to
play in my mind all day seemed like a good value proposition, and so I read.&lt;/p&gt;

&lt;p&gt;Now, while I have mostly outgrown this belief, it has impacted many of my past
reading behavior.  I want to reminisce on some of its most embarrassing
consequences later, but first I want to discuss its hypotheses.&lt;/p&gt;

&lt;h3 id=&quot;finding-the-good-books&quot;&gt;Finding the good books&lt;/h3&gt;

&lt;p&gt;You might object to assumptions #1 and #2, and, in an all my &lt;em&gt;naïveté&lt;/em&gt;, I &lt;em&gt;was&lt;/em&gt;
aware that these did not hold for all literature known to mankind; i.e., not all
books may be worth reading.  Or rather, I suspected that books came in varying
levels of quality, which in turn meant various amounts of knowledge to absorb.
To keep the culinary analogy going, some books are like fast-food: cheap and
full of saturated fat and sugars to give you a short but potent high, while
others are more like &lt;em&gt;légumes&lt;/em&gt;: less thrilling, but more nutritious.  In a
healthy diet, you want less of the former and more of the latter.  &lt;em&gt;Mens sana in
corpore sano&lt;/em&gt; calls for a healthy &lt;em&gt;reading&lt;/em&gt; diet.&lt;/p&gt;

&lt;p&gt;A &lt;a href=&quot;https://what-if.xkcd.com/76/&quot;&gt;quick back-of-the-envelope calculation&lt;/a&gt; shows
that the number of books one can reasonably manage to read in a lifetime has
been eclipsed by the number of books in circulation since around … five
centuries ago.  Clearly, in my eternal quest of wisdom, I would need to devise a
system to part the wheat from the chaff.&lt;/p&gt;

&lt;p&gt;From an early age, I caught on that &lt;em&gt;recommendations&lt;/em&gt; were an effective
solution.  I have a simple formula: the likelihood of me reading a book is
proportional to the number of times that book was recommended to me (directly by
a friend, or indirectly through word of mouth), weighted by the area of overlap
between the recommender’s interests and mine.  In other words, if we both are
passionate about waffles, and you tell me about this book on Belgian wafflology
after the first World War, then the likelihood of me reading it is very high.
On the other hand, if we have diverging interests, then chances are low that I
will get out of my way to find a copy of that &lt;em&gt;Shoelace Makers of the
Renaissance&lt;/em&gt; you keep raving about (even though it does sound a little
intriguing!).  But if I keep hearing about it, I just might get one.  Of course,
other factors come into play, and I do not actually carry out calculations to
rank books that were recommended to me in order to pick my next bedside
companion.  But even loosely applied, I do get good mileage out of this formula;
and it can be extended to other media like films, video games and music.&lt;/p&gt;

&lt;p&gt;Also of importance is the nature of the book.  Some books have explicit
pedagogical value: textbooks, scientific articles, historical treaties,
encyclopedias, user manuals and the like.  It is easy to see how one could want
to download all their factual contents directly into one’s brain.  Other kinds
of books, like fiction, have a different value proposition: they primarily
entertain you with a gripping story.  One may then question the utility of
cramming one’s head with the intricacies of Bridget Jones’ failings at finding
love.  Aside from the trivial observation that, yes, sometimes, it &lt;em&gt;is&lt;/em&gt; good to
be entertained, even by clichés, I would argue that even fictional writings may
hold valuable insights.  Stories have a way of making a mark in our collective
imagery, and their characters can often be used in daily conversations to draw
one’s point home more vividly.  Greek mythology is rife with figures whose
actions gave us useful analogies and even common words: Odysseus’ sirens,
Icarus’ flight, Tantalus’ punishment, Narcissus’ fatal narcissism, &lt;a href=&quot;http://existentialcomics.com/comic/29&quot;&gt;Sisyphus’
Sisyphean plight&lt;/a&gt;, &lt;em&gt;etc.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;All of that to say that while I favor reading technical books because they will
more often hold actionable knowledge, other literary pursuits may still be
worthwhile for expanding my horizons, or even just for fun.&lt;/p&gt;

&lt;h3 id=&quot;reading-habits&quot;&gt;Reading habits&lt;/h3&gt;

&lt;p&gt;Now, having no shortage of wonderful books to discover, I want to reflect on
behaviors that may have been brought about by that forementioned belief.&lt;/p&gt;

&lt;p&gt;I used to read books from front to back, without missing a letter.  Footnotes
and back-cover blurb included.  Carefully sieving each page, in search of
nuggets of wisdom.  I was once appalled at a fellow student who revealed to me
that her so-called speed-reading technique involved skipping &lt;em&gt;whole paragraphs&lt;/em&gt;.
I couldn’t bear the thought of glancing over a sentence in fear of missing out
some important tidbit; skipping paragraphs was heresy.&lt;/p&gt;

&lt;p&gt;Nowadays, I skip by leaps and bounds when the text does not seem to be relevant
to what I’m after.  Though I tend to read fiction at a more leisurely pace,
perhaps because there, half of the delight is in the composition of the words.
Textbooks may be engaging, but their prose is too often stifled.&lt;/p&gt;

&lt;p&gt;I had an unhealthy fixation on &lt;em&gt;quotes&lt;/em&gt;.  Assuredly, quotes contained the words
that were the most potent, the most meaningful of the book they came from,
otherwise they wouldn’t be worth quoting!  I was fascinated by the mysterious
force of some quotes that are part of the vernacular, passed down generation
after generation to be used as thought-terminating clichés around Christmas
dinner.&lt;/p&gt;

&lt;p&gt;I got a quote dictionary: that’s a book with quotes for every word.  Two quotes
for “berry”, three quotes for “lion”.  One quote for every occasion.  Each quote
was seemingly very profound, solemn in its brevity—the shorter the quote, the
stronger its impact.  My agenda even had quotes for each day; one was simply:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;D’oh!&lt;/p&gt;

  &lt;p&gt;— Homer Simpson&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Over time, I realized that quotes drew most of their power from two sources:
their author, and their generality.  Quoting famous figures is an effective way
to &lt;a href=&quot;https://en.wikipedia.org/wiki/Argument_from_authority&quot;&gt;argue from authority&lt;/a&gt;. And, like horoscopes, quotes thrive not because of
what they actually say, but because of what we read into them.&lt;/p&gt;

&lt;p&gt;Their effects started to wane when I tried to match quotes to their context.
Quotes lose their luster when you realize that their point is made better by the
surrounding paragraphs of the text that harbor them.  You come to appreciate the
intricacies and nuances of the author’s argument; quoting then too often feels
like cargo-culting one cherry-picked part.&lt;/p&gt;

&lt;p&gt;I still like a good quote, especially of the &lt;a href=&quot;/phd-manuscript/#variations&quot;&gt;whimsical variety&lt;/a&gt;.&lt;/p&gt;

&lt;h3 id=&quot;quixotic-quests-for-knowledge&quot;&gt;Quixotic quests for knowledge&lt;/h3&gt;

&lt;p&gt;Once upon a time, I imagined that I could expand my vocabulary tremendously by
reading the dictionary cover to cover.  Mind you, I must have been around ten.
Having somewhat more ambition than diligence, that attempt ended before reaching
the word ‘abacus’.&lt;/p&gt;

&lt;p&gt;Still, I clinged to the prospect of absorbing knowledge from such ponderous
volumes.&lt;/p&gt;

&lt;p&gt;I ardently coveted the &lt;a href=&quot;https://en.wikipedia.org/wiki/Junior_Woodchucks#Junior_Woodchucks'_Guidebook&quot;&gt;Junior Woodchucks’ Guidebook&lt;/a&gt;.  This
encyclopedic little tome seemingly contained all the answers to all the
questions one could ever ask.  Alas, it was fictional.  Remember that there was
no Wikipedia; and I had no Internet access.  But we did have encyclopedias.&lt;/p&gt;

&lt;p&gt;The one we had was a sort of insidious scam.  Initially, you would just buy a
magazine containing a few interesting articles.  These were nicely written
articles, quite didactical, and well illustrated.  They even came with punched
holes so you could reorganize them in a binder.  But still, the magazine only
came with a dozen of them, and thus you had a large binder with just a handful
of articles, and a child with a consuming curiosity, so what did you do?  You
bought a subscription to receive more articles every week in the mail.  Each
Tuesday, I would get a new batch of fresh knowledge; I fondly remember reading
about a wide range of topics, from medieval conflicts to relativity, from sound
waves to giraffes.  After reading, I would dutifully transfer the articles to
the binder, putting each one in its proper category.  As months went by, the
articles piled up, and the binder became too small to hold them all.  Of course,
the publishing house also sold official binders for a pretty penny.  That was
all part of the scam: they got you with initial low costs and a promise to “pay
as you go”, but as the costs slowly accrued over time, the total cost may have
rivaled the price of a twenty-volume encyclopedia.  We ended the subscription
after I had filled my fifth binder.&lt;/p&gt;

&lt;p&gt;Even though it occupied a whole shelf, and thus was roughly ten times larger
than the Woodchucks’ Guidebook, it was obvious that it did &lt;em&gt;not&lt;/em&gt;, in fact,
contain &lt;a href=&quot;https://en.wikipedia.org/wiki/Guardians_of_the_Lost_Library&quot;&gt;the sum knowledge of all humanity&lt;/a&gt;.  Reality crushed the
magic of the Guidebook, but the idea of a tome containing all the answers never
really left me.&lt;/p&gt;

&lt;p&gt;At University I was fascinated with data structures.  They have the elegance
of simplicity on the outside, and the beauty of intricacies on the inside.  It
seemed they were crucial to solving many fundamental problems in computer
science, and as I wanted to become the very best, I had to catch them all.&lt;/p&gt;

&lt;p&gt;I started compiling all the information I could find on each structure in a
personal encyclopedia.  I documented their applications, their big-oh
complexity, trade-offs of implementation…  My goal was for it to be the one
place of reference I would ever need for all my curriculum.&lt;/p&gt;

&lt;p&gt;Its sole article was, and still is, the Linked List.&lt;/p&gt;

&lt;p&gt;After that, I realized that exhaustivity of information was not only
unachievable, but often undesirable.  For informative books to be useful, they
need to maintain a high signal-to-noise ratio; they must focus on the essential
and forget trivia.&lt;/p&gt;

&lt;h3 id=&quot;counterexamples&quot;&gt;Counterexamples&lt;/h3&gt;

&lt;p&gt;While these events did alter my perception of knowledge and its acquisition,
they did not altogether dispel my belief that reading alone would grant me its
author’s wisdom.  Other observations, however, came to clash with this theory.&lt;/p&gt;

&lt;p&gt;First, since reading a book should have conferred me its powers, I should have
been able to &lt;em&gt;write the exact same book&lt;/em&gt; after having finished reading it.  Yet,
it never was the case.  &lt;em&gt;Au contraire&lt;/em&gt;: some books I would have difficulties to
explain or describe when asked.  As if reading the words was not enough to
really &lt;em&gt;comprehend&lt;/em&gt; what they said.&lt;/p&gt;

&lt;p&gt;This was the most evident when reading books whose contents were well out of
reach.  I tried to push forward, reading each word but not &lt;em&gt;getting&lt;/em&gt; them.  It
felt as if I was reading a text in an unknown language, even though it was all
written in my mother’s tongue.  Maybe I was hoping that, when I finally got to
that last page, after clutching my way through the tortuous sentences and
unfamiliar jargon, I would be rewarded?  Hoping that, after having been seeded
by these fuzzy morphemes, some part of my brain would just &lt;em&gt;click&lt;/em&gt;?  It is
difficult to know beforehand if some text is badly written, is intentional
gibberish, or if you are in fact not smart enough to make sense of it.&lt;/p&gt;

&lt;p&gt;In time, I have learned that reading requiring &lt;em&gt;some effort&lt;/em&gt; on my part is a
good sign: it is a signal that my brain is learning.  If I’m cruising, then the
material is certainly nothing new; if on the other hand I struggle with each
sentence, then the contents are probably out of my league (for now).  There is a
fine line, and I have to surf it.&lt;/p&gt;

&lt;p&gt;But the final counterexample to that quaint theory was reading the same book for
the second time.  Surely, if one is supposedly “gets” all the book by merely
reading it once, then reading it &lt;em&gt;twice&lt;/em&gt; would be a clear waste of time.  Yet it
wasn’t.  Even worse, when re-reading books I would find some &lt;em&gt;new&lt;/em&gt; insights—not
just some leftover knowledge bits that I had previously missed, but insights
that &lt;em&gt;replaced&lt;/em&gt; the ones I had gotten from previous readings.  As if I had
previously misinterpreted the book, and it was only in this second reading that
I had attained its true meaning.&lt;/p&gt;

&lt;p&gt;But it wasn’t misinterpretation.  It didn’t &lt;em&gt;feel&lt;/em&gt; like misinterpretation.&lt;/p&gt;

&lt;h3 id=&quot;helpful-little-lies&quot;&gt;Helpful little lies&lt;/h3&gt;

&lt;p&gt;Books can only teach you what you read into them.  That sounds vacuous, but it
emphasizes one key point: the reader’s own knowledge determines the outcome as
much as the sequence of words in the book.  There’s an interplay between the
two.  My experience of reading &lt;em&gt;The Lord of the Rings&lt;/em&gt;, and what I retained from
it, may be vastly different from yours.&lt;/p&gt;

&lt;p&gt;After reading some words off the page, the process of digesting them mostly
feels like a reorganization of thought.  The words need to be integrated with my
existing mental organization.  It feels like introducing a stranger into some
long-standing fellowship.  In any group of people, there is an established
dynamic at work, and any stranger will have to learn that dynamic in order to
fit in.  The same applies to foreign-knowledge-bearing words trying to get into
my brain.  Their concepts must be adapted to the way my mental models are
already set up.  And in this adaptation, which is dictated by my past
experiences, my reading of the words is irremediably different than yours.&lt;/p&gt;

&lt;p&gt;So there was the explanation.  When years had passed between my two readings of
the same book, the &lt;em&gt;contents&lt;/em&gt; of the book had been the same, but &lt;em&gt;I&lt;/em&gt; had
changed.  These intervening years are what altered my perception of the same
words.  Much like source code isn’t complete without an interpreter, the reader
is what gives &lt;em&gt;meaning&lt;/em&gt; to the book.  What good is a book if there is no one to
read it?&lt;/p&gt;

&lt;p&gt;Thinking that the words alone bore all the knowledge was naive, even foolish.
It was a simple explanation—too simple, but one has to start somewhere.  We use
theories in order to make sense of the world, in order to survive.  Our theories
are like spiders’ shells: they protect us as we troddle around the world.
Eventually we outgrow these shells; we shed them and develop other, more
sophisticated and more nuanced theories which improve on the previous one’s
shortfalls.  Shed after shed, our grasp on the world grows, ever-changing.  And
yet, we are only ever catching glimpses of the full picture.&lt;/p&gt;

</content>
  </entry>
  
  <entry>
	 <title>Ludum Dare 39 Post-Mortem</title>
	 <link href="https://0xc0de.fr/2017/08/08/ludum-dare-post-mortem.html"/>
	 <updated>2017-08-08T00:00:00+00:00</updated>
	 <id>https://0xc0de.fr/2017/08/08/ludum-dare-post-mortem</id>
	 <content type="html">&lt;p&gt;&lt;img src=&quot;/img/projects/ld39.png&quot; alt=&quot;Bright Plateau screenshot&quot; /&gt;&lt;/p&gt;

&lt;p&gt;So, &lt;a href=&quot;https://merwanachibet.net&quot;&gt;Merwan&lt;/a&gt; and I made another game for &lt;a href=&quot;https://ldjam.com&quot;&gt;Ludum Dare&lt;/a&gt;.  It had been two
years since our last entry, &lt;a href=&quot;/yatm&quot;&gt;You Are The Münster&lt;/a&gt; (&lt;em&gt;YATM&lt;/em&gt; henceforth),
which we had a really great time making.  Ever since, we have been meaning to
get together for another round, but never managed to align our schedules until
now.&lt;/p&gt;

&lt;p&gt;You can play &lt;em&gt;Bright Plateau&lt;/em&gt; &lt;a href=&quot;/ld39&quot;&gt;right here&lt;/a&gt;.  The rest of this post is a
recollection of the 72 hours we spent making it, and contains minor spoilers
(you should try the game first).&lt;/p&gt;

&lt;h3 id=&quot;day-0-warming-up&quot;&gt;Day 0: Warming-up&lt;/h3&gt;

&lt;p&gt;In our timezone, the theme is announced at 3AM, in the night from Friday to
Saturday.  Merwan arrived Friday evening and, after we exchanged pleasantries,
we spent a few hours throwing ideas at each other, talking about games we liked,
why we liked them, what we thought they were lacking.  I remember talking about
simulations and tycoons, and how they can they can double as teaching tools for
understanding complex systems.  These genres would come up again later.&lt;/p&gt;

&lt;p&gt;We went through the list of themes in the final voting round for this LD, which
at the time &lt;a href=&quot;https://ldjam.com/events/ludum-dare/39/theme/4&quot;&gt;was&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;ul&gt;
    &lt;li&gt;Signal lost&lt;/li&gt;
    &lt;li&gt;Parallel worlds&lt;/li&gt;
    &lt;li&gt;Moving fortress&lt;/li&gt;
    &lt;li&gt;Running out of power&lt;/li&gt;
    &lt;li&gt;You are alone&lt;/li&gt;
    &lt;li&gt;Start with nothing&lt;/li&gt;
    &lt;li&gt;Machines&lt;/li&gt;
    &lt;li&gt;Running out of space&lt;/li&gt;
    &lt;li&gt;On / Off&lt;/li&gt;
    &lt;li&gt;Connections&lt;/li&gt;
    &lt;li&gt;Protect it&lt;/li&gt;
    &lt;li&gt;You are not the main character&lt;/li&gt;
    &lt;li&gt;1 vs 100&lt;/li&gt;
    &lt;li&gt;It spreads&lt;/li&gt;
    &lt;li&gt;Different perspective&lt;/li&gt;
    &lt;li&gt;1 HP&lt;/li&gt;
  &lt;/ul&gt;
&lt;/blockquote&gt;

&lt;p&gt;We liked “Moving Fortress”, “Parallel Worlds”, “Running out of space”, “On /
Off”, “Connections”; I remember discussing “You are not the main character” and
concluding it was a dead-end, on the basis that any mechanism we could find
would make the player-controlled character &lt;em&gt;the&lt;/em&gt; main character in the eyes of
the player.  We felt pretty good about “Running out of space” winning, but we
did not cast our votes, as we didn’t even have accounts to the new LD site at
the time.&lt;/p&gt;

&lt;p&gt;We then checked out a few of the top games from the last Ludum Dare for
inspiration:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/posts/ld38.png&quot; alt=&quot;LD38 top games&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Out of these six games, we played: &lt;em&gt;Honey Home&lt;/em&gt;, &lt;em&gt;The Deep&lt;/em&gt;, &lt;em&gt;Super Kaiju Dunk
City&lt;/em&gt; and &lt;em&gt;Snowed In&lt;/em&gt;.  &lt;em&gt;Little Lands&lt;/em&gt; looked awesome, but was Windows only, so
we skipped it.  For each game, we tried to understand its appeal, how they used
the theme to good effect, and summarized what we liked/disliked.&lt;/p&gt;

&lt;p&gt;I showed Merwan another LD game I had stumbled upon
prior: &lt;a href=&quot;https://jackrugile.com/twin-condition/&quot;&gt;Twin Condition&lt;/a&gt;.  I really liked
the clean aesthetic, the smooth animations, and the fact that you had to play
both part of the levels differently, but at the same time:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/posts/twin-conditions.png&quot; alt=&quot;Twin Condition screenshot&quot; /&gt;&lt;/p&gt;

&lt;p&gt;I also showed him &lt;a href=&quot;http://rezoner.net/labs/spacesim.io/client/&quot;&gt;this prototype&lt;/a&gt;
by rezoner, the author of &lt;a href=&quot;http://playgroundjs.com/&quot;&gt;Playground.js&lt;/a&gt; (among other
nice things); I liked the effectiveness and look of the low-resolution 3D
graphics:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/posts/spacesim-proto.png&quot; alt=&quot;Rezoner space prototype screenshot&quot; /&gt;&lt;/p&gt;

&lt;p&gt;This low-resolution is achieved simply by drawing to a small canvas (say,
320x180), then scaling it in the browser:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&amp;lt;canvas width=&quot;320&quot; height=&quot;180&quot; style=&quot;width: 960px; height: 540px;&quot;&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The browser will use a linear filter by default when drawing the scaled canvas,
which looks buttery (as above).  You can use a nearest filter for maximum
crispness with the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;image-rendering&lt;/code&gt; CSS property.&lt;/p&gt;

&lt;p&gt;Though unbeknownst to us, the visual presentation of &lt;em&gt;Bright Plateau&lt;/em&gt; has been
definitely influenced by these two games.&lt;/p&gt;

&lt;p&gt;Then it was already 1AM.  For our previous LD, we stayed up until the theme had
been announced, started scribbling game ideas on paper, and went to bed only
after we had nothing further to write down.  This time though, we elected to
start off with a good night’s sleep and to find out the theme in the morning.&lt;/p&gt;

&lt;h3 id=&quot;day-1-finding-the-game-idea&quot;&gt;Day 1: Finding the game idea&lt;/h3&gt;

&lt;p&gt;We woke up at around 9AM to discover the winning theme:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Running out of power&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Well, we quickly skirted that one the night before, as it hadn’t inspired
anything to us.  But now the choice had been made.  We started throwing ideas:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/posts/ld39-ideas.jpg&quot; alt=&quot;paper with game ideas for the theme&quot; /&gt;&lt;/p&gt;

&lt;p&gt;We quickly gravitated to the idea in lower right: “Puzzle based around the power
grid”.  Although at first the game was quite different than what we ended up
with.&lt;/p&gt;

&lt;p&gt;I remember pushing for a tycoon-like: there would be houses and plants to
consume power, and you would buy and put power generators on the map to supply
them.  The power consumers would have different consumption profiles: plants
would need a lot of power with low downtime, while houses would consume mostly
in the day, but less at night.  The consumption would not be fixed, but could
vary from house to house.  The generators would &lt;em&gt;also&lt;/em&gt; produce a varying amount
of power, depending on environmental conditions (wind for wind turbine, sunlight
for solar panels), maintenance, efficiency…  You earned money by providing
power to consumers.  Maybe this money would have been your score, maybe it would
have allowed you to upgrade your generators.  You can make out some of that on
the doodles above.&lt;/p&gt;

&lt;p&gt;With our previous game, we had learned that it was quite tempting to aim for a
much larger game than we had the time budget (and ability) to build.  In &lt;em&gt;YATM&lt;/em&gt;,
we had planned for a large world with four areas, each guarded by a boss that
would be designed around the item kept in its area.  In the end, we only had two
areas, and zero boss.&lt;/p&gt;

&lt;p&gt;This time around, we agreed to focus our design in order to have a game that
would feel whole.  Merwan suggested simpler rules; less simulation, more puzzle.
What if instead of houses and plants, we had only houses?  What if, instead of
money, you would have a simple goal: power all the houses?  What if, instead of
a varying power distribution, we had only a binary state: on and off?&lt;/p&gt;

&lt;p&gt;We thought about how that would work.  You would put generators on the map to
supply houses.  Each generator would supply power to all the houses in an area
around it, but it would also have a fixed distribution in time.  So, for
instance, the solar panel would power all the houses in a disc around it, but it
would have a 50/50 distribution over 24 hours: 100% in daylight, 0% at night.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/posts/ld39-power-distribution.jpg&quot; alt=&quot;photo of generators distribution along time&quot; /&gt;&lt;/p&gt;

&lt;p&gt;We tried to play with the idea in our heads.  Would that be easy to understand?
How would you play the game?  How would you visualize the constraint of
validating the power distribution over time?  The time distribution was
equivalent to a distribution in a third spatial dimension.  So, instead of
placing generators on a plane, you were really placing them in space.  But how
would that work, exactly?  Placing objects in space on a 2D screen is not the
easy to grasp, even when accustomed.  Maybe display the time dimension below,
like in &lt;a href=&quot;http://www.zachtronics.com/shenzhen-io/&quot;&gt;Shenzhen I/O&lt;/a&gt;:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/posts/shenzhen-io.png&quot; alt=&quot;Shenzhen I/O screenshot&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Merwan suggested to simplify, again.  What if, instead of 24 slices of a day, we
had only two: one slice for the day, one for the night.  Then, we could show the
grid in the day, have a little sun floating in the sky, and you would pick up
the sun and drag to the right to see the grid at night.  The coverage of the
generators would change at night, and to solve the puzzle you would have to
power all the houses at day and night simultaneously.&lt;/p&gt;

&lt;p&gt;While I liked the idea of dragging the sun, I felt that going back and forth
between the day and night would make playing unnecessarily frustrating, so I
pushed for having them side-by-side.  Merwan argued that it might be difficult
to explain to players the sudden split between day and night.  But then again,
finding out that you could drag the sun away would not necessarily be more
intuitive.  And having the day and night portions side-by-side would probably be
easiest to get right.  We thought about potential ways to present this to the
player, but did not find a satisfying solution, and elected to review this once
we had the game running.  In the end, we never reviewed it: the night levels are
introduced without any explanation, but so far no player has complained about
not understanding that they have to light all the houses on both sides to solve
the level.&lt;/p&gt;

&lt;p&gt;At this point, we felt this &lt;em&gt;might&lt;/em&gt; be an interesting game: simple coverage
alone would quickly be boring, but solving coverage with two slightly different
sets of constraints?  A tricky twist!&lt;/p&gt;

&lt;h3 id=&quot;prototyping-on-paper&quot;&gt;Prototyping on paper&lt;/h3&gt;

&lt;p&gt;We took this idea for a spin on paper before committing to code it.&lt;/p&gt;

&lt;p&gt;What should the coverage for the generators be?  Again, we went for the simplest
thing we could think of: the solar panel would power a straight line, and the
wind turbine would power its immediate neighbors (in the final game, we did it
the other way around, as it felt more natural while playing).  I cut small lines
and crosses out of paper, and drew a few levels to get started:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/posts/ld39-paper-generators.jpg&quot; alt=&quot;sketch of generators&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/posts/ld39-paper-levels.jpg&quot; alt=&quot;level sketches on paper&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The number represent the amount of houses you have to power up.  Each generator
provides 1 unit of power to each tile it covers, so you have to overlap the
coverage area of multiple generators to supply power to tiles with 2 or more
houses.&lt;/p&gt;

&lt;p&gt;In designing these levels, I went for the simplest non-trivial configurations of
lines and crosses I could find, then simply numbered &lt;em&gt;some&lt;/em&gt; of the tiles covered
by the shapes.  Numbering all the covered tiles would have given away the
placement of the generators, and would have over-constrained the solution.
Solving the levels would have been easy and boring.  By not giving away too much
information, you leave the player guessing.  By not over-constraining, you also
let the player solve the level in different ways, and that makes it more
interesting.  Although, you have to try to make sure there are no trivial
solutions that you didn’t think of.  Obstacles are a good way to prevent trivial
solutions.&lt;/p&gt;

&lt;p&gt;I handed these levels to Merwan, and he tried to solve them.  He got one very
quickly, and got stumped on another one.  “Are you sure there is a solution?” he
asked.  “I think so” I replied, and I thought: this is challenging, he looks
like he is having fun!  Then I double-checked, and as it turned out, my level
had no solution.  When I fixed it, he solved it quite easily.  I then made
another one, this time making sure it had a solution, and one that was not too
obvious.  He solved it in around 1 minute.&lt;/p&gt;

&lt;p&gt;We felt that the process of solving the levels was fun enough to go forward with
this idea.&lt;/p&gt;

&lt;h3 id=&quot;solving-the-night-conundrum&quot;&gt;Solving the night conundrum&lt;/h3&gt;

&lt;p&gt;We really felt like we &lt;em&gt;had&lt;/em&gt; to have the night in there, as it would make the
game more unique.  But how exactly would that work?&lt;/p&gt;

&lt;p&gt;Maybe the houses you need to power would not be the same at night?  Maybe, some
houses would only need power during the day, and some would need power during
the night?  We felt that would make the puzzles hard to design.  As we wanted to
display the day and night side-by-side, your generators would necessarily be in
the same place on the plateau in both sides, otherwise you wouldn’t be solving
the same level, but really two separate levels.  So, we went for simple again,
and the houses you need to power would have to be the same on each side.&lt;/p&gt;

&lt;p&gt;We wanted to have a real interplay between the day and night portions: you would
have to go back and forth to satisfy the constraints on both sides.  We needed
to find a mechanism that would let you solve the day side, but you would have to
refine your solution in order to solve the night side.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/posts/ld39-paper-night-coverage.jpg&quot; alt=&quot;sketch of night coverage for generators&quot; /&gt;&lt;/p&gt;

&lt;p&gt;We started by reducing the coverage of wind turbines and solar panels by half.
Now, we had to somehow overcome this power disparity between night and day.  We
thought about adding a third power generator that would work only at night.
What would be its coverage area?  We thought of a battery that would spread
power to neighboring houses.  Maybe we could charge the battery in the day, and
depending on the amount of charge, it would power that many houses?  If two
generators are powering it in the day, it provides two units power at night.
But how would we decide &lt;em&gt;which&lt;/em&gt; houses would be powered by the battery at night?
If the battery has one unit of power, but two neighboring houses, which one is
powered?  And more importantly, how to communicate that rule to the player?  We
didn’t know, so we changed it: what if the battery gives power to all the
neighboring houses, regardless of its charge?  That would be less annoying, as
you wouldn’t have to care about the orientation of the battery.  The battery
would still need to be powered during the day, but receiving one unit of power
would be enough.  That made the day and night sides feel connected.&lt;/p&gt;

&lt;p&gt;We did a quick check on the paper levels to see if the battery would still allow
you to solve the levels we had.  It wasn’t clear at this time whether the
battery mechanism was interesting to play or not, but it was already 4PM, and we
ought to start coding at some point.&lt;/p&gt;

&lt;h3 id=&quot;getting-to-a-playable-state&quot;&gt;Getting to a playable state&lt;/h3&gt;

&lt;p&gt;We both felt that an isometric view would be right for the game, and a very good
pretext to play with &lt;a href=&quot;https://threejs.org&quot;&gt;THREE.js&lt;/a&gt;.  We threw together a
skeleton made of THREE.js and Playground.js (though we didn’t use much of the
latter in the end) and got to work.&lt;/p&gt;

&lt;p&gt;I took care of the game logic: first a grid you could put things on.  Things are
consumers, generators or obstacles.  A consumer is just be a number: how many
houses are on the tile.  A generator has a coverage area, and we use that
coverage in order to validate the level configuration: make sure every consumer
gets as many units of power as it requires.  Here is the first iteration of the
level validation code:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-js&quot; data-lang=&quot;js&quot;&gt;&lt;span class=&quot;kd&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;counters&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Map&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;th&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;of&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;things&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;keys&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;th&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;instanceof&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Consumer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;counters&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;th&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;th&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;of&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;things&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;keys&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;th&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;instanceof&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Generator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// Let the generator add to the power counters&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;th&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;distributePower&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;counters&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// Gather any mispowered (unpowered/overloaded) consumer,&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// with the current value&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;mispowered&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[];&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;counter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;power&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;of&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;counters&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;entries&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;counter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!==&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;power&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;mispowered&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;push&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;({&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;counter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;current_power&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;power&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// If there are no mispowered consumers, the level is solved!&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;solved&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;mispowered&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;length&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;===&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;I revisited it later to have separate counters for the night as well, and to
handle the case of the battery distributing power at night only when powered in
the day.  But the principle remains the same.&lt;/p&gt;

&lt;p&gt;For editing the levels, I simply represented them as strings in a JS file:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-js&quot; data-lang=&quot;js&quot;&gt;&lt;span class=&quot;kd&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;LEVELS&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;generators&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;SS&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;`....1.2...`&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;

  &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;generators&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;SSW&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;`.....
          .....
          .221.
          .2.1.
          .....`&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;For &lt;em&gt;YATM&lt;/em&gt;, we had used the &lt;a href=&quot;http://www.mapeditor.org/&quot;&gt;Tiled&lt;/a&gt; editor, and while it had its downsides, it
got the job done.  However, using an external editor, there is an extra
export step that gets in the way when you need to design levels quickly.&lt;/p&gt;

&lt;p&gt;With the levels in a string, I just wrote a simple parser that emitted instances
of the relevant objects according to the current character.  It made editing
levels trivial, as I just had to change a few characters in a text editor and
reload the game to immediately see the result.  This allowed me to iterate on
the levels rather quickly on Day 3, when I put the bulk of the levels in the
game.&lt;/p&gt;

&lt;p&gt;By the end of Day 1, we could load a level, move and place generators, and
the game could tell us whether the level was solved or not, and which tiles were
lacking power.  All of that in &lt;em&gt;the console&lt;/em&gt;, not on the screen.  Merwan was in
charge of everything visual (he has the credentials for that), but we didn’t
plug the view and the logic together until Day 2.&lt;/p&gt;

&lt;h3 id=&quot;day-2-adding-the-visuals&quot;&gt;Day 2: Adding the visuals&lt;/h3&gt;

&lt;p&gt;Here are a few screenshots of the visual progress for the first two days:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/posts/ld39-collage1.png&quot; alt=&quot;collage of several iterations of the game visuals&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Why are wind turbines floating in the air in the bottom two screenshots, you
ask?  This is the inventory that we cut from the game.  If you look back to the
level data higher up, you will see a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;generators&lt;/code&gt; line.  Initially, you were
supposed to pick the generators from the inventory area before placing them on
the plateau.  The code was a bit tricky to get right, as we needed to have a
separate render pass just for the inventory, and specific picking code since the
inventory would live in a separate world space.  Plus, we had extra logic that
dealt with taking things from the inventory and putting them back in it.  But
then, it occurred to me that we didn’t &lt;em&gt;need&lt;/em&gt; the inventory: we could place the
generators directly on the plateau, and the player would simply move them.  It
made the code simpler, and the game more intuitive!  I immediately told Merwan,
who agreed it was the right call—but only after initially hating me for cutting
the feature he had spent the last two hours working on.&lt;/p&gt;

&lt;p&gt;The lower-right screenshot shows the side-by-side day and night views working.
It was a bit simpler than anticipated to get working (THREE.js
had &lt;a href=&quot;https://threejs.org/examples/?q=multiple#webgl_multiple_views&quot;&gt;example code&lt;/a&gt; for multiple views), but it was still tricky to get
everything right.  At first, we thought about duplicating the day scene into a
night scene and keep the two scenes in sync with the model.  Merwan suggested an
alternative of rendering the same scene, but changing a few parameters on the
fly.  For instance, lights could be toggled on and off, or switched directions
before rendering the night scene, and reset before rendering the day scene.
This worked nicely.&lt;/p&gt;

&lt;p&gt;The tricky part was rendering the coverage area of the generators, as they were
actually different objects.  The visual feedback for powered houses would also
necessarily be different in both scene, as a house could be powered at day but
not receive any power at night.  We ended up rebuilding the coverage areas
&lt;em&gt;every time&lt;/em&gt; we set up the day or night scene, that is, at 120Hz.  This is
grossly inefficient and keeps the garbage collector quite busy, but as we only
show the coverage for the generator currently under the cursor, it has not been
an issue in practice.  The feedback for powered house is less wasteful: it’s
simply a list of objects that we give to an outline pass; we update this list
before rendering the day and night scenes.&lt;/p&gt;

&lt;p&gt;At the end of Day 2, we had something playable, although with only 5 levels, and
the visual feedback was still rough.  We parted ways as Merwan had to travel
back home, and we kept hacking on the game remotely with regular updates over
the phone, right until the deadline.&lt;/p&gt;

&lt;h3 id=&quot;day-3-night-levels-and-polish&quot;&gt;Day 3: night levels and polish&lt;/h3&gt;

&lt;p&gt;&lt;img src=&quot;/img/posts/ld39-collage2.png&quot; alt=&quot;second collage of several iterations of the game visuals&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The last day was probably the longest as we stayed up right until the deadline
to put everything we could in the game, and make a coherent whole.&lt;/p&gt;

&lt;p&gt;Merwan worked hard on making sure the rules of the games were intelligible
through the visuals.  You can see in the collage above some iterations of the
coverage areas and visual feedback for powered houses: from orange tiles
signaling unpowered houses, to individual outlines around houses.&lt;/p&gt;

&lt;p&gt;He also worked on visual polish: the dust particles when you plop a generator on
the plateau, or making the day and night scene have a different feel, so it
would be obvious to players that it was, in fact, the same plateau at different
times of the day.  The sun and the moon were a nice touch on that front, but
then the shadow directions were all wrong, and we had issues with the moon
coming in front of generators in the night scene.  We would have loved keeping
them, but due to lack of time, he cut them and opted for a starry background
instead.  He still took the time to put a &lt;em&gt;YATM&lt;/em&gt; easter egg in the stars though!&lt;/p&gt;

&lt;p&gt;I spent the day making the level validation work for the night levels, as we
settled on the exact behavior of the battery.  I then designed the night levels;
we still had only one at that point.  I aimed for 3 night levels, as I wasn’t
really sure I could stretch the idea without making the levels feel repetitive.
In the end, I managed to make quite a few levels more!  I then spent around two
hours making the sound effects in &lt;a href=&quot;http://www.drpetter.se/project_sfxr.html&quot;&gt;sfxr&lt;/a&gt; and the music in &lt;a href=&quot;http://www.warmplace.ru/soft/sunvox/&quot;&gt;Sunvox&lt;/a&gt;.  I was
quite satisfied with the music, as the end result was about what I had in mind
when I started.  We also worked on buttons for navigating between the levels,
and transitions.&lt;/p&gt;

&lt;p&gt;When the game felt whole, I asked Marie to test it from start to finish.  Having
a fresh set of eyes on the game is crucial, as there are things that are obvious
to the developers who have been working on it from the start, but may not be to
players who might not devote more than a few minutes to the game.  Her feedback
helped me nail down the level difficulty progression, and gave us a roadmap of
things to improve in the last hours of the jam.&lt;/p&gt;

&lt;p&gt;Ten minutes before the deadline, I hit the “Submit” button on the LD page.  We
pat each other’s back over the phone on a job well done.  We had made a game!&lt;/p&gt;

&lt;h3 id=&quot;aftermath&quot;&gt;Aftermath&lt;/h3&gt;

&lt;p&gt;It’s a weird feeling, to know that three days earlier you had no clue of what
your game would look like, or if you would even be able to get something fun
working this time around.  But, now, we have a game we can show to friends and
colleagues.  The feedback on
the &lt;a href=&quot;https://ldjam.com/events/ludum-dare/39/bright-plateau&quot;&gt;LD page&lt;/a&gt; has also
been overwhelmingly positive.  It’s a great feeling!&lt;/p&gt;

&lt;p&gt;I would without a doubt recommend to anyone who has ever wanted to make a game,
to participate in a game jam.  I feel it is a good way to learn how to focus
your game design and coding efforts in order to make something playable and
complete.  It forces you to relentlessly cut, cut and cut, until you have a core
that is both fun enough &lt;em&gt;and&lt;/em&gt; realizable.  This is quite different from the
greenfield development of side projects.&lt;/p&gt;

&lt;p&gt;And even if you don’t finish or submit your game, the experience itself is
something that is worth going through, as it will create memories that you will
gladly reminisce later down the road.  I know I will.&lt;/p&gt;

</content>
  </entry>
  
  <entry>
	 <title>Against Simplification</title>
	 <link href="https://0xc0de.fr/2016/05/24/against-simplification.html"/>
	 <updated>2016-05-24T00:00:00+00:00</updated>
	 <id>https://0xc0de.fr/2016/05/24/against-simplification</id>
	 <content type="html">&lt;p&gt;Our minds build theories, models of the world in order to function.  To
communicate, we build models of reality through language, gestures, paintings,
music…&lt;/p&gt;

&lt;p&gt;When reading an essay, or any piece of literature really, I read a simplified
view of the world.  A view that can fit into a page of words.  Reality in a can.&lt;/p&gt;

&lt;p&gt;Reality can be experienced, that’s for certain.  We do experience it every day.
It can be argued that we do not experience &lt;em&gt;all&lt;/em&gt; of it, because all our
experiences are filtered by our senses.  Our senses shape our reality, as do our
minds.  The mind mixes the sensorial inputs to form a pleasant &lt;em&gt;arrangement&lt;/em&gt;,
one that conforms to our memories and expectations.  So maybe, &lt;em&gt;my&lt;/em&gt; reality is
not the same as &lt;em&gt;your&lt;/em&gt; reality.  But in any case, we do experience it by living
and breathing.&lt;/p&gt;

&lt;p&gt;And, maybe I’m not a very eloquent person, but I can’t think of any sequence of
words, or any amount of epithets, that would describe that experience in its
full.  Any description is dumbing down.  And because everyone’s experience of
reality is slightly different, any one description will always be influenced by
the perception of its author.  But even if the description were apt, &lt;em&gt;reading&lt;/em&gt;
about a thing is not &lt;em&gt;experiencing&lt;/em&gt; the thing.  Reading is &lt;em&gt;an&lt;/em&gt; experience, and
can be quite an enjoyable one, but no description can substitute to the
experience itself.&lt;/p&gt;

&lt;p&gt;All models are wrong, but some are useful.  Our minds are seemingly incapable of
capturing our experience of reality.  The function of the brain is to capture
the infinite in a finite medium.  It compresses.  It classifies.  It categories.
It put small labels on things, and put them in separate boxes.  Then, it
generalizes.  It constructs theories about the things in the same box, so that
we don’t always have to deal with individual things, only with boxes.  It
chunks.  And it continues to chunk as we absorb more information, as we
experience.  It puts the boxes of things in larger boxes, and those boxes in
other boxes and so on.  It abstracts.&lt;/p&gt;

&lt;p&gt;Our mind devour theories.  A good theory, for our mind, is like finding a free
box on the street.  When you have a lot of junk, free boxes are always a good
thing.  Even more so when the box is accompanied with a list of the things it
should hold.  So finding a good theory is an opportunity to compress some more,
without having to do the work of devising it.  Extra space, no effort.  A good
essay is a coherent slice of reality, carefully chosen and assembled in a way
that’s easily digestible by our mind.&lt;/p&gt;

&lt;p&gt;There are interesting side-effects to this way the mind works.  Soon enough, we
start seeing relationships at the “box” level that were not apparent at the
“thing” level.  Things that had seemingly nothing in common now share abstract
characteristics.  Before, you saw lamps, wheels, bottle caps, mugs, and holes;
now you are seeing circles.&lt;/p&gt;

&lt;p&gt;Abstraction is a powerful tool.  Everything we know about a circle, we can apply
to any object that has a circle shape, or contains a circle-shaped part.
Instead of devising theories for each individual thing, we discover facts and
solve problems for whole categories of things, at once.  Mathematics are a
constant reminder of the power of thinking with abstractions.&lt;/p&gt;

&lt;p&gt;But abstractions are simplifications.  By abstracting, we strip the details, we
remove the ornaments, trim the superfluous, the extraneous, to keep only what is
essential.  But what is essential to a thing is not absolutely determined; it
may depend on the context.  A detail for one observer may be the essence for
another, and vice-versa.  Some models may be useful, but all are wrong.&lt;/p&gt;

&lt;p&gt;The danger is to mistake these abstractions for reality.  Abstractions are
artifacts, manifest side-effects of the way our brains make space for new
information.  Abstract relationships are illusions forged by our minds.  Trying
to shape the world around us according to our abstractions, our very personal,
and very peculiar, illusions, is indistinguishable from madness.&lt;/p&gt;

&lt;p&gt;The experience of reality is rich, complex in millions of little ways, and
ultimately chaotic.  But it is the only one there is, and we should embrace it
in full, even if it is irreconcilable with the way our main organ to experience
it works.&lt;/p&gt;

&lt;p&gt;But that’s only my, very own, simplification.&lt;/p&gt;
</content>
  </entry>
  
  <entry>
	 <title>The Many Ways to Build Objects in JavaScript</title>
	 <link href="https://0xc0de.fr/2014/10/24/js-object-creation.html"/>
	 <updated>2014-10-24T00:00:00+00:00</updated>
	 <id>https://0xc0de.fr/2014/10/24/js-object-creation</id>
	 <content type="html">&lt;p&gt;In JavaScript, the dominant pattern to declare objects is to write a
constructor function and append each method to its &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;prototype&lt;/code&gt;
property.  To create an instance of the object, one must then use the
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;new&lt;/code&gt; keyword on a call to the constructor function.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-js&quot; data-lang=&quot;js&quot;&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;items&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[];&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;nb&quot;&gt;Set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;prototype&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;add&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;items&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;push&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;Set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;prototype&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;items&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Here &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Set&lt;/code&gt; is the constructor function; in its body, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;this&lt;/code&gt; is a
reference to a newly-created object that will be returned after a call
to the constructor with the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;new&lt;/code&gt; keyword.  By convention, constructor
functions begin with a capital.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-js&quot; data-lang=&quot;js&quot;&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;s&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// 1&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Note that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Set&lt;/code&gt; is just a regular function, i.e. we can call it
without the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;new&lt;/code&gt; keyword.  Though usually it is not what you want,
since &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Set&lt;/code&gt; will return &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;undefined&lt;/code&gt; and will bind &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;this&lt;/code&gt; to the global
object in the absence of a caller.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-js&quot; data-lang=&quot;js&quot;&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;s&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// error: s is undefined&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;items&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// [] -- Global objects is polluted&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;new&lt;/code&gt; keyword applied to the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Set&lt;/code&gt; function has the following
effect:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;An empty object &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;o&lt;/code&gt; is created.&lt;/li&gt;
  &lt;li&gt;The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[[Proto]]&lt;/code&gt; property of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;o&lt;/code&gt; is bound to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Set.prototype&lt;/code&gt;.  In
other words, the prototype of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;o&lt;/code&gt; is set to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Set.prototype&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;The function &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Set&lt;/code&gt; is called with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;o&lt;/code&gt; is bound to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;this&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;When the function returns, it returns &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;o&lt;/code&gt; instead of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;undefined&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;That is why methods of the object must be attached to the
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Set.prototype&lt;/code&gt; object: it is the prototype of every instance of
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Set&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;I always found this pattern verbose and confusing.  The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;new&lt;/code&gt; keyword
was made to be familiar for Java programmers, but there is no mention
of classes, only prototypes.  In fact, it can be misleading to relate
to Java, since the receiver of a method is not bound statically in
JavaScript.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-js&quot; data-lang=&quot;js&quot;&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;s&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;add&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;// Aliasing the method&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;             &lt;span class=&quot;c1&quot;&gt;// error: `this.items` is undefined&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;The call to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;add(2)&lt;/code&gt; has no explicit caller, so &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;this&lt;/code&gt; is bound to the
global object, which doesn’t have an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;items&lt;/code&gt; object.&lt;/p&gt;

&lt;p&gt;Also, functions have a unique &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;prototype&lt;/code&gt; property that is
unfortunately not the same thing as the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[[Proto]]&lt;/code&gt; property that
every object – including functions – have.&lt;/p&gt;

&lt;p&gt;Languages that compile to JavaScript, like CoffeeScript, TypeScript,
Dart and even the next version of EcmaScript all provide syntactic
sugar to hide this pattern.&lt;/p&gt;

&lt;p&gt;Luckily, there are alternatives to this pattern in vanilla JS.  By not
trying to mimic class-based languages, but harnessing the simplicity
of the prototype model, one finds flexible and terse ways to create
objects.&lt;/p&gt;

&lt;h3 id=&quot;object-factories&quot;&gt;Object factories&lt;/h3&gt;

&lt;p&gt;JavaScript has objects literals, which are great for building
singleton objects.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-js&quot; data-lang=&quot;js&quot;&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;bag&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;items&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[],&lt;/span&gt;

  &lt;span class=&quot;na&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;items&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;push&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;items&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;

&lt;span class=&quot;nx&quot;&gt;bag&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;bag&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// 1&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Attributes and methods are now just properties of the same object.
In this respect, this form is more regular than the first pattern.&lt;/p&gt;

&lt;p&gt;What if we want another instance of this object?  In other
prototype-based languages, we would just clone the first one; in
JavaScript, there’s no built-in clone function.  Instead, we can
create a factory function.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-js&quot; data-lang=&quot;js&quot;&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;mkSet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;items&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[],&lt;/span&gt;

    &lt;span class=&quot;na&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;items&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;push&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;items&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;s1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;mkSet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;s2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;mkSet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;s1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;s2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// 0&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mkSet&lt;/code&gt; function puts out new objects of identical initial state.
Compared to the previous example, we have lost a bit of flexibility.
The bag was usable right away, but here we must call the factory to
create a new object before beginning to use it.&lt;/p&gt;

&lt;p&gt;This form also has another downside: the methods are recreated
separately for each object.  The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;add&lt;/code&gt; method of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;s1&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;s2&lt;/code&gt; are not
the same.  Not only if this wasteful, but leaves us no way of adding
or altering methods for all objects of the same factory.&lt;/p&gt;

&lt;h3 id=&quot;sharing-behavior-with-prototypes&quot;&gt;Sharing behavior with prototypes&lt;/h3&gt;

&lt;p&gt;We can correct the latter defect by putting the methods in a separate
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;setProto&lt;/code&gt; object.  Then, in the factory method we must set the
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[[Proto]]&lt;/code&gt; property to this &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;setProto&lt;/code&gt; object.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-js&quot; data-lang=&quot;js&quot;&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;setProto&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;items&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;push&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;items&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;

&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;mkSet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;__proto__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;setProto&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;items&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[],&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;s1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;mkSet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;s2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;mkSet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;s1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;s2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// 0&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;This way, all objects created by the factory &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mkSet&lt;/code&gt; share the same
methods.  We can easily extend the functionality of all sets by
appending methods to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;setProto&lt;/code&gt;.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-js&quot; data-lang=&quot;js&quot;&gt;&lt;span class=&quot;nx&quot;&gt;setProto&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;clear&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;items&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;length&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Note that we did not correct the first defect: we must still use the
factory to begin using sets.  However, we essentially recreated the
first pattern: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mkSet()&lt;/code&gt; does what &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;new Set()&lt;/code&gt; did, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;setProto&lt;/code&gt; is
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Set.prototype&lt;/code&gt;.  It is less verbose, but now the methods and
constructor are separate entities, which makes even less sense.  As a
result &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;setProto&lt;/code&gt;, is not a usable object on its own.  We would like
to reunite the two.&lt;/p&gt;

&lt;h3 id=&quot;reuniting-prototype-and-constructor&quot;&gt;Reuniting prototype and constructor&lt;/h3&gt;

&lt;p&gt;Since the constructor is just a function, why not put it inside the
prototype object itself?&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-js&quot; data-lang=&quot;js&quot;&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Set&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;na&quot;&gt;__proto__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;na&quot;&gt;items&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[],&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;

  &lt;span class=&quot;na&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;items&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;push&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;items&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;

&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;s1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;s2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;s1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;s2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// 0&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Set&lt;/code&gt; is still the prototype object, but the constructor is part of
its definition.  Calling &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Set.new()&lt;/code&gt; creates new objects inheriting
from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Set&lt;/code&gt;.  In fact, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;new&lt;/code&gt; is a method of all &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Set&lt;/code&gt; objects.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-js&quot; data-lang=&quot;js&quot;&gt;&lt;span class=&quot;nx&quot;&gt;s1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// Exactly the same as Set.new()&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;While this is unusual for class-based languages like Java, where
constructors are special methods, here we see they are just regular
functions that return new objects.&lt;/p&gt;

&lt;p&gt;There is a bit of redundancy there, namely the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Set&lt;/code&gt; in the line
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;__proto__: Set&lt;/code&gt;.  Since we will call the constructor with
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Set.new()&lt;/code&gt;, why not use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;this&lt;/code&gt; instead?&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-js&quot; data-lang=&quot;js&quot;&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Set&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;na&quot;&gt;__proto__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;na&quot;&gt;items&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[],&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;

  &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;

&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;s1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Now &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Set.new()&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;s1.new()&lt;/code&gt; are no longer equivalent.  The latter
create an object that have &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;s1&lt;/code&gt; as prototype, not &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Set&lt;/code&gt;.  This means
that we can specialize objects in branches, not only as a whole.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-js&quot; data-lang=&quot;js&quot;&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;s1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;s2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;s11&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;s1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;

&lt;span class=&quot;nb&quot;&gt;Set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;clear&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;items&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;length&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;s1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;forEach&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;items&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;forEach&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Here, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;s1&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;s2&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;s11&lt;/code&gt; all have the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;clear&lt;/code&gt; method, but only &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;s1&lt;/code&gt;
and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;s11&lt;/code&gt; have &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;forEach&lt;/code&gt;.&lt;/p&gt;

&lt;h3 id=&quot;prototypes-as-default-objects&quot;&gt;Prototypes as default objects&lt;/h3&gt;

&lt;p&gt;We should try to correct the first defect.  As it stands, the only
method that makes sense to call on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Set&lt;/code&gt; is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;new&lt;/code&gt;.  Why not make &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Set&lt;/code&gt;
a fully functional instance of itself?&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-js&quot; data-lang=&quot;js&quot;&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;set&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;

  &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}.&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;

&lt;span class=&quot;kd&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;s2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Just by calling &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;new()&lt;/code&gt; on the object defining the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;set&lt;/code&gt; prototype, we
are giving it an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;items&lt;/code&gt; array.  Somehow we lost the direct reference
to the original prototype, but it does not matter since all &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;set&lt;/code&gt;
objects will be derived from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;set&lt;/code&gt; itself (and we can still access it
from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;set.__proto__&lt;/code&gt;).&lt;/p&gt;

&lt;h3 id=&quot;cloning-is-just-another-method&quot;&gt;Cloning is just another method&lt;/h3&gt;

&lt;p&gt;Finally, when creating objects we might want to make a deep copy of
an existing copy – not start from a fresh state.  This can be
achieved by adding a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;clone&lt;/code&gt; method that defines the deep cloning of
each attribute.  It also binds &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[[Proto]]&lt;/code&gt; of course.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-js&quot; data-lang=&quot;js&quot;&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;set&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;

  &lt;span class=&quot;na&quot;&gt;clone&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;na&quot;&gt;__proto__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;na&quot;&gt;items&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;items&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;

  &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}.&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;

&lt;span class=&quot;kd&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;s1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;clone&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;s1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// 1&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Here &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;s1&lt;/code&gt; begins as an identical copy of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;set&lt;/code&gt;, but not sharing the
reference to the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;items&lt;/code&gt; array.  If we want shallow cloning, we can
just use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Object.create(set)&lt;/code&gt;.  And if we want to branch, we can call
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;set.new&lt;/code&gt;.  We also can use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;set&lt;/code&gt; right away as a set.  All objects
derived from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;set&lt;/code&gt; will inherit from changes made to their prototype,
which allow to specialize and extend their behavior as befit.&lt;/p&gt;

&lt;p&gt;This last form is not only more flexible than the dominant pattern of
the first example, it is more terse and regular.  But more
importantly, it illustrates the orthogonality of the core concepts of
JavaScript: object literals, first-class functions and prototypes.&lt;/p&gt;
</content>
  </entry>
  
  <entry>
	 <title>Suspend Troubles in Ubuntu 14.04</title>
	 <link href="https://0xc0de.fr/2014/08/19/vaio-z13-ubuntu-14-04.html"/>
	 <updated>2014-08-19T00:00:00+00:00</updated>
	 <id>https://0xc0de.fr/2014/08/19/vaio-z13-ubuntu-14-04</id>
	 <content type="html">&lt;p&gt;My laptop is a Vaio Z13 with an Nvidia GT330M discrete graphics card
and an integrated Intel graphics controller.  A physical switch above
the keyboard is used to toggle the discrete card.  The Nvidia card is
useful for games and hooking an external monitor via the HDMI port,
but is a battery hog.  When the Nvidia card is off, the Intel
controller is sufficient for everything else, and can also control an
external monitor via the VGA port.&lt;/p&gt;

&lt;p&gt;After a fresh install of Ubuntu 14.04, both graphics controllers are
correctly detected, and everything looks like it is run by the Intel
driver.  But the Nvidia card is also turned on, eating away at the
battery while I cannot use its benefits.&lt;/p&gt;

&lt;p&gt;Since Ubuntu 12.10, I had been relying on &lt;a href=&quot;https://github.com/Bumblebee-Project/Bumblebee/wiki&quot;&gt;Bumblebee&lt;/a&gt; to turn off
the Nvidia card on boot, and use the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;optirun&lt;/code&gt; command to run
GPU-hungry applications under the Nvidia card whenever I needed.  It
was a setup good enough for my needs.&lt;/p&gt;

&lt;p&gt;Alas, under 14.04, installing Bumblebee only turned off the Nvidia
card, without the ability to run it later using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;optirun&lt;/code&gt;.  Also, the
laptop would not suspend any more.  Bummer.&lt;/p&gt;

&lt;p&gt;After many hours of trying different Nvidia drivers and mucking with
the Bumblebee configuration, I failed to find a way to get suspend to
work.&lt;/p&gt;

&lt;h3 id=&quot;getting-suspend-to-work-with-nvidia-prime&quot;&gt;Getting suspend to work with nvidia-prime&lt;/h3&gt;

&lt;p&gt;So I purged Bumblebee and the existing Nvidia drivers.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;sudo ppa-purge ppa:bumblebee/stable
sudo apt-get purge 'bumblebee*'
sudo apt-get purge 'nvidia*'
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Then tried the alternative that’s apparently officially supported in
14.04, &lt;a href=&quot;https://wiki.ubuntu.com/X/Config/HybridGraphics&quot;&gt;nvidia-prime&lt;/a&gt;.  All I needed was:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;sudo apt-get install nvidia-331
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;On reboot, I was running under the Nvidia card, but the Intel
controller was also correctly handling brightness controls.  Suspend
also worked.  I went to the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nvidia-settings&lt;/code&gt; panel to toggle the
Nvidia card off, and logged out as requested to apply the change.&lt;/p&gt;

&lt;p&gt;And then I was greeted by the “low-graphics mode” dialog, unable to
get log back using only the Intel controller.  As it turns out, it
appears to be a race condition between the nvidia driver and lightdm,
as logging into tty1 and restarting lightdm did the trick.&lt;/p&gt;

&lt;p&gt;To make lightdm wait a bit longer on boot and on toggling, I added a
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sleep 2&lt;/code&gt; just before the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;exec lightdm&lt;/code&gt; line in
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/etc/init/lightdm.conf&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now the Nvidia card is turned off and can be turned on after logging
out.  More importantly, suspend worked correctly again.  Though it
now takes ~20 seconds to put the machine in sleep mode, I can live
with this minor hassle.&lt;/p&gt;

&lt;h3 id=&quot;turning-wifi-on-after-resume&quot;&gt;Turning wifi on after resume&lt;/h3&gt;

&lt;p&gt;And then I wireless was turned off on resume.  Toggling the physical
switch did not help.  Thankfully, the solution can &lt;a href=&quot;http://askubuntu.com/questions/452826/wireless-networking-not-working-after-resume-in-ubuntu-14-04&quot;&gt;easily be found&lt;/a&gt;
by browsing the web.&lt;/p&gt;

</content>
  </entry>
  
  <entry>
	 <title>Functional JavaScript Wizardry</title>
	 <link href="https://0xc0de.fr/2014/03/28/functional-javascript-wizardry.html"/>
	 <updated>2014-03-28T00:00:00+00:00</updated>
	 <id>https://0xc0de.fr/2014/03/28/functional-javascript-wizardry</id>
	 <content type="html">&lt;p&gt;Since EcmaScript 5, JavaScript has gotten nice collection functions
like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Array.map&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Array.forEach&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Array.filter&lt;/code&gt; and the
&lt;a href=&quot;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array#Iteration_methods&quot;&gt;usual suspects&lt;/a&gt;.  However, they can be a bit clunky to use
when dealing with objects.  Look at the following piece of code:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-js&quot; data-lang=&quot;js&quot;&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt; fun&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt; ction &lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;  al&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;trim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;})&lt;/span&gt;
                           &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;join&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;//=&amp;gt; 'functional'&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;I map over an array of strings, trim each string, then join the
resulting array of trimmed strings.  Except it’s a bit too verbose for
my tastes.  I would like to write (and read) this instead:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-js&quot; data-lang=&quot;js&quot;&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt; fun&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt; ction &lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;  al&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;trim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;join&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;The straightforward way to do that is to define the anonymous wrapper
function that calls &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;trim&lt;/code&gt; beforehand:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-js&quot; data-lang=&quot;js&quot;&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;trim&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;trim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt; fun&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt; ction &lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;  al&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;trim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;join&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;//=&amp;gt; 'functional'&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Okay.  Except that I don’t want to call just &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;trim&lt;/code&gt;, I might call any
function on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Array&lt;/code&gt;, or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Object&lt;/code&gt;, or any custom created object for
that matter.  And I certainly don’t want to write stupid wrapper
functions each time.&lt;/p&gt;

&lt;p&gt;The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;trim&lt;/code&gt; function already has a name: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;String.prototype.trim&lt;/code&gt;.  But
unfortunately, I cannot just write the following:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-js&quot; data-lang=&quot;js&quot;&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt; fun&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt; ction &lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;  al&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;prototype&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;trim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;join&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// TypeError: String.prototype.trim called on null or undefined&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Because here &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map&lt;/code&gt; will call &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;trim&lt;/code&gt; on each string in the array, but
without any context object (a value for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;this&lt;/code&gt;).  Instead, I would
like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map&lt;/code&gt; to pass each string &lt;em&gt;as&lt;/em&gt; the context object to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;trim&lt;/code&gt;, in
turn.  We could redefine a custom version of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map&lt;/code&gt; that works that
way, but there’s a simpler solution.&lt;/p&gt;

&lt;h3 id=&quot;abstracting-wrappers&quot;&gt;Abstracting wrappers&lt;/h3&gt;

&lt;p&gt;What we really need is a function that transforms method calls into
function calls.  We need a function &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;m2f&lt;/code&gt; to transform &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;o.m(args)&lt;/code&gt;
calls into &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;f(o, args)&lt;/code&gt; calls, where &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;f = m2f(o.m)&lt;/code&gt;.  Easy enough
in JavaScript (bar the ugliness of dealing with the arguments special
object):&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-js&quot; data-lang=&quot;js&quot;&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;m2f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;fun&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;cm&quot;&gt;/* args */&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;args&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[].&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;slice&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;apply&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;arguments&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;fun&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;apply&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;shift&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;trim&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;m2f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;prototype&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;trim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt; fun&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt; ction &lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;  al&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;trim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;join&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;//=&amp;gt; 'functional'&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Good!  Now I can use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;m2f&lt;/code&gt; on any “method”, and get back a more
flexible function.  For instance, I can use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map&lt;/code&gt; on any “array-like”
objects like strings, the special &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;arguments&lt;/code&gt; object, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;NodeList&lt;/code&gt; and
so on:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-js&quot; data-lang=&quot;js&quot;&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;map&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;m2f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([].&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// Saving a few chars over `Array.prototype.map`&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;up&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;m2f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;prototype&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;toUpperCase&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;abc&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;up&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// [ 'A', 'B', 'C' ]&lt;/span&gt;

&lt;span class=&quot;nx&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;({&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;albatros&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;albatros&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;},&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;up&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// [ 'ALBATROS', 'ALBATROS' ]&lt;/span&gt;

&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(){&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;arguments&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;up&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;abc&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;z&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// [ '1', '2', 'ABC', 'Z' ]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;And of course, I can use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;m2f&lt;/code&gt; on any method, not just &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map&lt;/code&gt;.  Here I
populate an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;array&lt;/code&gt; object with functions from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Array.prototype&lt;/code&gt; that
work on any array-like object:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-js&quot; data-lang=&quot;js&quot;&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;array&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{};&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;isFunction&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;typeof&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;===&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;names&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;getOwnPropertyNames&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;Array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;prototype&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;names&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;forEach&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;isFunction&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;Array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;prototype&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]))&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;m2f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;Array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;prototype&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;})&lt;/span&gt;

&lt;span class=&quot;nx&quot;&gt;array&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// { join: [Function],&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;//   pop: [Function],&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;//   ... }&lt;/span&gt;

&lt;span class=&quot;nx&quot;&gt;array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;slice&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;//=&amp;gt; [ 2 ]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;This replicates the Mozilla-specific “Array generics” extension.  This
extension is &lt;a href=&quot;https://code.google.com/p/v8/issues/detail?id=308&quot;&gt;not available&lt;/a&gt; in V8-powered environment like
nodejs.&lt;/p&gt;

&lt;p&gt;This &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;m2f&lt;/code&gt; function is quite handy when you are used to a functional
style of programming.  You can find it in Fogus’
&lt;a href=&quot;https://github.com/funjs/book-source/blob/master/chapter04.js#L59&quot;&gt;&lt;em&gt;Functional JavaScript&lt;/em&gt;&lt;/a&gt; under the name ‘invoker’.  However,
there is another way to define &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;m2f&lt;/code&gt; using a powerful corner of
EcmaScript 5.1, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bind&lt;/code&gt;.&lt;/p&gt;

&lt;h3 id=&quot;calling-and-binding-functions&quot;&gt;Calling and binding functions&lt;/h3&gt;

&lt;p&gt;Earlier, I said I could not just write:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-js&quot; data-lang=&quot;js&quot;&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;trim&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;prototype&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;trim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Because JavaScript methods are really just functions.  Now my &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;trim&lt;/code&gt;
is pretty useless on its own, because it expects a context object.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-js&quot; data-lang=&quot;js&quot;&gt;&lt;span class=&quot;nx&quot;&gt;trim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;  a &lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// TypeError: String.prototype.trim called on null or undefined&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;There’s a way to provide one to it, using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;call&lt;/code&gt;:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-js&quot; data-lang=&quot;js&quot;&gt;&lt;span class=&quot;nx&quot;&gt;trim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;call&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;  a &lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// 'a'&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;call&lt;/code&gt; function takes a context, some arguments, and calls the
receiver (a function) with them.&lt;/p&gt;

&lt;p&gt;Another way to provide the context is to use the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bind&lt;/code&gt; function.  As
its name implies, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bind&lt;/code&gt; will return a function with the supplied
context bound to it.  Let’s see how it works:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-js&quot; data-lang=&quot;js&quot;&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;trimA&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;trim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;bind&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;  a &lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;trimA&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// 'a'&lt;/span&gt;

&lt;span class=&quot;nx&quot;&gt;trimA&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;call&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// 'a'&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Whenever &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;trimA&lt;/code&gt; is called, it will call &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;trim&lt;/code&gt; with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;a&quot;&lt;/code&gt; as context,
even if &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;trimA&lt;/code&gt; is supplied another context with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;call&lt;/code&gt;.&lt;/p&gt;

&lt;h3 id=&quot;binding-call&quot;&gt;Binding call&lt;/h3&gt;

&lt;p&gt;Where does that lead us?  Well, if we look at the precedent example
again,&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-js&quot; data-lang=&quot;js&quot;&gt;&lt;span class=&quot;nx&quot;&gt;trim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;call&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;  a &lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;we see that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;trim.call&lt;/code&gt; is essentially the function we want.  But we’d
rather use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;trim&lt;/code&gt; directly, without calling &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;call&lt;/code&gt; on it.  How to
achieve that?&lt;/p&gt;

&lt;p&gt;Notice that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;call&lt;/code&gt; is itself a method call.  So we can extract it on
its own, and use it to call &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;trim&lt;/code&gt;.  But since it’s an extracted
method, we still need to use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;call&lt;/code&gt; on it,&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-js&quot; data-lang=&quot;js&quot;&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;call&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;trim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;call&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;nx&quot;&gt;call&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;trim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;  a &lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// TypeError: object is not a function&lt;/span&gt;

&lt;span class=&quot;nx&quot;&gt;call&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;call&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;trim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;  a &lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// 'a'&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Since we are calling &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;call&lt;/code&gt; on itself, the notation is a bit
overloaded, but it works.  Note that now, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;trim&lt;/code&gt; is just an argument;
we can put any other method in there,&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-js&quot; data-lang=&quot;js&quot;&gt;&lt;span class=&quot;nx&quot;&gt;call&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;call&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([].&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;abc&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;up&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// [ 'A', 'B', 'C' ]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;In fact, there’s nothing tying &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;call&lt;/code&gt; to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;trim&lt;/code&gt;.  We could have
defined &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;call&lt;/code&gt; without &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;trim&lt;/code&gt;, by taking it on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Function.prototype&lt;/code&gt;
directly,&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-js&quot; data-lang=&quot;js&quot;&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;call&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;prototype&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;call&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Nevertheless, the greatest benefit of this transformation is that we
can now use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bind&lt;/code&gt; to fix the first argument of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;call&lt;/code&gt; to a specific
function.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-js&quot; data-lang=&quot;js&quot;&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;map&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;call&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;bind&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([].&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;nx&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;abc&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;up&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// [ 'A', 'B', 'C' ]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;And we just redefined &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;m2f&lt;/code&gt;!&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-js&quot; data-lang=&quot;js&quot;&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;m2f&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;call&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;bind&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;map&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;m2f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([].&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// TypeError: Bind must be called on a function&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Except it doesn’t work … because we did not provide a context to
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bind&lt;/code&gt;.  Putting it all together,&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-js&quot; data-lang=&quot;js&quot;&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;call&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;prototype&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;call&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;bind&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;call&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;bind&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;m2f&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;bind&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;bind&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;call&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Alternatively, since &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bind&lt;/code&gt; is not tied to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;call&lt;/code&gt;, it might be clearer
to just write:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-js&quot; data-lang=&quot;js&quot;&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;call&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;prototype&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;call&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;bind&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;prototype&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;bind&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;m2f&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;bind&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;bind&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;call&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Which finally gives this one-liner gem:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-js&quot; data-lang=&quot;js&quot;&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;m2f&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;prototype&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;bind&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;bind&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;Function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;prototype&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;call&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;A very useful tool for writing terse JavaScript.&lt;/p&gt;

&lt;p&gt;A variant of this form was given by
&lt;a href=&quot;https://twitter.com/littlecalculist/status/125413301965438976&quot;&gt;Dave Herman&lt;/a&gt;
and explained by
&lt;a href=&quot;https://variadic.me/posts/2013-10-22-bind-call-and-apply-in-javascript.html&quot;&gt;Erik Kronberg&lt;/a&gt;,
which I read thrice without really understanding what was going on.
This article is my attempt to derive this magic one-liner in a way
that makes sense for me.&lt;/p&gt;

</content>
  </entry>
  
  <entry>
	 <title>Walking</title>
	 <link href="https://0xc0de.fr/2013/09/26/walking.html"/>
	 <updated>2013-09-26T00:00:00+00:00</updated>
	 <id>https://0xc0de.fr/2013/09/26/walking</id>
	 <content type="html">&lt;p&gt;I like to walk.  Walking, at my own pace, sometimes for hours.
Walking, under the trees, alongside a river, on the beach, or in the
mountains.  Walking, alone with my thoughts, or chatting with my
friends.  Walking, among the crowd, to feel like a part of the world;
or walking in the countryside, to get away from it all.&lt;/p&gt;

&lt;p&gt;I don’t walk for the exercise; though it’s not an unhealthy leisure.
I don’t walk as a means of transportation, to &lt;em&gt;get somewhere&lt;/em&gt;; there
are far more efficient ways to transit.  I could run and I could
cycle, that would get me farther, further away, faster.  But I just
want to take my time, to take the time to &lt;em&gt;not be anywhere&lt;/em&gt;.  When I
walk, I’m out there.  No duties, no tasks, no deadlines, no pressure.
No information, no social conventions, no morale, no legitimacy,
nothing.  No me.&lt;/p&gt;

&lt;p&gt;Walking, you appreciate the beauty of nature, the richness and
complexity of a tree’s trunk, of a flower’s corolla, of a ladybug’s
elytra.  Walking, I can hear, see, smell the world without feeling
overwhelmed.  Walking gives me perspective.&lt;/p&gt;

&lt;p&gt;I wish research was more like walking.  I feel like a lot of the
organization around research is goal-oriented.  You must have a goal
before deciding which path to take.  A goal that is enticing enough to
get you grants.  Once the goal is set, you must take the most
rewarding route to get there: the one that holds the greater number of
publishable results.&lt;/p&gt;

&lt;p&gt;Both pressures for grants and for publications greatly limit the
number of acceptable research trails.  You have to take the most
efficient, most trodden path with sure benefits – and you certainly
can’t take your time to contemplate the landscape.  You have to go
fast, have to be the first.  You must get in the car and take the
highway.&lt;/p&gt;

&lt;p&gt;But it’s not the goal that empowers you, it’s the journey.  Results
may get you respect, recognition, fame; but only the journey will
bring you enlightenment, joy and, ultimately, freedom.  Choose the
path for beauty and for truth.  Then, don’t rush it; take your time,
pay attention.&lt;/p&gt;

&lt;p&gt;Walk.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Inspired by:
&lt;a href=&quot;http://zenpencils.com/comic/128-bill-watterson-a-cartoonists-advice/&quot;&gt;Bill Waterson&lt;/a&gt;,
&lt;a href=&quot;http://www.brainpickings.org/index.php/2012/03/19/einstein-on-kindness/&quot;&gt;Alfred Einstein&lt;/a&gt;,
&lt;a href=&quot;http://theoatmeal.com/comics/running&quot;&gt;The Oatmeal&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;
</content>
  </entry>
  
  <entry>
	 <title>My Setup</title>
	 <link href="https://0xc0de.fr/2012/04/21/my-setup.html"/>
	 <updated>2012-04-21T00:00:00+00:00</updated>
	 <id>https://0xc0de.fr/2012/04/21/my-setup</id>
	 <content type="html">&lt;p&gt;There’s this neat concept over at &lt;a href=&quot;http://usesthis.com/&quot;&gt;The Setup&lt;/a&gt; where (more-or-less)
famous people describe their computer setup.  Every interviewee get
asked the same four questions:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Who are you and what do you do?&lt;/li&gt;
  &lt;li&gt;What hardware are you using?&lt;/li&gt;
  &lt;li&gt;And what software?&lt;/li&gt;
  &lt;li&gt;What would be you dream setup?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;By browsing their answers, you can finally find out which hardware and
software help your personal heroes (or nemeses) conceive their great
works.  They usually list their day-to-day gizmos as well as their
invaluable instruments.&lt;/p&gt;

&lt;p&gt;However, if you expect them to use life-changing tools you never heard
of, you’d better lower you expectations or you will probably be
disappointed.  Most of the hardware setups presented on the site are
merely a subset of the Apple product line.  Their software stack is
usually much more interesting, since they help solving specific
use-cases.  You may discover unsuspected gems which will end up
completing your own setup.&lt;/p&gt;

&lt;p&gt;I wanted to describe my own setup for future reference, and since I’m
unlikely to be interviewed by the team behind the site, I’ll answer
the questions right here.&lt;/p&gt;

&lt;h3 id=&quot;who-are-you-and-what-do-you-do&quot;&gt;Who are you and what do you do?&lt;/h3&gt;

&lt;p&gt;I’m a CS student with a knack for languages.  My main activities are
learning and research; both are powered by reading and writing articles,
books and computer programs.&lt;/p&gt;

&lt;h3 id=&quot;what-hardware-are-you-using&quot;&gt;What hardware are you using?&lt;/h3&gt;

&lt;p&gt;Although I have a desktop machine, nowadays I mostly use my &lt;a href=&quot;http://www.sony.co.uk/product/vn-z-series&quot;&gt;Vaio Z&lt;/a&gt;
laptop.  It’s a fast, comfortable, elegant and very light machine.
All components are high-end.  The screen is gorgeous, with very
accurate colors, and a large 1600x900 resolution under 13”1 of matte
glass.  The keyboard is similar to those found in Apple products, and
it’s my favorite keyboard yet: responsive and quiet.  Though my
standards might be a bit on the low end, since I’ve never tried a
mechanical keyboard.  The battery usually lasts around 4 hours with
WiFi on, which is sufficient for my current needs.  On the inside, it
hides a dual-core i5 M560 running at 2.67Ghz and 4Gb of RAM.  The
killer feature though is the 128Gb SSD.  I fear the speed upgrade it
brought made me less patient around computers running on hard disk
drives.&lt;/p&gt;

&lt;p&gt;There are two downsides to this machine however.  The first is the
price.  Depending on the configuration, and especially if you add an
SSD, it can add up to quite a lot: the price range is $1600~$4500.
Looking at the competition, and after more than one year of daily use,
I don’t regret buying it, but the investment is not trivial.  The
second drawback is the poor support of the NVIDIA graphics card under
Linux.  There are two graphics solutions: one integrated Intel
chipset, capable of playing video and modest games, and an NVIDIA
GT330M when more graphics muscle is required.  Switching between the
“stamina” mode and the “performance” mode works well under Windows,
but Linux only supports the Intel chipset.  Though I need to state
that every single other thing works like a charm under Linux, which is
laudable considering the sophistication of the hardware and the lack
of cooperation from Sony.  This last point alone has convinced me to
consider thoroughly all options before buying another Sony product.&lt;/p&gt;

&lt;p&gt;My companion computer which lives in my pocket is a Samsung Galaxy
Nexus.  I’m happy with it for the moment, since it’s fast and useful.
The display is large and comfortable to browse the web with.  I just
hope I don’t have to replace it too soon, since these costly gadgets
tend to have a very short shelf life.&lt;/p&gt;

&lt;h3 id=&quot;and-what-software&quot;&gt;And what software?&lt;/h3&gt;

&lt;p&gt;I’ve used &lt;a href=&quot;http://www.ubuntu.com/&quot;&gt;Ubuntu&lt;/a&gt; continuously since version 8.04 I think.  The
Debian package system works well, the system is stable, but more
importantly, everything just works.  No need to muck with drivers or
config files for things I don’t care about.  WiFi, external monitors,
SD card and even the proprietary Memory Stick reader.  My laptop runs
Ubuntu 11.04 since the upgrade from 10.10 to 11.04 broke a few things
I had to fix afterward, and I don’t want to go through trouble again.&lt;/p&gt;

&lt;p&gt;However, when I &lt;em&gt;do&lt;/em&gt; want a fine-tuned configuration, I turn over to
the latest &lt;a href=&quot;https://www.archlinux.org/&quot;&gt;Arch Linux&lt;/a&gt;.  My previous laptop couldn’t run the bloat
of a full-featured Ubuntu, so I took time going through the
&lt;a href=&quot;http://wiki.archlinux.org/index.php/Main_Page&quot;&gt;ArchWiki&lt;/a&gt; and related fora in order to build a more lightweight
system.  Since then, I’ve grown fond of this distribution.  The
rolling releases mean I don’t need to upgrade the entire system to get
a new major version of specific software.  Once you get to know your
way around, the configuration is not so bad for desktops; exotic
peripherals can take some time to get working though.  I’ve heard good
things about &lt;a href=&quot;http://www.gentoo.org/&quot;&gt;Gentoo&lt;/a&gt; as well, and might try it on a spare computer
when I find myself in a curious mood.&lt;/p&gt;

&lt;p&gt;On top of this OS, my window manager of choice is &lt;a href=&quot;http://xmonad.org/&quot;&gt;Xmonad&lt;/a&gt;.  I’ve
used &lt;a href=&quot;http://www.gnome.org/&quot;&gt;Gnome&lt;/a&gt;, &lt;a href=&quot;http://unity.ubuntu.com/&quot;&gt;Unity&lt;/a&gt;, and &lt;a href=&quot;http://openbox.org/&quot;&gt;OpenBox&lt;/a&gt; before settling on Xmonad.
The tiling paradigm aptly fits my work flow, and it liberates screen
estate from useless widgets.  Save for a thin status bar, the whole
screen is devoted to applications.  No longer do I have to move
overlapping windows out of the way.  All new windows get a share of
the current workspace, or their own workspace.  Xmonad is also deeply
customizable, though you need to be able to parse &lt;a href=&quot;http://www.haskell.org/haskellwiki/Haskell&quot;&gt;Haskell&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Growing up with a computer, I never took a good look at the keyboard
layout I was using, and just typed away until my fingers knew it by
heart.  It was only when my hands and wrists began to hurt after
repeated long typing sessions that I finally questioned the merit of
&lt;a href=&quot;https://en.wikipedia.org/wiki/AZERTY&quot;&gt;AZERTY&lt;/a&gt;.  Even though it’s the &lt;em&gt;de facto&lt;/em&gt; standard French layout,
there are letters in the French alphabet you can’t type without the
help of additional software, like œ, or É.  In addition, pairs of
brackets, braces and parentheses are never side by side, which is a
pain when programming in most languages.  There are plenty of
alternative, carefully designed layouts you can choose from.  I
personally use &lt;a href=&quot;http://colemak.com/&quot;&gt;Colemak&lt;/a&gt;, since it seemed easier to learn than
&lt;a href=&quot;https://en.wikipedia.org/wiki/Dvorak_Simplified_Keyboard&quot;&gt;Dvorak&lt;/a&gt;, while being equivalently comfortable for typing.
Switching hampered my productivity for a few months, but was worth it
in the long run.&lt;/p&gt;

&lt;p&gt;Aside from a keyboard and its layout, a good text editor is also
essential for typing greatness.  This is why my most cherished piece
of software is &lt;a href=&quot;https://www.gnu.org/software/emacs/&quot;&gt;Emacs&lt;/a&gt;.  Obviously, I edit all source code with it,
and all my version control is done by using &lt;a href=&quot;http://git-scm.com/&quot;&gt;Git&lt;/a&gt; from Emacs with
&lt;a href=&quot;http://philjackson.github.com/magit/&quot;&gt;Magit&lt;/a&gt;.  I also manage my research notes and draft articles with
&lt;a href=&quot;http://orgmode.org/&quot;&gt;Org mode&lt;/a&gt;, read and write email with &lt;a href=&quot;http://www.gnus.org/&quot;&gt;Gnus&lt;/a&gt;, navigate in projects
with &lt;a href=&quot;https://github.com/bbatsov/projectile&quot;&gt;projectile&lt;/a&gt;, &lt;a href=&quot;http://ctags.sourceforge.net/&quot;&gt;etags&lt;/a&gt; and &lt;a href=&quot;http://betterthangrep.com/&quot;&gt;ack&lt;/a&gt;…  I could write a whole
article to enumerate the ways I use Emacs and again another article to
describe my Emacs configuration file.&lt;/p&gt;

&lt;p&gt;A well-designed monospace font is a primordial part of a programmer’s
toolbox.  Again, there are many free, good-looking fonts suited for
programming to choose from.  Personally, I find the &lt;a href=&quot;http://font.ubuntu.com/&quot;&gt;Ubuntu Mono&lt;/a&gt;
font fantastic.  Before its release, I used the &lt;a href=&quot;http://dejavu-fonts.org/wiki/Main_Page&quot;&gt;DejaVu Sans Mono&lt;/a&gt;
from Bitstream, but the Ubuntu monospace variant has more charm and
perfect legibility.&lt;/p&gt;

&lt;p&gt;My email setup is a tad convoluted, but for good reason.  I use
&lt;a href=&quot;http://gmail.com/&quot;&gt;Gmail&lt;/a&gt; for my main email address.  I use IMAP for all my email
accounts since I want to read my emails from my phone, laptop or
desktop machine.  In addition, I want to regularly backup all mail in
case any funny business arises in Google’s headquarters.  Toward this
end, I copy everything with &lt;a href=&quot;http://offlineimap.org/&quot;&gt;OfflineIMAP&lt;/a&gt; to a local &lt;a href=&quot;http://www.dovecot.org/&quot;&gt;Dovecot&lt;/a&gt;
server.  Then I use the already-mentioned &lt;a href=&quot;http://www.gnus.org/&quot;&gt;Gnus&lt;/a&gt; reader to compose
and read emails.  Nowadays I also read short emails or write quick
answers from my phone.&lt;/p&gt;

&lt;p&gt;Apart from Emacs, the application I launch everyday is a web browser.
These days I’m back to &lt;a href=&quot;https://www.mozilla.org/firefox/&quot;&gt;Firefox&lt;/a&gt; (trunk builds).  I have a battery
of privacy extensions, and can sync my bookmarks, history and open
tabs with other devices.  I used &lt;a href=&quot;https://www.google.com/chrome&quot;&gt;Chrome&lt;/a&gt; for a while, but let it go
due to its smaller offering of add-ons and its annoying habit of going
to Google search instead of relying on browsing history. The speed
difference between these two is getting narrower, and the developer
tools are comparable.&lt;/p&gt;

&lt;p&gt;The remaining programs deserving mention are: most of the
&lt;a href=&quot;https://www.gnu.org/software/coreutils/&quot;&gt;GNU coreutils&lt;/a&gt;, Gnome terminal, Evince and other &lt;a href=&quot;http://www.gnome.org/&quot;&gt;Gnome&lt;/a&gt; apps,
and &lt;a href=&quot;http://www.zsh.org/&quot;&gt;zsh&lt;/a&gt;.&lt;/p&gt;

&lt;h3 id=&quot;what-would-be-your-dream-setup&quot;&gt;What would be your dream setup?&lt;/h3&gt;

&lt;p&gt;My &lt;em&gt;truly&lt;/em&gt; ideal setup would have the following characteristics:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;A large display I can read and write on for hours on end without straining my eyes,&lt;/li&gt;
  &lt;li&gt;Talking, thinking and handwriting input,&lt;/li&gt;
  &lt;li&gt;A way to conceive and develop programs without caring for minute details,&lt;/li&gt;
  &lt;li&gt;No need to wade through a hundred pages of documentation to
configure or fix something in the computer.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;While this may be ideal, it might not be realistic, yet.  Though we
live in interesting times, and I do hope that someday I can tick a few
or all of these items.&lt;/p&gt;

</content>
  </entry>
  
  <entry>
	 <title>Notes on a Thesis Defense</title>
	 <link href="https://0xc0de.fr/2011/11/27/notes-on-a-thesis-defense.html"/>
	 <updated>2011-11-27T00:00:00+00:00</updated>
	 <id>https://0xc0de.fr/2011/11/27/notes-on-a-thesis-defense</id>
	 <content type="html">&lt;p&gt;Tuesday was a very special day for my friend Karim.  He defended his
thesis.  After nearly five years of work, during which much teases and
jokes were made about his thesis pace (three years is the norm here),
he finally finished his dissertation and defended successfully.&lt;/p&gt;

&lt;p&gt;It was also a very interesting afternoon for me, as I got to watch and
learn.  Especially, I focused on the jury’s reactions during both his
talk and the following Q&amp;amp;A, as I was seated just behind the six of
them.  I also took note of specific points I think he got right during
his defense, and some others I would have done differently.&lt;/p&gt;

&lt;h3 id=&quot;focus-on-content-not-on-style&quot;&gt;Focus on content, not on style&lt;/h3&gt;

&lt;p&gt;Nearly everyone uses &lt;a href=&quot;https://bitbucket.org/rivanvx/beamer/wiki/Home&quot;&gt;Beamer&lt;/a&gt; at my University.  It’s not
particularly a joy to work with, but it produces clean slides.  The
downside is, your slides tend to look the same as those of other
Beamer users.  It’s particularly true when one doesn’t take the time
to question the default &lt;a href=&quot;http://www.informatik.uni-freiburg.de/~frank/ENG/beamer/example/Beamer-class-example8-Warsaw.pdf&quot;&gt;Warsaw theme&lt;/a&gt;.  The blue and black color
scheme has become as tiring as Times New Roman for me, and I cringe
every time I see it. And I cringed when Karim launched his talk.&lt;/p&gt;

&lt;p&gt;However, Karim quickly made me forget the atrocious theme after two
minutes or so, because the &lt;em&gt;content&lt;/em&gt; was interesting.  The theme may
be ugly, but at least it’s not distracting.&lt;/p&gt;

&lt;h3 id=&quot;only-show-whats-relevant&quot;&gt;Only show what’s relevant&lt;/h3&gt;

&lt;p&gt;At one point, Karim showed us one of his algorithms on one slide.  An
entire, forty-something lines algorithm on just one slide.  In a talk,
a slide stays between one and two minutes on screen.  I can’t
&lt;a href=&quot;https://en.wikipedia.org/wiki/Grok#In_computer_programmer_culture&quot;&gt;&lt;em&gt;grok&lt;/em&gt;&lt;/a&gt; a forty lines algorithm in two minutes.  Nevertheless,
I am able to get the gist of it if you highlight the relevant parts,
as Karim did.&lt;/p&gt;

&lt;p&gt;Still, I would have preferred a high-level description of the
algorithm rather than the whole thing.  I can always check his
dissertation for the details.&lt;/p&gt;

&lt;h3 id=&quot;state-your-point-clearly-and-hammer-it&quot;&gt;State your point clearly, and hammer it&lt;/h3&gt;

&lt;p&gt;When you have the luxury of a one-hour talk, everyone should get your
main point, even your family at the end of the room who came to cheer
on you.  This is especially true of a thesis defense.  Everyone should
know your thesis, and how you tackled it.  Make one slide with only
your thesis on it, and show it a least three times during your talk.
Start with it, show it when you switch sections, and begin your
conclusion with it.  You &lt;em&gt;don’t&lt;/em&gt; want the jury to ask you to state the
aim of your thesis during Q&amp;amp;A, so print it on a t-shirt just to be
safe.&lt;/p&gt;

&lt;h3 id=&quot;dont-worry&quot;&gt;Don’t worry&lt;/h3&gt;

&lt;p&gt;Karim worked nearly five years on his thesis.  Of the two algorithms
he offered, one was developed in the last year, and the other he
developed over the last &lt;em&gt;six months&lt;/em&gt;.  He spent the best part of four
years trying to come up with the &lt;em&gt;perfect&lt;/em&gt; model, and the &lt;em&gt;best
suited&lt;/em&gt; algorithm.  When he ran overtime, he settled on those two
algorithms and completed his thesis.  While not perfect, the end
result is still very interesting, and was enough to satisfy the jury
who awarded him his doctorate with the highest honors.&lt;/p&gt;

&lt;p&gt;A thesis is just the beginning of an academic career, not its zenith.
Try your best, but stay away from &lt;em&gt;the&lt;/em&gt; best if you want to finish
someday.  You can always continue working on the subject after your
defense.&lt;/p&gt;

&lt;p&gt;If you want to read more on the subject, Olin Shivers has
&lt;a href=&quot;http://www.ccs.neu.edu/home/shivers/diss-advice.html&quot;&gt;some good thesis advice&lt;/a&gt;, and Gian-Carlo Rota’s
&lt;a href=&quot;http://www.math.osu.edu/~nevai/MYMATH/rota_ams_notices_01_97.html&quot;&gt;&lt;em&gt;Ten Lessons I Wish I Had Been Taught&lt;/em&gt;&lt;/a&gt; is a good read on
teaching and giving presentations.&lt;/p&gt;

</content>
  </entry>
  
  <entry>
	 <title>Unity Backlight Color Algorithm</title>
	 <link href="https://0xc0de.fr/2011/09/03/unity-backlight-algorithm.html"/>
	 <updated>2011-09-03T00:00:00+00:00</updated>
	 <id>https://0xc0de.fr/2011/09/03/unity-backlight-algorithm</id>
	 <content type="html">&lt;p&gt;With the release of Ubuntu 11.04, the new &lt;a href=&quot;http://unity.ubuntu.com/&quot;&gt;Unity&lt;/a&gt; interface was
introduced to users.  While it has its issues, I have been satisfied
with it overall.  I find it cleaner than the Gnome 2 default bottom
panel, and I can quickly launch any application or find recent files
without leaving the keyboard.&lt;/p&gt;

&lt;p&gt;Ever since I began using it however, one detail caught my attention:
the colored backlight behind applications icons.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/posts/unity.png&quot; alt=&quot;Unity backlight zoom&quot; /&gt;&lt;/p&gt;

&lt;h3 id=&quot;backlight-color&quot;&gt;Backlight color&lt;/h3&gt;

&lt;p&gt;Notice how every icon has a square with rounded corners behind it?
This square is colored for currently open applications.  This is the
backlight.&lt;/p&gt;

&lt;p&gt;First, notice the backlight color is not constant: Emacs has a rich
purple matching its icon, while the Firefox icon has an orange
backlight, but a deeper orange than Update Manager (the box with the
orange arrow).  Thus, the backlight color seems to match the icon
contents.  But how exactly is the color determined?&lt;/p&gt;

&lt;h3 id=&quot;algorithm-hypothesis&quot;&gt;Algorithm hypothesis&lt;/h3&gt;

&lt;p&gt;Initially, we might guess that the backlight algorithm picks the
icon’s most abundant color.  It is a good fit for the icons of Emacs,
Firefox and Empathy (the two blue heads).  However, it falls flat when
considering the Update Manager icon, as the orange arrow is smaller
than the beige box, but the backlight color is orange and not beige.
This is also demonstrated by GIMP’s icon.  Our hypothesis is not far
from the truth however.&lt;/p&gt;

&lt;p&gt;The catch is, we did not define what “most abundant color” meant.
Should we scan the icon and keep a count of each pixel color,
returning the most frequent color?  That could lead to surprising
results when considering high resolution icons with gradients. Our
eyes might easily differentiate between the blue globe and the humping
fire fox around it, but such an algorithm would not.  Individual RGBA
values are all the algorithm considers, but humans look at the whole
picture at once and see continents, a tail, a paw, ears and a muzzle.
There are algorithms for detecting image features, but they are far
too complex for the task at hand.  In fact, the actual algorithm is
quite simple.&lt;/p&gt;

&lt;h3 id=&quot;the-code&quot;&gt;The code&lt;/h3&gt;

&lt;p&gt;Diving into the &lt;a href=&quot;http://bazaar.launchpad.net/~ubuntu-branches/ubuntu/natty/unity/natty/view/head:/src/LauncherIcon.cpp#L223&quot;&gt;source code&lt;/a&gt; for Unity, we find the relevant function
in the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;LauncherIcon&lt;/code&gt; class, named &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ColorForIcon&lt;/code&gt;.  Here is
the entire function:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-c--&quot; data-lang=&quot;c++&quot;&gt;&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LauncherIcon&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ColorForIcon&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GdkPixbuf&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pixbuf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                                 &lt;span class=&quot;n&quot;&gt;nux&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Color&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;background&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                                 &lt;span class=&quot;n&quot;&gt;nux&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Color&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;glow&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;width&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;gdk_pixbuf_get_width&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pixbuf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;height&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;gdk_pixbuf_get_height&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pixbuf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;row_bytes&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;gdk_pixbuf_get_rowstride&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pixbuf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

  &lt;span class=&quot;kt&quot;&gt;long&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rtotal&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;gtotal&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;btotal&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;total&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;


  &lt;span class=&quot;n&quot;&gt;guchar&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;img&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;gdk_pixbuf_get_pixels&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pixbuf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;width&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;height&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;guchar&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pixels&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;img&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;row_bytes&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;guchar&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pixels&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;guchar&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;g&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pixels&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;guchar&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pixels&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;guchar&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pixels&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

      &lt;span class=&quot;kt&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;saturation&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MAX&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;MAX&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;g&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;MIN&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;MIN&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;g&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;255.0&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
      &lt;span class=&quot;kt&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;relevance&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;.1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;.9&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;255.0&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;saturation&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

      &lt;span class=&quot;n&quot;&gt;rtotal&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;guchar&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;relevance&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;gtotal&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;guchar&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;g&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;relevance&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;btotal&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;guchar&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;relevance&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

      &lt;span class=&quot;n&quot;&gt;total&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;relevance&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;255&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;kt&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;g&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;h&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;r&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rtotal&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;total&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;g&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;gtotal&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;total&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;btotal&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;total&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;n&quot;&gt;nux&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;RGBtoHSV&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;g&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;h&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;.15&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.65&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.90&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;n&quot;&gt;nux&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;HSVtoRGB&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;g&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;h&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;background&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nux&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Color&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;g&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

  &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;nux&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;HSVtoRGB&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;g&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;h&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;glow&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nux&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Color&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;g&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;The code is rather straightforward.  Even without GDK experience, the
calls are self-explanatory.  The backlight color is close to a mean of
each pixel’s color, except each color channel is weighted by a
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;relevance&lt;/code&gt; factor before contributing to the icon total.  The
relevance of each pixel is proportional to its saturation and alpha
value.  This explains the orange backlight for Update Manager: even
though the beige box is larger, the orange arrow is more saturated,
weighting more than beige in the backlight total.  Thus the result is
more orange than beige, but not quite the same backlight color as for
the Firefox icon, which has more saturated orange pixels.&lt;/p&gt;

&lt;p&gt;Finally, there is also some saturation normalization:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-c--&quot; data-lang=&quot;c++&quot;&gt;&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;.15&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.65&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.90&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Every backlight color that is saturated enough will be normalized to
65%.  I guess this is done in order to achieve a more unified feel, and
to avoid highly-saturated backlight colors.&lt;/p&gt;

</content>
  </entry>
  
  <entry>
	 <title>Programming Languages Names Etymology</title>
	 <link href="https://0xc0de.fr/2011/08/27/programming-etymology.html"/>
	 <updated>2011-08-27T00:00:00+00:00</updated>
	 <id>https://0xc0de.fr/2011/08/27/programming-etymology</id>
	 <content type="html">&lt;p&gt;Do you ever wonder where the name of your favorite programming
language comes from?  I do.  The first to struck me was Python.&lt;/p&gt;

&lt;h3 id=&quot;why-python&quot;&gt;Why Python?&lt;/h3&gt;

&lt;p&gt;At first we might associate it with the large, constricting snake,
star of many B-movies about people in beige shorts being swallowed
whole by not-so-mythological beasts.  The &lt;a href=&quot;http://www.python.org/&quot;&gt;official logo&lt;/a&gt; strongly
indicates that this is the intended meaning behind the name.  The
actual origin has a more &lt;em&gt;British&lt;/em&gt; flair, as it comes from the Monty
Python’s Flying Circus BBC show.  Quoting Guido van Rossum, the
language’s author, in &lt;a href=&quot;http://www.python.org/doc/essays/foreword/&quot;&gt;Foreword to &lt;em&gt;Programming Python&lt;/em&gt;&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Over six years ago, in December 1989, I was looking for a “hobby”
programming project that would keep me occupied during the week around
Christmas. My office (a government-run research lab in Amsterdam)
would be closed, but I had a home computer, and not much else on my
hands. I decided to write an interpreter for the new scripting
language I had been thinking about lately: a descendant of ABC that
would appeal to Unix/C hackers. I chose Python as a working title for
the project, being in a slightly irreverent mood (and a big fan of
Monty Python’s Flying Circus).&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Jolly good!  As a testament to this ancestry, the literature
concerning the Python language often references Monty Python skits (see
the &lt;a href=&quot;http://docs.python.org/tutorial/controlflow.html#keyword-arguments&quot;&gt;official tutorial&lt;/a&gt; for an example), but seldom mentions
reptiles.&lt;/p&gt;

&lt;h3 id=&quot;its-not-a-snake--or-is-it&quot;&gt;It’s not a snake … or is it?&lt;/h3&gt;

&lt;p&gt;But wait, where did the snake logo came from then?  There must be some
connection, otherwise it would not ornate the official homepage of the
language. Guido van Rossum provides the answer in
&lt;a href=&quot;http://www.computerworld.com.au/article/255835/a-z_programming_languages_python/&quot;&gt;an interview with Computer World&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;Are you a Monty Python fan (given the name and other elements of the
language derive from Monty Python’s Flying Circus)?&lt;/strong&gt;&lt;/p&gt;

  &lt;p&gt;Yes, this is where I took the language’s name. The association with
the snake of the same name was forced upon me by publishers who didn’t
want to license Monty-Python artwork for their book covers. I’m also
into the Hitchhiker’s Guide to the Galaxy, though I’m not into much
other staples of geek culture (e.g. no sci-fi or fantasy, no role
playing games, and definitely no computer gaming).&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So, Monty Python artwork licensing difficulties on Python books
introduced the association with snakes.  I guess the serpentine
imagery stuck, and ended up replacing the &lt;a href=&quot;https://upload.wikimedia.org/wikipedia/commons/7/72/Python_logo_1990s.svg&quot;&gt;legacy logo&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I wonder which publishers van Rossum refers to though.  Perhaps
&lt;a href=&quot;http://oreilly.com/&quot;&gt;the publisher&lt;/a&gt; of the aforementioned &lt;em&gt;Programming Python&lt;/em&gt;, who
coincidentally happens to favor putting animals on their book covers.
In that case, the elegant compromise would have been to choose a
&lt;a href=&quot;http://pythonline.com/youtube_archive/parrot-sketch&quot;&gt;dead parrot&lt;/a&gt;.&lt;/p&gt;

</content>
  </entry>
  

</feed>
