<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
    <title>Lucentbeing.com</title>
    <link href="http://lucentbeing.com/writing/archives/atom.xml" rel="self" />
    <link href="http://lucentbeing.com" />
    <id>http://lucentbeing.com/writing/archives/atom.xml</id>
    <author>
        <name>P.C. Shyamshankar</name>
        <email>sykora@lucentbeing.com</email>
    </author>
    <updated>2014-09-12T00:00:00Z</updated>
    <entry>
    <title>Nested Lambdas and Move Capture in C++14</title>
    <link href="http://lucentbeing.com/writing/archives/nested-lambdas-and-move-capture-in-cpp-14/" />
    <id>http://lucentbeing.com/writing/archives/nested-lambdas-and-move-capture-in-cpp-14/</id>
    <published>2014-09-12T00:00:00Z</published>
    <updated>2014-09-12T00:00:00Z</updated>
    <summary type="html"><![CDATA[<article id="entry">
  <header>
    <hgroup>
      <h1><a href="/writing/archives/nested-lambdas-and-move-capture-in-cpp-14/">Nested Lambdas and Move Capture in C++14</a></h1>
      
    </hgroup>
    <div id="meta">
      <div class="date">Friday, September 12, 2014</div>
      <div class="tags"><a href="/writing/tags/cpp/">C++</a> / <a href="/writing/tags/programming/">programming</a></div>
    </div>
  </header>

  <hr>

  <div id="detail">
    <p>Anonymous functions, or <em>lambdas</em>, were introduced in C++11 as a convenient, lightweight syntax for creating one-off functions. What excited me most about this development was that I could now compile a functional language into C++ by constructing a more faithful embedding of higher-order functions, without requiring me to deal with issues like closures and lifting. It wasn’t perfect though, and there were a number of warts that made lambdas unusable for anything other than the simplest cases.</p>
<p>In this post I’m going to motivate C++14’s <em>initialized lambda capture</em> as a solution to one of the numerous problems hitting one of my most common use-cases: nested lambdas.</p>
<h3 id="take-1-pass-by-value-capture-by-value">Take #1: Pass by Value, Capture by Value</h3>
<p>Consider a simple haskell-esque function:</p>
<div class="sourceCode"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span class="ot">add ::</span> <span class="dt">Int</span> <span class="ot">-&gt;</span> <span class="dt">Int</span> <span class="ot">-&gt;</span> <span class="dt">Int</span>
add <span class="fu">=</span> \x <span class="ot">-&gt;</span> \y <span class="ot">-&gt;</span> x <span class="fu">+</span> y</code></pre></div>
<p>This translates into a correspondingly simple C++11 lambda as follows:</p>
<div class="sourceCode"><pre class="sourceCode cpp"><code class="sourceCode cpp"><span class="kw">auto</span> add = [] (<span class="dt">int</span> x) {
    <span class="kw">return</span> [x] (<span class="dt">int</span> y) {
        <span class="kw">return</span> x + y;
    }
}</code></pre></div>
<p>On the face of it, this doesn’t look so bad – an outer lambda which takes an argument and returns an inner lambda which takes its own argument and while capturing the outer argument. This function can be called as <code>add(5)(2)</code>, in the usual curried form.</p>
<p>On closer inspection, things can get less than pretty. Specifically, see if you can spot the number of times the value <code>x</code> is actually copied before the addition is performed.</p>
<p>Since <code>add</code> takes <code>x</code> by value, that’s potentially where the first copy happens. <code>x</code> is then copied again, when it is captured in the inner lambda. That’s two copies, when at worst we should only have one (the outer one), and at best none at all.</p>
<p>There are several methods to eliminate some or all of these copies, methods that are available in C++11. They all have some drawbacks, making you forfeit either convenience, semantics or safety. Let’s take a look at a few of these.</p>
<h3 id="take-2-pass-by-value-capture-by-reference">Take #2: Pass by Value, Capture by Reference</h3>
<p>The obvious way to eliminate the capture copy is to capture by reference, instead of by value. After all, that’s what it’s there for. This leads to issues however, because we’re allowing a reference to a stack-allocated value escape its scope. Here’s the same example, capturing <code>x</code> by reference:</p>
<div class="sourceCode"><pre class="sourceCode cpp"><code class="sourceCode cpp"><span class="kw">auto</span> add = [] (<span class="dt">int</span> x) {
    <span class="kw">return</span> [&amp;x] (<span class="dt">int</span> y) {
        <span class="kw">return</span> x + y;
    }
}</code></pre></div>
<p>While <code>x</code> is no longer copied at the capture point, this opens us up to a whole host of unforeseen behaviors. Since <code>x</code> is stack allocated, it technically doesn’t exist after <code>add</code> returns. This means that any piece of code that can conceivably call the inner lambda is already dealing with a reference to a memory location that doesn’t necessarily contain the value it did when the lambda was created. At best you’ll get the wrong answer immediately; at worst you’ll get it sometime later where you can no longer correlate the error and the cause.</p>
<h3 id="take-3-pass-by-reference-capture-by-reference">Take #3: Pass by Reference, Capture by Reference</h3>
<p>What if <code>x</code> <em>were</em> guaranteed to exist after <code>add</code> returned? This would solve the inner lambda’s problems, but the only way to accomplish this to force <code>add</code> to accept its own argument by reference.</p>
<div class="sourceCode"><pre class="sourceCode cpp"><code class="sourceCode cpp"><span class="kw">auto</span> add = [] (<span class="dt">int</span>&amp; x) {
    <span class="kw">return</span> [&amp;x] (<span class="dt">int</span> y) {
        <span class="kw">return</span> x + y;
    }
}</code></pre></div>
<p>Combined with reference capture, this eliminates <em>both</em> copies. This is not however without its own problems. The semantic issue from the previous solution still exists, since we are still capturing in the inner lambda by reference.</p>
<p>A more obvious problem is that we can no longer call <code>add</code> with r-value arguments, such as temporaries and literals. For example, we can’t call <code>add(5)(2)</code>, as <code>5</code> isn’t an l-value, and cannot therefore be passed by reference. We’re forced to do one of two things: declare the argument ahead of time (<code>int x = 5; add(x)(2)</code>), or use constant references.</p>
<h3 id="take-4-pass-by-constant-reference-capture-by-reference">Take #4: Pass by Constant Reference, Capture by Reference</h3>
<p>Accepting an argument by constant reference allows us to pass in both l-values and r-values – the compiler extends the lifetime of an r-value by just long enough that things work out. Our <code>add</code> function now looks like:</p>
<div class="sourceCode"><pre class="sourceCode cpp"><code class="sourceCode cpp"><span class="kw">auto</span> add = [] (<span class="dt">const</span> <span class="dt">int</span>&amp; x) {
    <span class="kw">return</span> [&amp;x] (<span class="dt">int</span> y) {
        <span class="kw">return</span> x + y;
    }
}</code></pre></div>
<p>Which is great – no copies anywhere, and we can call the function however we’d like. Semantic issues still abound, but apart from the fact that the program might not do what we want it to do, we’re fine.</p>
<p>But we’re not done yet.</p>
<p>Suppose we wanted to change the definition of <code>add</code> just a little bit – increment <code>x</code> before adding it. This is a contrived example, but it’s not that difficult to imagine a function where you’d like to modify your arguments.</p>
<div class="sourceCode"><pre class="sourceCode cpp"><code class="sourceCode cpp"><span class="kw">auto</span> increment_add = [] (<span class="dt">const</span> <span class="dt">int</span>&amp; x) {
    ++x;
    <span class="kw">return</span> [&amp;x] (<span class="dt">int</span> y) {
        <span class="kw">return</span> x + y;
    }
}</code></pre></div>
<p>But this can’t work! We’ve already declared <code>x</code> to be passed in by constant reference, which means we can’t change it. What we <em>really</em> want is for <code>x</code> to be passed in by value. At this point, we realize that we’re precisely back at square one, and throw up our hands.</p>
<p>At least that’s how it was before <em>initialized lambda capture</em> in C++14.</p>
<h3 id="take-5-pass-by-value-capture-by-move">Take #5: Pass by Value, Capture by Move</h3>
<p>Initialized lambda capture gives us access to the <code>[id = expression]</code> syntax in the capture specifier list, allowing us to initialize the captured variables however we want.</p>
<p>The utility in our recurrent example is in the realization that <code>x</code> isn’t used in the outer function after the <code>return</code> statement (for obvious reasons), so it can technically be <em>moved</em> into the returned lambda.</p>
<div class="sourceCode"><pre class="sourceCode cpp"><code class="sourceCode cpp"><span class="kw">auto</span> add = [] (<span class="dt">int</span> x) {
    ++x;
    <span class="kw">return</span> [x = std::move(x)] (<span class="dt">int</span> y) {
        <span class="kw">return</span> x + y;
    }
}</code></pre></div>
<p>By moving <code>x</code> into the lambda, we prevent the capture copy, while permitting <code>x</code> to be modified in the outer function (and indeed, in the inner function as well). Additionally, by accepting <code>x</code> by value in the outer function, we permit the compiler to infer moves there as well. This will result in exactly as many copies as are necessary in order to get the desired behaviour.</p>
<p>This technique exhibits varying degrees of success for different data-types; primitive types such as <code>int</code> and <code>float</code> arguably don’t benefit that much, since a move is about as expensive as a copy. More complex types with heap-allocated resources such as <code>std::vector</code> might take arbitrarily long to copy, making a move quite appealing.</p>
<p>Lastly, for types which can <em>only</em> be moved and not copied – such as <code>std::unique_ptr</code>, this is the <em>only</em> way to capture them.</p>
  </div>
  <footer>
  </footer>
</article>
]]></summary>
</entry>
<entry>
    <title>A Guide to 256 Color Codes</title>
    <link href="http://lucentbeing.com/writing/archives/a-guide-to-256-color-codes/" />
    <id>http://lucentbeing.com/writing/archives/a-guide-to-256-color-codes/</id>
    <published>2014-04-22T00:00:00Z</published>
    <updated>2014-04-22T00:00:00Z</updated>
    <summary type="html"><![CDATA[<article id="entry">
  <header>
    <hgroup>
      <h1><a href="/writing/archives/a-guide-to-256-color-codes/">A Guide to 256 Color Codes</a></h1>
      
    </hgroup>
    <div id="meta">
      <div class="date">Tuesday, April 22, 2014</div>
      <div class="tags"><a href="/writing/tags/command-line/">command-line</a> / <a href="/writing/tags/linux/">linux</a></div>
    </div>
  </header>

  <hr>

  <div id="detail">
    <blockquote>
<p>To kick-start the new site, I’ve resurrected one of the more popular previous entries, albeit touched up, and at a new location. I apologize for all the broken links.</p>
<p>This article was originally published on November 10, 2009.</p>
</blockquote>
<p>Terminal capabilities have some of the sparsest documentation I have ever had the misfortune of having to dig through. Popular terminal applications such as vim, emacs, mutt, etc. seem to make use of every last bit of a terminal’s flexibility, but exactly how they do that always appears to be a mystery.</p>
<p>Among these capabilities are those to display a 256 color pallette.</p>
<p>Most terminals can handle at least 16 colors (8, and a bold version of each), and the ANSI codes necessary to invoke them are already a mess. Getting a terminal to display 256 colors requires character gymnastics that are altogether incomprehensible.</p>
<p>After hours of poring over non-existent documentation stretching back to a time when color actually meant ink, I think I’ve identified a pattern. In this entry, I’ll be giving you a run-down on what a 256 color escape seqence looks like, and how you can use them<a href="#fn1" class="footnoteRef" id="fnref1"><sup>1</sup></a>.</p>
<h3 id="terminal-basics">Terminal Basics</h3>
<p>The primary function of a terminal is to display text. Terminals of yore were <em>serial</em> terminals – text came in as a stream, and the terminal had one pass to do whatever it needed to do. While this limitation probably derived from a lack of resources to store more than a small buffer, most of the terminals we use today are still built in this way, so that’s what we’re stuck with.</p>
<p>Terminals use a pattern of <em>delimiters</em> to mark-up regions of text with attributes, the same way an HTML document uses start and end tags to mark-up text in a document. However terminals usually can’t express attributes as rich as HTML, nor can they afford to include a sufficiently powerful parser to do so. The delimiters for terminal attributes are therefore extremely simple, and are known as <em>escape-codes</em>.</p>
<p>Every escape code is a sequence of characters which tell the terminal to do something special. The most common escape code is one you might be familiar with: <code>\n</code>. This tells the terminal to print a newline character instead of the literal characters <code>\</code> followed by <code>n</code>.</p>
<p>For more complex text attributes such as foreground color, bold-ness, etc, the terminal keeps a small flag to remember which state it’s in. When it sees the turn-on-bold escape code, it sets that flag, and continues to display bold text until it’s given an escape code that tells it otherwise.</p>
<p>For the standard 8-color palette, these codes are fairly well documented. However, finding the codes for the 256 color palette, and furthermore the syntax in which they may be used, has driven me almost to distraction. It would seem that although they’ve managed to increase the number of colors, they haven’t added any more documentation in that direction.</p>
<h3 id="special-effects">Special Effects</h3>
<p>There are a variety of effects that you can apply to text, but not all of them are supported on every terminal/emulator. The following are the most common:</p>
<ul>
<li>Reset (Clear all text attributes, including color, effects, etc.)</li>
<li>Bold</li>
<li>Italic</li>
<li>Underline</li>
<li>Blink</li>
<li>Reverse (Swap foreground and background)</li>
</ul>
<p>Colors are themselves special effects, but they need a different code, depending on whether they are to be applied on the foreground or the background.</p>
<h3 id="anatomy-of-a-color-code">Anatomy of a Color Code</h3>
<p>The general structure of a 256 color code is:</p>
<pre class="text"><code>code       :: ^[[(value)m
value      :: (attributes);(foreground);(background)
attributes :: attribute;attributes
attribute  :: 00|01|03|04|05|07|22|23|24|25|27
foreground :: 38;05;color
background :: 48;05;color
color      :: 000-255</code></pre>
<p>A few observations about the above:</p>
<ul>
<li>The parentheses aren’t really necessary.</li>
<li>Any leading zeroes can be skipped.</li>
<li>That is a literal <code>m</code> in the code.</li>
<li>The <code>^[[</code> is an escape key (U+001B) followed by a literal <code>[</code>.</li>
</ul>
<p>The attribute codes are one of the following:</p>
<table>
<thead>
<tr class="header">
<th align="center">Effect</th>
<th align="center">Code</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td align="center">Reset</td>
<td align="center">00</td>
</tr>
<tr class="even">
<td align="center">Bold</td>
<td align="center">01</td>
</tr>
<tr class="odd">
<td align="center">Italic</td>
<td align="center">03</td>
</tr>
<tr class="even">
<td align="center">Underline</td>
<td align="center">04</td>
</tr>
<tr class="odd">
<td align="center">Blink</td>
<td align="center">05</td>
</tr>
<tr class="even">
<td align="center">Reverse</td>
<td align="center">07</td>
</tr>
<tr class="odd">
<td align="center">No Bold</td>
<td align="center">22</td>
</tr>
<tr class="even">
<td align="center">No Italic</td>
<td align="center">23</td>
</tr>
<tr class="odd">
<td align="center">No Underline</td>
<td align="center">24</td>
</tr>
<tr class="even">
<td align="center">No Blink</td>
<td align="center">25</td>
</tr>
<tr class="odd">
<td align="center">No Reverse</td>
<td align="center">27</td>
</tr>
</tbody>
</table>
<p>The “No-” variations are codes to turn off the particular effect. They’re enormously useful if you have a lot of effects, you only want to turn off one and you don’t want to use reset followed by re-enabling everything else.</p>
<p>That’s pretty much all you need to start making use of them yourself. However, examples are always nice, so:</p>
<div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash"><span class="co"># Print text normally, in color 214, which happens to be a nice orange.</span>
$<span class="kw">&gt;</span> <span class="kw">echo</span> <span class="st">&quot;^[[38;05;214mHello, World&quot;</span>

<span class="co"># Make the same text bold.</span>
$<span class="kw">&gt;</span> <span class="kw">echo</span> <span class="st">&quot;^[[(01);(38;05;214)mHello, World&quot;</span>

<span class="co"># Print underlined and italicized text, with normal foreground, and blue</span>
<span class="co"># background.</span>
$<span class="kw">&gt;</span> <span class="kw">echo</span> <span class="st">&quot;^[[(03;04);(48;05;20)mHello, World&quot;</span>

<span class="co"># Bold, blinking purple text.</span>
$<span class="kw">&gt;</span> <span class="kw">echo</span> <span class="st">&quot;^[[(01;05);(38;05;93)mHello, World&quot;</span>

<span class="co"># Simple purple text on yellow background.</span>
$<span class="kw">&gt;</span> <span class="kw">echo</span> <span class="st">&quot;^[[(38;05;93);(48;05;226)mHello, World&quot;</span></code></pre></div>
<h3 id="a-note-on-prompt-usage">A Note on Prompt Usage</h3>
<p>If you want to use these colors and effects in your prompt, you can do so, but you will need to wrap each color code in a <code>%{%}</code> construct. This tells the shell that everything in between should not actually be printed, and terminal space should not be reserved for those characters.</p>
<h3 id="spectrum-a-zsh-abstraction">Spectrum, a ZSH Abstraction</h3>
<p>I’ve abstracted these codes and put them together in a zsh function which I call <code>spectrum</code>. The code for spectrum can be found as part of my <code>~/etc</code>, on <a href="http://github.com/sykora/etc/tree/master/zsh/functions/spectrum/">github</a>.</p>
<p>To use, first either source or autoload-and-run the spectrum script. It will set up three mappings, <code>FX</code>, <code>FG</code> and <code>BG</code>. <code>FX</code> is a mapping from effect names to their corresponding codes, and FG and BG are mappings from color number to codes. All numbers are 0-padded to 3 digits, so if you want light blue, use <code>$FG[000]</code>. The source is quite readable, take a look. Note that you can’t <em>run</em> the script, as this would execute it in a different shell, and the changes wouldn’t stick.</p>
<p>The above examples then, could be replicated as follows:</p>
<div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash"><span class="co"># Provided spectrum is in your $fpath,</span>
$<span class="kw">&gt;</span> <span class="kw">autoload</span> spectrum <span class="kw">&amp;&amp;</span> <span class="kw">spectrum</span>

<span class="co"># Print text normally, in color 214, which happens to be a nice orange.</span>
$<span class="kw">&gt;</span> <span class="kw">echo</span> <span class="st">&quot;</span><span class="ot">$FG[214]</span><span class="st">Hello, World&quot;</span>

<span class="co"># Make the same text bold.</span>
$<span class="kw">&gt;</span> <span class="kw">echo</span> <span class="st">&quot;</span><span class="ot">$FX[bold]$FG[214]</span><span class="st">Hello, World&quot;</span>

<span class="co"># Print underlined and italicized text, with normal foreground, and blue</span>
<span class="co"># background.</span>
$<span class="kw">&gt;</span> <span class="kw">echo</span> <span class="st">&quot;</span><span class="ot">$FX[italic]$FX[underline]$BG[020]</span><span class="st">Hello, World&quot;</span>

<span class="co"># Bold, blinking purple text.</span>
$<span class="kw">&gt;</span> <span class="kw">echo</span> <span class="st">&quot;</span><span class="ot">$FX[bold]$FX[blink]$FG[093]</span><span class="st">Hello, World&quot;</span>

<span class="co"># Simple purple text on yellow background.</span>
$<span class="kw">&gt;</span> <span class="kw">echo</span> <span class="st">&quot;</span><span class="ot">$FG[093]$BG[226]</span><span class="st">Hello, World&quot;</span></code></pre></div>
<div class="footnotes">
<hr />
<ol>
<li id="fn1"><p>Although I might specifically mention my terminal (rxvt-unicode) or my shell (zsh), the information here is largely portable across all such terminals and shells.<a href="#fnref1">↩</a></p></li>
</ol>
</div>
  </div>
  <footer>
  </footer>
</article>
]]></summary>
</entry>

</feed>
