<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <title>Benjamin Tan's Learnings and Writings</title>
  <subtitle></subtitle>
  <id>http://benjamintan.io/blog</id>
  <link href="http://benjamintan.io/blog"/>
  <link href="http://benjamintan.io/feed.xml" rel="self"/>
  <updated>2019-09-23T15:55:00+00:00</updated>
  <author>
    <name>Benjamin Tan Wei Hao</name>
  </author>
  <entry>
    <title>Battling the Dreaded Black Screen in VNC</title>
    <link rel="alternate" href="http://benjamintan.io/blog/2019/09/23/battling-the-dreaded-black-screen-in-vnc/"/>
    <id>http://benjamintan.io/blog/2019/09/23/battling-the-dreaded-black-screen-in-vnc/</id>
    <published>2019-09-23T15:55:00+00:00</published>
    <updated>2019-09-23T15:58:42+00:00</updated>
    <author>
      <name>Benjamin Tan Wei Hao</name>
    </author>
    <content type="html">&lt;p&gt;VNC is an awesome tool. It&amp;rsquo;s super fast (of course, in a local network!), and gets the job done when a plain terminal doesn&amp;rsquo;t cut it. &lt;/p&gt;

&lt;p&gt;However, there&amp;rsquo;s nothing more irritating then seeing the dreaded black or grey VNC screen:&lt;/p&gt;

&lt;p&gt;&lt;img alt="Mfavo" src="https://i.stack.imgur.com/mfAVO.png" /&gt;&lt;/p&gt;

&lt;p&gt;It is such a productivity suck  and pain in the ass to deal with. I&amp;rsquo;ve had to deal with this for more than one occasion so I&amp;rsquo;m finally writing all the troubleshooting steps so that future me will thank current me.&lt;/p&gt;

&lt;p&gt;These steps were tried on Tiger VNC on Ubuntu, but I suspect they would apply to Tight VNC and the like.&lt;/p&gt;

&lt;p&gt;These are some steps that might help you to escape the VNC void.&lt;/p&gt;

&lt;h3&gt;1: Kill all existing VNC servers first&lt;/h3&gt;

&lt;p&gt;First do a &lt;code&gt;vncserver -list&lt;/code&gt;&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;% vncserver -kill :0 # or whatever number was listed before
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h3&gt;2: Do not use &lt;code&gt;sudo&lt;/code&gt; to launch &lt;code&gt;vncserver&lt;/code&gt;&lt;/h3&gt;

&lt;p&gt;Firstly, do not use &lt;code&gt;sudo&lt;/code&gt; to launch &lt;code&gt;vncserver&lt;/code&gt;. This creates files in the wrong permissions that would mess up non-&lt;code&gt;sudo&lt;/code&gt; sessions.
* Kill all other VNC servers first&lt;/p&gt;

&lt;h3&gt;3. Check file permissions of &lt;code&gt;~/.Xauthority&lt;/code&gt;&lt;/h3&gt;

&lt;p&gt;Check permissions are set to &lt;code&gt;$USER&lt;/code&gt; for the following files:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;~/.Xauthority&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;~/.ICEauthority&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;4. Is &lt;code&gt;$USER&lt;/code&gt; logged in anywhere else?&lt;/h3&gt;

&lt;p&gt;Check that &lt;code&gt;$USER&lt;/code&gt; is not logged in with the GUI. &lt;/p&gt;

&lt;h3&gt;5. Check &lt;code&gt;~/.vnc/xstartup&lt;/code&gt;&lt;/h3&gt;

&lt;p&gt;The following &lt;code&gt;~/.vnc/xstartup&lt;/code&gt; works. Find one which is known to work for your system. &lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#!/bin/sh&lt;/span&gt;

&lt;span class="nb"&gt;unset &lt;/span&gt;SESSION_MANAGER
&lt;span class="nb"&gt;unset &lt;/span&gt;DBUS_SESSION_BUS_ADDRESS
&lt;span class="nv"&gt;OS&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sb"&gt;`&lt;/span&gt;&lt;span class="nb"&gt;uname&lt;/span&gt; &lt;span class="nt"&gt;-s&lt;/span&gt;&lt;span class="sb"&gt;`&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt; &lt;span class="nv"&gt;$OS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'Linux'&lt;/span&gt; &lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;then
  case&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$WINDOWMANAGER&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt;
    &lt;span class="k"&gt;*&lt;/span&gt;gnome&lt;span class="k"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt; &lt;span class="nt"&gt;-e&lt;/span&gt; /etc/SuSE-release &lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;then
        &lt;/span&gt;&lt;span class="nv"&gt;PATH&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nv"&gt;$PATH&lt;/span&gt;:/opt/gnome/bin
        &lt;span class="nb"&gt;export &lt;/span&gt;PATH
      &lt;span class="k"&gt;fi&lt;/span&gt;
      &lt;span class="p"&gt;;;&lt;/span&gt;
  &lt;span class="k"&gt;esac&lt;/span&gt;
&lt;span class="k"&gt;fi
if&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt; &lt;span class="nt"&gt;-x&lt;/span&gt; /etc/X11/xinit/xinitrc &lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;then
  &lt;/span&gt;&lt;span class="nb"&gt;exec&lt;/span&gt; /etc/X11/xinit/xinitrc
&lt;span class="k"&gt;fi
if&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt; &lt;span class="nt"&gt;-f&lt;/span&gt; /etc/X11/xinit/xinitrc &lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;then
  &lt;/span&gt;&lt;span class="nb"&gt;exec &lt;/span&gt;sh /etc/X11/xinit/xinitrc
&lt;span class="k"&gt;fi&lt;/span&gt;
&lt;span class="o"&gt;[&lt;/span&gt; &lt;span class="nt"&gt;-r&lt;/span&gt; &lt;span class="nv"&gt;$HOME&lt;/span&gt;/.Xresources &lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; xrdb &lt;span class="nv"&gt;$HOME&lt;/span&gt;/.Xresources
xsetroot &lt;span class="nt"&gt;-solid&lt;/span&gt; grey
xterm &lt;span class="nt"&gt;-geometry&lt;/span&gt; 80x24+10+10 &lt;span class="nt"&gt;-ls&lt;/span&gt; &lt;span class="nt"&gt;-title&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$VNCDESKTOP&lt;/span&gt;&lt;span class="s2"&gt; Desktop"&lt;/span&gt; &amp;amp;
twm &amp;amp;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h3&gt;6. Check &lt;code&gt;~/.xsession-error&lt;/code&gt; for clues&lt;/h3&gt;

&lt;p&gt;This is my favorite and by far the most useful tip. &lt;/p&gt;

&lt;p&gt;Whenever there&amp;rsquo;s something wrong with the X Window system, it gets logged into this &lt;code&gt;~/.xsession-error&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Below are some of the example error messages that helped with troubleshooting:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;xfce4-session: Unable to access file /home/benjamintan/.ICEauthority: Permission denied&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;/usr/bin/x-session-manager: X server already running on display :2 xfce4-session: Another session manager is already running&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;Good Luck!&lt;/h2&gt;

&lt;p&gt;Hopefully, you&amp;rsquo;ll never have to encounter any VNC problems, but if you stumble upon then, I hope this is of some use to you.&lt;/p&gt;
</content>
  </entry>
  <entry>
    <title>Using PyCharm CE in Docker for Great Good!</title>
    <link rel="alternate" href="http://benjamintan.io/blog/2019/09/22/using-pycharm-ce-in-docker-for-great-good/"/>
    <id>http://benjamintan.io/blog/2019/09/22/using-pycharm-ce-in-docker-for-great-good/</id>
    <published>2019-09-22T05:40:00+00:00</published>
    <updated>2019-09-22T06:02:54+00:00</updated>
    <author>
      <name>Benjamin Tan Wei Hao</name>
    </author>
    <content type="html">&lt;p&gt;I&amp;rsquo;ve been using Docker quite a bit recently and beginning to see what the fuss is all about. Since, then, I&amp;rsquo;ve been Dockerizing everything up, down, left, right and center (My blog, my personal learning projects, my kids&amp;hellip;)&lt;/p&gt;

&lt;p&gt;In this post, I&amp;rsquo;ll demonstrate how to add PyCharm into an existing Python (or Java, C++, Elixir etc) project that relies on a Jet Brains IDE. This is essentially my workflow for non-trivial projects that require a full-fledged IDE. This is awesome because you can reap the benefits of a Dockerized project without sacrificing your favorite IDE. &lt;/p&gt;

&lt;h2&gt;The Problem&lt;/h2&gt;

&lt;p&gt;I have a non-trivial Python project that I&amp;rsquo;ve been coding in PyCharm (Community Edition) that has since been Dockerized. This is awesome, because there&amp;rsquo;s quite a few non-trivial dependencies in there, some of which are quite time consuming to build. Additionally, some of the resources needs to be pulled from S3. This was getting very painful after having to set up the project in multiple computers.&lt;/p&gt;

&lt;p&gt;With Docker, I just have to endure a couple of gigabytes worth of downloading, and I can start developing &amp;hellip; right?&lt;/p&gt;

&lt;p&gt;Well, the problem is PyCharm &lt;em&gt;Community Edition&lt;/em&gt;. &lt;/p&gt;

&lt;p&gt;You see, in order for PyCharm on the host to understand my Python project, it would have to use a &lt;em&gt;remote interpreter&lt;/em&gt;, which is totally legit since technically I didn&amp;rsquo;t have to install any dependencies &amp;ndash; everything that&amp;rsquo;s ever needed for the project is packaged nicely in the Docker container.&lt;/p&gt;

&lt;p&gt;You might think it&amp;rsquo;s possible to mount and map a volume, but again I&amp;rsquo;m assuming that this is a completely fresh machine, or at least, without the project being set up before. &lt;/p&gt;

&lt;p&gt;However, the remote interpreter is a paid-feature. Also, it feels a bit iffy to set it up as a remote interpreter, since that just seems like another potential point of failure. &lt;/p&gt;

&lt;h2&gt;The (Cheapo) Solution&lt;/h2&gt;

&lt;p&gt;Turns out, the solution (in hindsight) is pretty obvious. If PyCharm cannot read the Dockerized project, then simply have a copy of PyCharm in the project!&lt;/p&gt;

&lt;h2&gt;Prerequisites&lt;/h2&gt;

&lt;p&gt;There&amp;rsquo;s a big caveat to this approach. I&amp;rsquo;ve only tested this on an &lt;em&gt;Ubuntu&lt;/em&gt; machine. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Ubuntu (I&amp;rsquo;ve tried 16.04 and 18.04)&lt;/li&gt;
&lt;li&gt;(Optional) PyCharm installed on the host&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;The Scenario and Setup&lt;/h2&gt;

&lt;p&gt;So I&amp;rsquo;ve been learning me some &lt;a href="https://spark.apache.org/"&gt;Apache Spark&lt;/a&gt; and &lt;a href="https://beam.apache.org/"&gt;Apache Beam&lt;/a&gt;, and of course I&amp;rsquo;m not going to pollute my beautiful machine with all the dependencies and deal with fiddly set-up instructions. &lt;/p&gt;

&lt;p&gt;It is not important to know what Spark and Apache Beam are, just that they are for data processing.&lt;/p&gt;

&lt;p&gt;This is basically what I need:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Spark 2.4.x&lt;/li&gt;
&lt;li&gt;Apache Beam&lt;/li&gt;
&lt;li&gt;PyCharm&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;Dockerfile&lt;/h3&gt;

&lt;p&gt;The full &lt;a href="https://gist.github.com/benjamintanweihao/29c85fcd598cc7ccab4c9b3baba3f96f"&gt;Dockerfile&lt;/a&gt; can be found here. Here&amp;rsquo;s only the important bits:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# SYSTEM SETUP GOES HERE&lt;/span&gt;
...
&lt;span class="c"&gt;# JAVA and PYTHON DEPENDENCIES GO HERE&lt;/span&gt;
...
&lt;span class="c"&gt;# HADOOP DEPENDENCIES GO HERE&lt;/span&gt;
...
&lt;span class="c"&gt;# SPARK DEPENDENCIES GO HERE&lt;/span&gt;
...

&lt;span class="c"&gt;###### PYCHARM DEPENDENCIES #######&lt;/span&gt;

WORKDIR /opt/pycharm

ARG &lt;span class="nv"&gt;pycharm_source&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;https://download.jetbrains.com/python/pycharm-community-192.6603.24.tar.gz

RUN curl &lt;span class="nt"&gt;-fsSL&lt;/span&gt; &lt;span class="nv"&gt;$pycharm_source&lt;/span&gt; &lt;span class="nt"&gt;-o&lt;/span&gt; /opt/pycharm/installer.tgz &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nb"&gt;tar&lt;/span&gt; &lt;span class="nt"&gt;--strip-components&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;1 &lt;span class="nt"&gt;-xzf&lt;/span&gt; installer.tgz &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nb"&gt;rm &lt;/span&gt;installer.tgz &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; /usr/bin/python2 /opt/pycharm/helpers/pydev/setup_cython.py build_ext &lt;span class="nt"&gt;--inplace&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; /usr/bin/python3 /opt/pycharm/helpers/pydev/setup_cython.py build_ext &lt;span class="nt"&gt;--inplace&lt;/span&gt;

&lt;span class="c"&gt;###### PYCHARM DEPENDENCIES #######&lt;/span&gt;

CMD &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"bin/spark-class"&lt;/span&gt;, &lt;span class="s2"&gt;"org.apache.spark.deploy.master.Master"&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Basically, the setup for PyCharm is left almost at the end of the the DockerFile, though it doesn&amp;rsquo;t really matter. You can always change &lt;code&gt;pycharm_source&lt;/code&gt; to point to any Jet Brains IDE you like, though you&amp;rsquo;ll have to adjust the paths yourself. For example, if you&amp;rsquo;re using Intellij then you shouldn&amp;rsquo;t be using &lt;code&gt;/opt/pycharm&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;After the PyCharm dependencies are set up, the final command is meant to start the Spark server. In practice, this can be anything you want, or nothing at all.&lt;/p&gt;

&lt;h3&gt;Building the Docker Image&lt;/h3&gt;

&lt;p&gt;This is the usual &lt;code&gt;docker build&lt;/code&gt; (remember the dot at the end!):&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;% docker build &lt;span class="nt"&gt;-t&lt;/span&gt; benjamintanweihao/sparkdev &lt;span class="nb"&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h3&gt;Running the Docker Container&lt;/h3&gt;

&lt;p&gt;Here comes the interesting bit:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;% docker run &lt;span class="nt"&gt;-it&lt;/span&gt; &lt;span class="nt"&gt;--rm&lt;/span&gt; &lt;span class="nt"&gt;--name&lt;/span&gt; sparkdev &lt;span class="nt"&gt;-e&lt;/span&gt; &lt;span class="nv"&gt;DISPLAY&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;DISPLAY&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt; &lt;span class="nt"&gt;-v&lt;/span&gt; &lt;span class="sb"&gt;`&lt;/span&gt;&lt;span class="nb"&gt;pwd&lt;/span&gt;&lt;span class="sb"&gt;`&lt;/span&gt;:/home/developer/ &lt;span class="nt"&gt;-v&lt;/span&gt; /tmp/.X11-unix:/tmp/.X11-unix &lt;span class="nt"&gt;-v&lt;/span&gt; ~/.PyCharmCE2019.2:/home/developer/.PyCharmCE2019.2 &lt;span class="nt"&gt;-p&lt;/span&gt; 4040:4040 benjamintanweihao/sparkdev
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The most interesting flags that are related to PyCharm are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;-e DISPLAY=${DISPLAY}&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;-v /tmp/.X11-unix:/tmp/.X11-unix&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;-v ~/.PyCharmCE2019.2:/home/developer/.PyCharmCE2019.2&lt;/code&gt; (Optional, but nice to have to match your local PyCharm settings and plugins)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The first two basically exposes your &lt;code&gt;xhost&lt;/code&gt; so that the Docker container can render the display by reading and writing through the X11 socket. &lt;/p&gt;

&lt;p&gt;The final one maps the PyCharm settings directory. This is not strictly needed, but useful if you have plugins (like IdeaVim), themes, or any custom settings that you don&amp;rsquo;t want to set manually. Note that you should adjust the version numbers according to the version you have installed on your own machine (the left part) and the Docker container (right part). &lt;/p&gt;

&lt;p&gt;Finally, note that in this case, the user account created in Docker image is &lt;code&gt;developer&lt;/code&gt;, so be sure to adjust that too. &lt;/p&gt;

&lt;p&gt;Check out the full &lt;a href="https://gist.github.com/benjamintanweihao/29c85fcd598cc7ccab4c9b3baba3f96f"&gt;Dockerfile&lt;/a&gt; for more details.&lt;/p&gt;

&lt;h2&gt;Payoff!&lt;/h2&gt;

&lt;p&gt;Launching PyCharm from Docker is super simple:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;% docker exec -it sparkdev /opt/pycharm/bin/pycharm.sh
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Woot! Notice the cheerful &lt;em&gt;Welcome to PyCharm on &amp;lt;Docker Container ID&amp;gt;&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;img alt="Urybli3" src="https://i.imgur.com/URybli3.png" /&gt;&lt;/p&gt;

&lt;p&gt;I&amp;rsquo;ve been using this for my workflow for quite some time, and have been really pleased with it. I haven&amp;rsquo;t noticed any performance issues. The only thing is the confusion that arises when I have a copy of Docker PyCharm and a local PyCharm opened at the same time, though that&amp;rsquo;s purely an operator problem!&lt;/p&gt;

&lt;p&gt;&lt;code&gt;vim&lt;/code&gt; is forever.  🤘&lt;/p&gt;
</content>
  </entry>
  <entry>
    <title>Implementing Parallel Comprehension with Elixir Macro Goodness</title>
    <link rel="alternate" href="http://benjamintan.io/blog/2016/07/13/implementing-parallel-comprehension-with-elixir-macro-goodness/"/>
    <id>http://benjamintan.io/blog/2016/07/13/implementing-parallel-comprehension-with-elixir-macro-goodness/</id>
    <published>2016-07-13T13:50:00+00:00</published>
    <updated>2019-09-22T04:24:49+00:00</updated>
    <author>
      <name>Benjamin Tan Wei Hao</name>
    </author>
    <content type="html">&lt;p&gt;I have been ploughing through Chris McCords&amp;rsquo; &lt;a href="https://pragprog.com/book/cmelixir/metaprogramming-elixir"&gt;Metaprogramming Elixir&lt;/a&gt; book. In one of the chapters, he talks about how Macros are key in extending Elixir. An example he gave was that of a &lt;em&gt;parallel&lt;/em&gt; comprehension. Also, if you have no idea what Macros are, you should do either or both of this things:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Buy Metaprogramming Elixir.&lt;/li&gt;
&lt;li&gt;Read Saša Jurić&amp;rsquo;s outstanding series on Macros. &lt;a href="http://theerlangelist.com/article/macros_1"&gt;Here&amp;rsquo;s Part 1&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Throw caution to the wind and try out the example anyway.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let&amp;rsquo;s jump straight in! Here&amp;rsquo;s a normal comprehension:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for i &amp;lt;- 1..10, do: i * i
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;And here&amp;rsquo;s how a parallel one could look like:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;para(for i &amp;lt;- 1..10, do: i * i)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In this case, &lt;code&gt;para&lt;/code&gt; is a Macro that runs the body of the comprehension (&lt;code&gt;i * i&lt;/code&gt;) in &lt;em&gt;ten&lt;/em&gt; proceses, and returns the result.&lt;/p&gt;

&lt;p&gt;I don&amp;rsquo;t recall the book containing the actual implementation, but that&amp;rsquo;s great! This post will cover how I implemented this from scratch. Also, I have &lt;em&gt;one&lt;/em&gt; week of Macro experience at the time of this post. What could go wrong?&lt;/p&gt;

&lt;h2&gt;Look Ma, No Macros!&lt;/h2&gt;

&lt;p&gt;Before we go metaprogramming crazy, let&amp;rsquo;s consider how a &lt;em&gt;non&lt;/em&gt;-Macro solution would look like. Here&amp;rsquo;s a sketch:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="n"&gt;me&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;

&lt;span class="n"&gt;pids&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;spawn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;me&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="k"&gt;end&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="n"&gt;pids&lt;/span&gt; &lt;span class="o"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;Enum&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="n"&gt;pid&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; 
  &lt;span class="k"&gt;receive&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="o"&gt;^&lt;/span&gt;&lt;span class="n"&gt;pid&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;If this reminds you of parallel map, you&amp;rsquo;re absolutely right! The only difference here is that the comprehension allows you to have generators and filters so that you can tailor the generated values to your needs. For those who have not seen the code before, let&amp;rsquo;s unpack the code, because I think it&amp;rsquo;s pretty cool.&lt;/p&gt;

&lt;p&gt;First, we save a reference to the pid of the current process in &lt;code&gt;me&lt;/code&gt;.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="n"&gt;me&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Why? Because the value of &lt;code&gt;self&lt;/code&gt; changes, like when you are inside a &lt;code&gt;spawn&lt;/code&gt; for example. We are saving the pid of the current process so that we can send messages to it. In fact, this is the next thing we do: &lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="n"&gt;pids&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;spawn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;me&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="k"&gt;end&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;We execute the comprehension here, but instead of merely executing &lt;code&gt;i * i&lt;/code&gt;, we wrap the computation in a function and pass it to &lt;code&gt;spawn&lt;/code&gt;. We are effectively running the computation (again, &lt;code&gt;i * i&lt;/code&gt;) in a separate process. Of course, we are not simply running the computation in a separate process. If you look closely, we are sending a message of &lt;code&gt;{self, i * i}&lt;/code&gt; to &lt;code&gt;me&lt;/code&gt;. Therefore, once all the computation is done, &lt;code&gt;me&lt;/code&gt; will receive &lt;em&gt;ten&lt;/em&gt; messages in its mailbox. &lt;/p&gt;

&lt;p&gt;It&amp;rsquo;s important to realize that the messages can come &lt;em&gt;out of order&lt;/em&gt;. Imagine that the first process takes 10 seconds to complete while the rest takes less than a second. This means that the &lt;em&gt;first&lt;/em&gt; process would finish its task &lt;em&gt;last&lt;/em&gt;, which also means that the message would be seen last. We definitely want to preserve the order of the results coming back. That&amp;rsquo;s the purpose of the final piece of code:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="n"&gt;pids&lt;/span&gt; &lt;span class="o"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;Enum&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="n"&gt;pid&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; 
  &lt;span class="k"&gt;receive&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="o"&gt;^&lt;/span&gt;&lt;span class="n"&gt;pid&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;We know that the &lt;code&gt;pids&lt;/code&gt; contain an ordered list of pids created. When &lt;code&gt;map&lt;/code&gt;ped with a &lt;code&gt;receive&lt;/code&gt; block &lt;em&gt;and&lt;/em&gt; matching on the &lt;code&gt;pid&lt;/code&gt;, we can be sure that we are matching the &lt;code&gt;pid&lt;/code&gt; with the return value. Notice that the &lt;code&gt;^&lt;/code&gt; in &lt;code&gt;^pid&lt;/code&gt; is &lt;em&gt;super&lt;/em&gt; important here, otherwise the &lt;code&gt;pid&lt;/code&gt; in &lt;code&gt;fn pid -&amp;gt; ... end&lt;/code&gt; and &lt;code&gt;{pid, result}&lt;/code&gt; wouldn&amp;rsquo;t be matched.&lt;/p&gt;

&lt;p&gt;So now we have a plan of attack. We have a basic idea of how to implement parallel comprehension using processes. Time to metaprogram this thing.&lt;/p&gt;

&lt;h2&gt;Macros, Macros Everywhere&amp;hellip;&lt;/h2&gt;

&lt;p&gt;Let&amp;rsquo;s start with a new module and Macro definition:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="k"&gt;defmodule&lt;/span&gt; &lt;span class="no"&gt;MacroPlayground&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;

  &lt;span class="k"&gt;defmacro&lt;/span&gt; &lt;span class="n"&gt;para&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ast&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="n"&gt;me&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;

  &lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;I&amp;rsquo;ve named the input argument &lt;code&gt;ast&lt;/code&gt;, short of Abstract Syntax Tree. This is a good reminder that Macros take in ASTs and spit out ASTs. What we do &lt;em&gt;inside&lt;/em&gt; of the Macro is the fun stuff.&lt;/p&gt;

&lt;p&gt;Here&amp;rsquo;s the plan. Given a comprehension such as &lt;code&gt;for i &amp;lt;- 1..10, do: i * i&lt;/code&gt;, extract the &lt;code&gt;do&lt;/code&gt; part (&lt;code&gt;i * i&lt;/code&gt;) and turn it into this:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="n"&gt;spawn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;me&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="k"&gt;end&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Then, we will add the rest of the pieces such as the code for &lt;code&gt;map&lt;/code&gt;ping &lt;code&gt;pids&lt;/code&gt; against the &lt;code&gt;receive&lt;/code&gt; block.&lt;/p&gt;

&lt;p&gt;When working with ASTs, it&amp;rsquo;s important to understand how the AST is being represented. For example, you can wrap an expression around a &lt;code&gt;quote&lt;/code&gt; block:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="kn"&gt;quote&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;for&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This evaluates to:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;:for&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[],&lt;/span&gt;
 &lt;span class="p"&gt;[{&lt;/span&gt;&lt;span class="ss"&gt;:&amp;lt;&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[],&lt;/span&gt;
   &lt;span class="p"&gt;[{&lt;/span&gt;&lt;span class="ss"&gt;:i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[],&lt;/span&gt; &lt;span class="no"&gt;Elixir&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;{:&lt;/span&gt;&lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;context:&lt;/span&gt; &lt;span class="no"&gt;Elixir&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;Kernel&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;]}]},&lt;/span&gt;
  &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="k"&gt;do&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{:&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;context:&lt;/span&gt; &lt;span class="no"&gt;Elixir&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;Kernel&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
    &lt;span class="p"&gt;[{&lt;/span&gt;&lt;span class="ss"&gt;:i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[],&lt;/span&gt; &lt;span class="no"&gt;Elixir&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;:i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[],&lt;/span&gt; &lt;span class="no"&gt;Elixir&lt;/span&gt;&lt;span class="p"&gt;}]}]]}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;We need to replace the &lt;code&gt;do&lt;/code&gt; block with our &amp;ldquo;spawn&amp;rdquo; version. How do we do that? &lt;code&gt;Macro.prewalk/2&lt;/code&gt; to the rescue! There&amp;rsquo;s &lt;code&gt;Macro.postwalk/2&lt;/code&gt; too. So what&amp;rsquo;s the difference? Observe:&lt;/p&gt;

&lt;p&gt;First we&amp;rsquo;ll create the AST:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="n"&gt;iex&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;ast&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kn"&gt;quote&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
&lt;span class="o"&gt;...&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;for&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;...&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Prewalk:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="n"&gt;iex&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;Macro&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;prewalk&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ast&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;IO&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"-&amp;gt; &lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;inspect&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="k"&gt;end&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This returns:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;:for&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[],&lt;/span&gt; &lt;span class="p"&gt;[{&lt;/span&gt;&lt;span class="ss"&gt;:&amp;lt;&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[],&lt;/span&gt; &lt;span class="p"&gt;[{&lt;/span&gt;&lt;span class="ss"&gt;:i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[],&lt;/span&gt; &lt;span class="no"&gt;Elixir&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;{:&lt;/span&gt;&lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;context:&lt;/span&gt; &lt;span class="no"&gt;Elixir&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;Kernel&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;]}]},&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="k"&gt;do&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{:&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;context:&lt;/span&gt; &lt;span class="no"&gt;Elixir&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;Kernel&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="p"&gt;[{&lt;/span&gt;&lt;span class="ss"&gt;:i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[],&lt;/span&gt; &lt;span class="no"&gt;Elixir&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;:i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[],&lt;/span&gt; &lt;span class="no"&gt;Elixir&lt;/span&gt;&lt;span class="p"&gt;}]}]]}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;What about Postwalk?&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="n"&gt;iex&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;Macro&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;postwalk&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ast&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;IO&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"-&amp;gt; &lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;inspect&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="k"&gt;end&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="no"&gt;Macro&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;postwalk&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ast&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;IO&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"-&amp;gt; &lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;inspect&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="k"&gt;end&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This time, the result is different:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;:i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[],&lt;/span&gt; &lt;span class="no"&gt;Elixir&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;
&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{:&lt;/span&gt;&lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;context:&lt;/span&gt; &lt;span class="no"&gt;Elixir&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;Kernel&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:ok&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:ok&lt;/span&gt;&lt;span class="p"&gt;]}&lt;/span&gt;
&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;:&amp;lt;&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[],&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:ok&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:ok&lt;/span&gt;&lt;span class="p"&gt;]}&lt;/span&gt;
&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="ss"&gt;:do&lt;/span&gt;
&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;:i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[],&lt;/span&gt; &lt;span class="no"&gt;Elixir&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;:i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[],&lt;/span&gt; &lt;span class="no"&gt;Elixir&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{:&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;context:&lt;/span&gt; &lt;span class="no"&gt;Elixir&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;Kernel&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:ok&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:ok&lt;/span&gt;&lt;span class="p"&gt;]}&lt;/span&gt;
&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;:ok&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:ok&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:ok&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;:for&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[],&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:ok&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:ok&lt;/span&gt;&lt;span class="p"&gt;]}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;What &lt;code&gt;Macro.prewalk/2&lt;/code&gt;/&lt;code&gt;Macro.postwalk/2&lt;/code&gt; does is it takes an AST and traverse it. At each node, it applies the given funcion. That is exactly what we need. We will traverse the AST, pick out the node that matches a comprehension, then replace the &lt;code&gt;do&lt;/code&gt; block with the &amp;ldquo;&lt;code&gt;spawn&lt;/code&gt;&amp;rdquo; version.&lt;/p&gt;

&lt;p&gt;Here&amp;rsquo;s the full implementation. Scroll down further for the step by step explanations.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="k"&gt;defmodule&lt;/span&gt; &lt;span class="no"&gt;MacroPlayground&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;

  &lt;span class="k"&gt;defmacro&lt;/span&gt; &lt;span class="n"&gt;para&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ast&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="c1"&gt;# 0. Store the pid of the current process&lt;/span&gt;
    &lt;span class="n"&gt;me&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;

    &lt;span class="c1"&gt;# 6. Save the resulting pids&lt;/span&gt;
    &lt;span class="n"&gt;pids&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Macro&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;prewalk&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ast&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt;
      &lt;span class="c1"&gt;# 1. Pattern matching can be done with function arguments!&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;:for&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;meta&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;
        &lt;span class="c1"&gt;# 2. Extract the do block of the comprehension&lt;/span&gt;
        &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="k"&gt;do&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;do_block&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt; &lt;span class="o"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;List&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;last&lt;/span&gt;

      &lt;span class="c1"&gt;# 3. Wrap the do block around a spawn. Send the result to the&lt;/span&gt;
      &lt;span class="c1"&gt;#    current process.&lt;/span&gt;
      &lt;span class="n"&gt;spawn_do_block&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kn"&gt;quote&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
        &lt;span class="n"&gt;spawn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kn"&gt;unquote&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;me&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kn"&gt;unquote&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;do_block&lt;/span&gt;&lt;span class="p"&gt;)})&lt;/span&gt; &lt;span class="k"&gt;end&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="k"&gt;end&lt;/span&gt;

      &lt;span class="c1"&gt;# 4. Swap out the `do_block` (the last element of `args`)&lt;/span&gt;
      &lt;span class="c1"&gt;#    with the `spawn_do_block`&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;:for&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;meta&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;List&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;replace_at&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="k"&gt;do&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;spawn_do_block&lt;/span&gt;&lt;span class="p"&gt;])}&lt;/span&gt;

      &lt;span class="c1"&gt;# 5. We just output the same node for any other node&lt;/span&gt;
      &lt;span class="n"&gt;node&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;
        &lt;span class="n"&gt;node&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="c1"&gt;# 7. Collect the results&lt;/span&gt;
    &lt;span class="kn"&gt;quote&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
      &lt;span class="kn"&gt;unquote&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pids&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="o"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;Enum&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="n"&gt;pid&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;
        &lt;span class="k"&gt;receive&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
          &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="o"&gt;^&lt;/span&gt;&lt;span class="n"&gt;pid&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;
            &lt;span class="n"&gt;result&lt;/span&gt;
        &lt;span class="k"&gt;end&lt;/span&gt;
      &lt;span class="k"&gt;end&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The function we are passing to &lt;code&gt;Macro.prewalk/2&lt;/code&gt; matches on two cases. The first case is when we encounter a node that is a comprehension, and that happens when there&amp;rsquo;s a match for &lt;code&gt;{:for, meta, args}&lt;/code&gt; (Step 1). Otherwise, we simply emit the same node, leaving it unchanged (Step 5).&lt;/p&gt;

&lt;p&gt;Take a look at the AST again:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;:for&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[],&lt;/span&gt;
 &lt;span class="p"&gt;[{&lt;/span&gt;&lt;span class="ss"&gt;:&amp;lt;&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[],&lt;/span&gt;
   &lt;span class="p"&gt;[{&lt;/span&gt;&lt;span class="ss"&gt;:i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[],&lt;/span&gt; &lt;span class="no"&gt;Elixir&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;{:&lt;/span&gt;&lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;context:&lt;/span&gt; &lt;span class="no"&gt;Elixir&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;Kernel&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;]}]},&lt;/span&gt;
  &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="k"&gt;do&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{:&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;context:&lt;/span&gt; &lt;span class="no"&gt;Elixir&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;Kernel&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
    &lt;span class="p"&gt;[{&lt;/span&gt;&lt;span class="ss"&gt;:i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[],&lt;/span&gt; &lt;span class="no"&gt;Elixir&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;:i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[],&lt;/span&gt; &lt;span class="no"&gt;Elixir&lt;/span&gt;&lt;span class="p"&gt;}]}]]}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Here, &lt;code&gt;meta&lt;/code&gt; is &lt;code&gt;[]&lt;/code&gt; while &lt;code&gt;args&lt;/code&gt; is this chunk:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight elixir"&gt;&lt;code&gt; &lt;span class="p"&gt;[{&lt;/span&gt;&lt;span class="ss"&gt;:&amp;lt;&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[],&lt;/span&gt;
   &lt;span class="p"&gt;[{&lt;/span&gt;&lt;span class="ss"&gt;:i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[],&lt;/span&gt; &lt;span class="no"&gt;Elixir&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;{:&lt;/span&gt;&lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;context:&lt;/span&gt; &lt;span class="no"&gt;Elixir&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;Kernel&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;]}]},&lt;/span&gt;
  &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="k"&gt;do&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{:&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;context:&lt;/span&gt; &lt;span class="no"&gt;Elixir&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;Kernel&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
    &lt;span class="p"&gt;[{&lt;/span&gt;&lt;span class="ss"&gt;:i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[],&lt;/span&gt; &lt;span class="no"&gt;Elixir&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;:i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[],&lt;/span&gt; &lt;span class="no"&gt;Elixir&lt;/span&gt;&lt;span class="p"&gt;}]}]]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Our goal is to get to the &lt;code&gt;do&lt;/code&gt; bit. Thankfully, &lt;code&gt;args&lt;/code&gt; is just a &lt;code&gt;List&lt;/code&gt;. And the &lt;code&gt;do&lt;/code&gt; bit is the last element (Step 2):&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="k"&gt;do&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;do_block&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt; &lt;span class="o"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;List&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;last&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now that we have the AST of the &lt;code&gt;do_block&lt;/code&gt;, we can inject this into the &lt;code&gt;spawn&lt;/code&gt; (Step 3). We create a new AST with the &lt;code&gt;quote&lt;/code&gt; and in it we have the &lt;code&gt;spawn&lt;/code&gt; expression:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="n"&gt;spawn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kn"&gt;unquote&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;me&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kn"&gt;unquote&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;do_block&lt;/span&gt;&lt;span class="p"&gt;)})&lt;/span&gt; &lt;span class="k"&gt;end&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Remember, having an expression inside a &lt;code&gt;quote&lt;/code&gt; block turns it into an AST. We need to &lt;code&gt;unquote(me)&lt;/code&gt; and &lt;code&gt;unquote(do_block)&lt;/code&gt; in order to use its value inside the quoted expression. Finally, we savse the resulting AST into &lt;code&gt;spawn_do_block&lt;/code&gt;:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="n"&gt;spawn_do_block&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kn"&gt;quote&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;spawn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kn"&gt;unquote&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;me&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kn"&gt;unquote&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;do_block&lt;/span&gt;&lt;span class="p"&gt;)})&lt;/span&gt; &lt;span class="k"&gt;end&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now, we can perform the sleight-of-hand. We simply replace the last element of &lt;code&gt;args&lt;/code&gt; with &lt;code&gt;[do: spawn_do_block]&lt;/code&gt;, effectively transforming the resulting AST of the orignal comprehension (Step 4):&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;:for&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;meta&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;List&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;replace_at&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="k"&gt;do&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;spawn_do_block&lt;/span&gt;&lt;span class="p"&gt;])}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Recall that when the comprehension executes with &lt;code&gt;spawn&lt;/code&gt;s, the result is going to be a list of pids. As with the non-Macro version, the result of the comprehension is saved in the &lt;code&gt;pids&lt;/code&gt; (Step 6).&lt;/p&gt;

&lt;p&gt;Finally, we need to collect the results by matching pids.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="kn"&gt;quote&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="kn"&gt;unquote&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pids&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="o"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;Enum&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="n"&gt;pid&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;
    &lt;span class="k"&gt;receive&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="o"&gt;^&lt;/span&gt;&lt;span class="n"&gt;pid&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;
        &lt;span class="n"&gt;result&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Again, since we need to be dealing with ASTs, we need to wrap&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="n"&gt;pids&lt;/span&gt; &lt;span class="o"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;Enum&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="n"&gt;pid&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;
  &lt;span class="k"&gt;receive&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="o"&gt;^&lt;/span&gt;&lt;span class="n"&gt;pid&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;
      &lt;span class="n"&gt;result&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;in a &lt;code&gt;quote&lt;/code&gt; block. However, as with &lt;code&gt;me&lt;/code&gt; and &lt;code&gt;do_block&lt;/code&gt;, &lt;code&gt;pids&lt;/code&gt; has to be unquoted in order for it to be used in a quoted expression.&lt;/p&gt;

&lt;h2&gt;Taking it for a spin&lt;/h2&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="k"&gt;defmodule&lt;/span&gt; &lt;span class="no"&gt;Foo&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="no"&gt;MacroPlayground&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;foo&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;

    &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;para&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;for&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;10000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
   &lt;span class="k"&gt;do&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;

    &lt;span class="no"&gt;IO&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;inspect&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="no"&gt;Foo&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;foo&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This will give:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[{1, 1, 1}, {1, 1, 2}, {1, 1, 3}, {1, 2, 1}, {1, 2, 2}, {1, 3, 1}, {2, 1, 1},
 {2, 1, 2}, {2, 2, 1}, {3, 1, 1}]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;For good measure, you can &lt;code&gt;inspect&lt;/code&gt; the pid when the results are received:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="k"&gt;receive&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="o"&gt;^&lt;/span&gt;&lt;span class="n"&gt;pid&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;
    &lt;span class="no"&gt;IO&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;inspect&lt;/span&gt; &lt;span class="n"&gt;pid&lt;/span&gt;  &lt;span class="c1"&gt;# &amp;lt;-- add this line&lt;/span&gt;
    &lt;span class="n"&gt;result&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;You should be able to see this:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="c1"&gt;#PID&amp;lt;0.2170.0&amp;gt;&lt;/span&gt;
&lt;span class="c1"&gt;#PID&amp;lt;0.2171.0&amp;gt;&lt;/span&gt;
&lt;span class="c1"&gt;#PID&amp;lt;0.2172.0&amp;gt;&lt;/span&gt;
&lt;span class="c1"&gt;#PID&amp;lt;0.2173.0&amp;gt;&lt;/span&gt;
&lt;span class="c1"&gt;#PID&amp;lt;0.2174.0&amp;gt;&lt;/span&gt;
&lt;span class="c1"&gt;#PID&amp;lt;0.2175.0&amp;gt;&lt;/span&gt;
&lt;span class="c1"&gt;#PID&amp;lt;0.2176.0&amp;gt;&lt;/span&gt;
&lt;span class="c1"&gt;#PID&amp;lt;0.2177.0&amp;gt;&lt;/span&gt;
&lt;span class="c1"&gt;#PID&amp;lt;0.2178.0&amp;gt;&lt;/span&gt;
&lt;span class="c1"&gt;#PID&amp;lt;0.2179.0&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;[{&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
 &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;}]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Great Success! Hopefully this was educational. I took a while to figure this out, so don&amp;rsquo;t feel bad if you don&amp;rsquo;t get it the first time round. If you spot any mistakes or have suggestions for improvements, please do so in the comments. Thanks for reading!&lt;/p&gt;
</content>
  </entry>
  <entry>
    <title>A (almost) perfect Elm development environment with Spacemacs</title>
    <link rel="alternate" href="http://benjamintan.io/blog/2016/02/10/a-almost-perfect-elm-development-environment-with-spacemacs/"/>
    <id>http://benjamintan.io/blog/2016/02/10/a-almost-perfect-elm-development-environment-with-spacemacs/</id>
    <published>2016-02-10T16:31:00+00:00</published>
    <updated>2019-09-22T04:24:49+00:00</updated>
    <author>
      <name>Benjamin Tan Wei Hao</name>
    </author>
    <content type="html">&lt;p&gt;&lt;img alt="Dfnhbnn" src="http://i.imgur.com/dfNhBNN.png" /&gt;&lt;/p&gt;

&lt;p&gt;I have recently been messing around with Elm, especially since it seems to tie in beautifully with the Phoenix web framework. Also, anything to get away from JavaScript.&lt;/p&gt;

&lt;p&gt;Whenever I am learning a new language, I always want to get my development environment set up nicely. For Elm in particular, I had a few requirements that were important to me:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Proper syntax highlighting, formatting and indentation;&lt;/li&gt;
&lt;li&gt;Documentation + Type Signatures;&lt;/li&gt;
&lt;li&gt;Catch errors as I type and present them nicely, just like a Real IDE™;&lt;/li&gt;
&lt;li&gt;REPL, package installation, previewing in a browser and other bells and whistles.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Since I needed to excuse to play with Spacemacs, coupled with most other editors proved to be pretty inadequate, Spacemacs it was. (Again, I needed an excuse).&lt;/p&gt;

&lt;p&gt;It was surprising that getting the Elm-lang layer to function the way I wanted took a bit more work than expected. This post serves to document what I did, &lt;em&gt;in the hopes that someone will tell me that I got something hopelessly wrong and say &amp;ldquo;Here, this is the way to do it&amp;rdquo;&lt;/em&gt;.&lt;/p&gt;

&lt;h2&gt;The End Result&lt;/h2&gt;

&lt;iframe width="420" height="315" src="https://www.youtube.com/embed/raEwHv53XSA" frameborder="0" allowfullscreen&gt;&lt;/iframe&gt;

&lt;p&gt;If you don&amp;rsquo;t care about any explanations and want to see if it works for you, here&amp;rsquo;s the link to the &lt;a href="https://gist.github.com/benjamintanweihao/55a8b2e91dba66c50d8e"&gt;&lt;code&gt;.spacemacs&lt;/code&gt;&lt;/a&gt; file.&lt;/p&gt;

&lt;h2&gt;The Important Bits&lt;/h2&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(defun dotspacemacs/layers ()
  (setq-default
   dotspacemacs-configuration-layers
   '(
     (auto-completion :variables
                      auto-completion-enable-help-tooltip t
                      auto-completion-enable-snippets-in-popup t)
     ...
     (syntax-checking :variables
                      syntax-checking-enable-tooltips nil)
    )
    ...
    elm
    ...
  )
)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Here, we add extra configuration to &lt;code&gt;auto-completion&lt;/code&gt; so that documentation and snippets appear. Note: I am assuming you have read the elm-layer &lt;a href="https://github.com/syl20bnr/spacemacs/tree/master/layers/%2Blang/elm"&gt;README&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;In &lt;code&gt;syntax-checking&lt;/code&gt;, we disable tooltips because it will obscure the view of the documentation tooltip with the error message tooltip.&lt;/p&gt;

&lt;p&gt;Finally, I found that I had to manually add &lt;code&gt;company-elm&lt;/code&gt; to the list of &lt;code&gt;company-backends&lt;/code&gt;:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(defun dotspacemacs/user-config ()
  "Configuration function for user code.
This function is called at the very end of Spacemacs initialization after
layers configuration. You are free to put any user code."
  (with-eval-after-load 'company
    (add-to-list 'company-backends 'company-elm))
)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2&gt;A Sample Workflow&lt;/h2&gt;

&lt;p&gt;Here&amp;rsquo;s what you get once you have set up everything nicely:&lt;/p&gt;

&lt;iframe width="420" height="315" src="https://www.youtube.com/embed/xwboPogg35k" frameborder="0" allowfullscreen&gt;&lt;/iframe&gt;

&lt;p&gt;A couple of features you might notice:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Syntax highlighting and Indentation&lt;/li&gt;
&lt;li&gt;Auto-completion with documentation that includes type signatures&lt;/li&gt;
&lt;li&gt;Installing of Elm packages within Spacemacs&lt;/li&gt;
&lt;li&gt;Importing of Elm packages within Spacemacs&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;Why the &amp;ldquo;almost&amp;rdquo;?&lt;/h2&gt;

&lt;p&gt;I have found that the auto-completion isn&amp;rsquo;t as reliable I would like it to be. &lt;/p&gt;

&lt;p&gt;Sometimes &lt;code&gt;Array.&lt;/code&gt; shows me the full list of completions. However there are quite a few occasions where I have to help it out a little and do &lt;code&gt;Array.i&lt;/code&gt; for it to show me &lt;code&gt;Array.isEmpty&lt;/code&gt; and &lt;code&gt;Array.initialize&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;Part of the reason seems to stem from the limitations for the current implementation of &lt;code&gt;elm-oracle&lt;/code&gt;, which the README prompts you to install.&lt;/p&gt;

&lt;p&gt;From what I understand, that&amp;rsquo;s because it needs to fetch the documentation from the internet each time a completion needs to be performed. Also, the queries don&amp;rsquo;t seem to be cached.&lt;/p&gt;

&lt;p&gt;You can find out more &lt;a href="https://github.com/ElmCast/elm-oracle/issues/13"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The Elm layer already goes a pretty long way to streamline the development process. So while it isn&amp;rsquo;t exactly IDE-grade yet, it is pretty damn close. &lt;/p&gt;

&lt;p&gt;And since Elm is a fantastic language from what I see so far, I think it is worth the wait.  &lt;/p&gt;
</content>
  </entry>
  <entry>
    <title>The Story of The Little Elixir and OTP Guidebook - Part I</title>
    <link rel="alternate" href="http://benjamintan.io/blog/2015/04/22/the-story-of-the-little-elixir-and-otp-guidebook---part-i/"/>
    <id>http://benjamintan.io/blog/2015/04/22/the-story-of-the-little-elixir-and-otp-guidebook---part-i/</id>
    <published>2015-04-22T14:58:00+00:00</published>
    <updated>2019-09-22T04:24:49+00:00</updated>
    <author>
      <name>Benjamin Tan Wei Hao</name>
    </author>
    <content type="html">&lt;p&gt;Unlike most of my posts, this one is going to be a retrospective of the writing process of &lt;a href="http://www.manning.com/tanweihao?a_aid=exotpbook&amp;amp;a_bid=99f537ec"&gt;The Little Elixir and OTP Guidebook&lt;/a&gt;. I especially enjoy reading about behind-the-scenes stories like the one I am about to tell you. Hopefully, you are no different. &lt;/p&gt;

&lt;h2&gt;It started with stickers.&lt;/h2&gt;

&lt;p&gt;I am a sucker for stickers. One fine day, a certain &lt;a href="https://twitter.com/pragdave"&gt;Dave Thomas&lt;/a&gt; was asking on Twitter who wanted Elixir stickers. I am someone who would ever miss up on something &lt;em&gt;free&lt;/em&gt;, obviously wanted a &lt;em&gt;gajillion&lt;/em&gt; stickers. Didn&amp;rsquo;t matter where I was going to stick them.&lt;/p&gt;

&lt;p&gt;&lt;img alt="Stickers OMG!" src="http://i.imgur.com/0kIMYFX.png" /&gt;&lt;/p&gt;

&lt;p&gt;Ok fine, so I just asked for &lt;em&gt;three&lt;/em&gt;. &lt;em&gt;Two&lt;/em&gt; whole days passed (two days is too long to wait for stickers) and Dave finally replied:&lt;/p&gt;

&lt;p&gt;&lt;img alt="Dave Thomas is awesome" src="http://i.imgur.com/HV8Polg.png" /&gt;&lt;/p&gt;

&lt;p&gt;Holy crap! Dave Thomas – Mr. Pragmatic Programmer – read my blog post! I hope I wasn&amp;rsquo;t interpreting it the wrong way, but he seemed to suggest that I was somewhat capable of writing a book. Ha!&lt;/p&gt;

&lt;h2&gt;Blogging, blogging.&lt;/h2&gt;

&lt;p&gt;I kept blogging. Initially, I wrote about design patterns. After an abysmal three posts, I decided to abandon the effort and focused on Elixir. I couldn&amp;rsquo;t get enough of it. I could see the excitement and community gradually swell. Sure, it was hard work, but it as &lt;em&gt;addictive&lt;/em&gt; too, more so when people are enjoying what you write.&lt;/p&gt;

&lt;h2&gt;Cover design, Title, Domain Name&lt;/h2&gt;

&lt;p&gt;Dave might have never thought twice about the email he sent, but somehow that nagging message kept prodding at the back of my head.&lt;/p&gt;

&lt;p&gt;One fine day, I gave in. Since Elixir was fairly unknown then, I decided to create a sales page. By then I was hell bent on writing a book, any book, on Elixir. But I also wanted to make sure I wasn&amp;rsquo;t going to waste my precious youth (ha-ha). &lt;/p&gt;

&lt;p&gt;You know how some developers have trouble starting a project if they didn&amp;rsquo;t had a good name? I had the writer&amp;rsquo;s equivalent. First, I had to decide on a cover. I headed over to the &lt;a href="http://thenounproject.com/"&gt;Noun Project&lt;/a&gt; and picked me a nice logo. For the background, I went for &lt;a href="http://subtlepatterns.com/"&gt;Subtle Patterns&lt;/a&gt;. I designed everything in &amp;hellip; wait for it &amp;hellip; PowerPoint.&lt;/p&gt;

&lt;p&gt;Next, I had to figure out a good title. I knew it had to contain &amp;ldquo;Elixir&amp;rdquo; and &amp;ldquo;OTP&amp;rdquo;. I started to research titles. I came across &lt;a href="www.littleriakbook.com"&gt;A Little Riak Book&lt;/a&gt; and &lt;a href="http://riakhandbook.com/"&gt;Riak Handbook&lt;/a&gt;. A little bit here, a little bit there, and I settled on a title. Having &amp;ldquo;Little&amp;rdquo; was great too, because it explicitly told people &lt;em&gt;not&lt;/em&gt; to expect that much content.&lt;/p&gt;

&lt;p&gt;Domain name came next. Here were some of the ones I came up with: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;thelittleelixirandotpguidebook.com&lt;/code&gt; (Too long, duh)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;elixirotpguidebook.com&lt;/code&gt; (I kept noticing &amp;ldquo;rot&amp;rdquo;)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I decided that &lt;code&gt;www.exotpbook.com&lt;/code&gt; made for a good compromise. With all that out of the way, I had enough material to make me a landing page. &lt;a href="https://www.launchrock.com/"&gt;LaunchRock&lt;/a&gt; was perfect for this. &lt;/p&gt;

&lt;h2&gt;Did people even bother? Well, yes!&lt;/h2&gt;

&lt;p&gt;As soon as I launched the page and announced it to the world (maybe 10 people were listening), I nervously kept refreshing the page every 5 minutes or so, just to see if there were any new sign ups. Thankfully, by the end of the week, I had around 100+ signups. Looks like it&amp;rsquo;s a go! &lt;/p&gt;

&lt;h2&gt;Publisher and Publishing&lt;/h2&gt;

&lt;p&gt;I settled for &lt;a href="https://www.softcover.io/"&gt;Softcover&lt;/a&gt;, a service created by Michael Hartl, of Rails Tutorial fame. I especially enjoyed the toolchain. I could write the manuscript Markdown, and it spat out EPUB, MOBI and PDF. I really didn&amp;rsquo;t think about payments then. I was obsessing more about the tools used to write the book. Not only did Softcover provide the toolchain, it had a marketing page and other bells and whistles which made it a very pleasant platform to write on.&lt;/p&gt;

&lt;p&gt;&lt;img alt="Softcover" src="http://i.imgur.com/tWr1WpX.png" /&gt;&lt;/p&gt;

&lt;p&gt;I briefly considered LeanPub, but dismissed it because I thought its fonts were too damn ugly. I have a LeanPub book &lt;a href="https://leanpub.com/therubyclosuresbook"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;The Plan&lt;/h2&gt;

&lt;p&gt;I thought up the Table of Contents, was pleased with it, and proceeded to begin writing on the 23rd of February, 2014. Along the way, I set up MailChimp for sign ups and newsletters. &lt;/p&gt;

&lt;p&gt;And so it began:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;% git log | tail                                                
commit b2ef2ac7d730d11066e9d81615e5f070ebbf43e0
Author: Benjamin Tan Wei Hao &amp;lt;benjamintanweihao@gmail.com&amp;gt;
Date:   Sun Feb 23 10:54:17 2014 +0800

    Initial commit.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2&gt;I needed help &amp;hellip;&lt;/h2&gt;

&lt;p&gt;Along the way, I knew I needed reviewers. I reached out to the Elixir community, and also to other developers via the &lt;a href="http://us3.campaign-archive2.com/?u=e6c489d8ae654374bfa191d29&amp;amp;id=4ee1177637"&gt;mailing list&lt;/a&gt;. I wasn&amp;rsquo;t placing much hope in it, but it was worth a try.&lt;/p&gt;

&lt;h2&gt;&amp;hellip; and help came pouring!&lt;/h2&gt;

&lt;p&gt;The response was &lt;em&gt;incredible&lt;/em&gt;. Developers from all over reached out. Some even sent me their &lt;em&gt;résumés&lt;/em&gt;. I was absolutely floored and humbled. In the end, I had to &lt;a href="https://us3.admin.mailchimp.com/campaigns/show?id=725449"&gt;turn down many requests&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;As the months went by, I received numerous other emails from developers wanting to volunteer their time. Once again, I thank each and every one of you who wrote to me.&lt;/p&gt;

&lt;p&gt;I went on to set up a Trello board for the reviewers:&lt;/p&gt;

&lt;p&gt;&lt;img alt="Trello" src="http://i.imgur.com/8PR5XIO.png" /&gt;&lt;/p&gt;

&lt;p&gt;The reviewers and I started on the 1st of May 2014. The plan was to release June/July 2014, just in time for the inaugural ElixirConf. That was the plan until Manning contacted me.&lt;/p&gt;

&lt;h2&gt;Ohai, Manning!&lt;/h2&gt;

&lt;p&gt;On the 6th of June, 2014, I received an email from Michael Stephens, the Associate Publisher from Manning:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Mike Stephens with Manning Publications here. I’ve seen your page for The Little Elixir and OTP Guidebook and it looks like it’s going to be  a wonderful addition to the books announced on Elixir.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I was totally freaking out and dancing around by then.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;I’m wondering whether you’d be interested in the possibility of publishing with Manning rather than self publishing.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Oh hell yes. &lt;/p&gt;

&lt;p&gt;So far, the people at Manning have been super friendly. My only gripe is the constant back and forth, though I think most of it is unavoidable. This is in part that writing a book is tough, and also made tougher when we operate on different timezones. One fine day I will write about the entire Manning experience.&lt;/p&gt;

&lt;h2&gt;Ohai, The Little Elixir/OTP Guidebook!&lt;/h2&gt;

&lt;p&gt;The totally amount of effort needed to put together a book, even though it contains 3 chapters, is daunting to say the least. While I did all the writing, it was extremely heartening to have the community donate their time and energy into making the book better, from its earliest stages to its current iteration.&lt;/p&gt;

&lt;p&gt;I&amp;rsquo;m super stoked that I can finally get the book into the hands of readers. There&amp;rsquo;s just the small thing of writing the &lt;em&gt;rest&lt;/em&gt; of the chapters. &lt;/p&gt;

&lt;p&gt;Besides the fantastically smart people behind Elixir and all the wonderful Elixir libraries, I don&amp;rsquo;t think there&amp;rsquo;s really an Elixir &lt;em&gt;expert&lt;/em&gt;. At least I&amp;rsquo;m far from being one. I just like to about write things that other smart people make.&lt;/p&gt;

&lt;h2&gt;How&amp;rsquo;s the book doing?&lt;/h2&gt;

&lt;p&gt;So far it&amp;rsquo;s selling pretty well! I expect it to surpass 300 by the end of the week. Thanks for all the support – My wife and I thank you!&lt;/p&gt;

&lt;p&gt;&lt;img alt="Tanweihao cover150" src="http://www.manning.com/tanweihao/tanweihao_cover150.jpg" /&gt;&lt;/p&gt;

&lt;p&gt;If you haven&amp;rsquo;t bought the book yet, &lt;a href="http://www.manning.com/tanweihao?a_aid=exotpbook&amp;amp;a_bid=99f537ec"&gt;here you go&lt;/a&gt;. Ping me at &lt;code&gt;@bentanweihao&lt;/code&gt; on Twitter if would like a coupon code too!&lt;/p&gt;
</content>
  </entry>
  <entry>
    <title>Ruby Block Patterns and How to Implement File.open</title>
    <link rel="alternate" href="http://benjamintan.io/blog/2015/03/28/ruby-block-patterns-and-how-to-implement-file-open/"/>
    <id>http://benjamintan.io/blog/2015/03/28/ruby-block-patterns-and-how-to-implement-file-open/</id>
    <published>2015-03-28T03:42:00+00:00</published>
    <updated>2019-09-22T04:24:49+00:00</updated>
    <author>
      <name>Benjamin Tan Wei Hao</name>
    </author>
    <content type="html">&lt;p&gt;&lt;em&gt;This is an excerpt from my latest book, &lt;a href="https://leanpub.com/therubyclosuresbook"&gt;The Ruby Closures Book&lt;/a&gt;. If you like it, consider &lt;a href="https://leanpub.com/therubyclosuresbook"&gt;buying&lt;/a&gt; it! &amp;lt;3&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;Managing Resources with Blocks&lt;/h2&gt;

&lt;p&gt;Blocks are an excellent way to abstract pre and post processing. A wonderful example is how resource management is managed. Examples include opening and closing file handlers, socket connections, database connections etc.&lt;/p&gt;

&lt;p&gt;In other languages (C and Java, I&amp;rsquo;m looking at you), remembering to open and close the resource is a largely manual affair. This is both painful and ugly:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;File&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'Leo Tolstoy - War and Peace.txt'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'w'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s2"&gt;"Well, Prince, so Genoa and Lucca"&lt;/span&gt; 
&lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s2"&gt;" are now just family estates of the Buonapartes."&lt;/span&gt;
&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;close&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;If you omit &lt;code&gt;f.close&lt;/code&gt;, the file will remain open until the script terminates. In other words, you get a &lt;em&gt;resource leak&lt;/em&gt;. If you have a long running application like a daemon or web application, then this is bad news. That&amp;rsquo;s because the operating system can only handle a finite number of file handles. If you the long running daemon continuously opens files and doesn&amp;rsquo;t close them, soon enough the file handles run out, and you&amp;rsquo;ll get a 3 a.m. call. Happy times.&lt;/p&gt;

&lt;p&gt;If you think about it, what we really want is to &lt;em&gt;write&lt;/em&gt; to the file. Having to remember to &lt;em&gt;close&lt;/em&gt; the file handle is a hassle. &lt;/p&gt;

&lt;p&gt;Ruby has a very elegant way of doing this:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="no"&gt;File&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'Leo Tolstoy - War and Peace.txt'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'w'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
  &lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s2"&gt;"Well, Prince, so Genoa and Lucca"&lt;/span&gt; 
  &lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s2"&gt;" are now just family estates of the Buonapartes."&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;By passing in a block into &lt;code&gt;File.open&lt;/code&gt;, Ruby helps you, the over-burdened (and downright lazy) developer, to close the file handle when you are done writing the program. Notice that the file handle is nicely scoped &lt;em&gt;within&lt;/em&gt; the block.&lt;/p&gt;

&lt;iframe width="160" height="400" src="https://leanpub.com/therubyclosuresbook/embed" frameborder="0" allowtransparency="true" style="float: right; margin-top: 40px; margin-left: 20px;"&gt;&lt;/iframe&gt;

&lt;h2&gt;Implementing File.open&lt;/h2&gt;

&lt;p&gt;How is this done? Let&amp;rsquo;s learn to do this ourselves. First of all, the &lt;a href="http://ruby-doc.org/core-2.1.4/File.html#method-c-new"&gt;Ruby documentation&lt;/a&gt; provides and excellent overview of &lt;code&gt;File.open&lt;/code&gt;: &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;With no associated block, File.open is a synonym for ::new. If the optional code block is given, it will be passed the opened file as an argument and the File object will automatically be closed when the block terminates. The value of the block will be returned from File.open.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This tells us &lt;em&gt;everything&lt;/em&gt; we need to implement &lt;code&gt;File.open&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;a) If there&amp;rsquo;s no block given, &lt;code&gt;File.open&lt;/code&gt; is the same as &lt;code&gt;File.new&lt;/code&gt;:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;File&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nc"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;mode&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="n"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;mode&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;unless&lt;/span&gt; &lt;span class="nb"&gt;block_given?&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;b) If there&amp;rsquo;s a block, the block is then passed the opened file as an argument &amp;hellip;&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;File&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nc"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;mode&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;block&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;file&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;mode&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;file&lt;/span&gt; &lt;span class="k"&gt;unless&lt;/span&gt; &lt;span class="nb"&gt;block_given?&lt;/span&gt;
    &lt;span class="k"&gt;yield&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;c) &amp;hellip; and the file is automatically closed when the block terminates&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;File&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nc"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;mode&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;block&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;file&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;mode&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;file&lt;/span&gt; &lt;span class="k"&gt;unless&lt;/span&gt; &lt;span class="nb"&gt;block_given?&lt;/span&gt;
    &lt;span class="k"&gt;yield&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;close&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;There&amp;rsquo;s a subtlety to this. What happens if an exception is raised in the block? &lt;code&gt;file.close&lt;/code&gt; will not be called! Thankfully, that&amp;rsquo;s an easy fix with the &lt;code&gt;ensure&lt;/code&gt; keyword:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;File&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nc"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;mode&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;block&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;file&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;mode&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;file&lt;/span&gt; &lt;span class="k"&gt;unless&lt;/span&gt; &lt;span class="nb"&gt;block_given?&lt;/span&gt;
    &lt;span class="k"&gt;yield&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;ensure&lt;/span&gt;
    &lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;close&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now, &lt;code&gt;file.close&lt;/code&gt; is &lt;em&gt;always&lt;/em&gt; guaranteed to close properly.&lt;/p&gt;

&lt;p&gt;d) The value of the block will be returned from File.open.&lt;/p&gt;

&lt;p&gt;Since &lt;code&gt;yield(file)&lt;/code&gt; is the last line, the value of the block will be returned from File.open.&lt;/p&gt;

&lt;p&gt;&lt;img alt="Gh5m6wm" src="http://i.imgur.com/Gh5M6wm.jpg" /&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;In the &lt;a href="https://leanpub.com/therubyclosuresbook"&gt;book&lt;/a&gt;, I placed little exercises at the end of sections that let you test your understanding of the concepts that were just presented. Solutions are also included!&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;Exercises&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Implement File.open&lt;/em&gt;. Start off with the Ruby Documentation on &lt;code&gt;File.open&lt;/code&gt;. The key here is to understand where to put pre and post processing code, where to put &lt;code&gt;yield&lt;/code&gt;, and ensuring that resources are cleared up.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Real-world Ruby code Ruby Redis Library&lt;/em&gt;: Here is some code adapted from the &lt;a href="https://github.com/redis/redis-rb/blob/7c4a95413009cefa0c74d8d320f1ae90a1c953c2/test/support/redis_mock.rb"&gt;Ruby Redis library&lt;/a&gt;:&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;module&lt;/span&gt; &lt;span class="nn"&gt;Redis&lt;/span&gt;
  &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Server&lt;/span&gt;
    &lt;span class="c1"&gt;# ... more code ...&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;run&lt;/span&gt;
      &lt;span class="kp"&gt;loop&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
        &lt;span class="n"&gt;session&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="vi"&gt;@server&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;accept&lt;/span&gt;

        &lt;span class="k"&gt;begin&lt;/span&gt;
          &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="k"&gt;yield&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;session&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="ss"&gt;:exit&lt;/span&gt;
        &lt;span class="k"&gt;ensure&lt;/span&gt;
          &lt;span class="n"&gt;session&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;close&lt;/span&gt;
        &lt;span class="k"&gt;end&lt;/span&gt;
      &lt;span class="k"&gt;end&lt;/span&gt;
    &lt;span class="k"&gt;rescue&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;ex&lt;/span&gt;
      &lt;span class="vg"&gt;$stderr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"Error running server: &lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;ex&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;message&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
      &lt;span class="vg"&gt;$stderr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;puts&lt;/span&gt; &lt;span class="n"&gt;ex&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;backtrace&lt;/span&gt;
    &lt;span class="k"&gt;ensure&lt;/span&gt;
      &lt;span class="vi"&gt;@server&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;close&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;

    &lt;span class="c1"&gt;# ... more code ...&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Notice the similarities to the &lt;code&gt;File.open&lt;/code&gt; example. Does &lt;code&gt;run&lt;/code&gt; require a block to be passed in? How is the return result of the block used? How could this code be called? &lt;/p&gt;

&lt;h2&gt;Solutions&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Your final code should look something like this:&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;File&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nc"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;mode&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;block&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;file&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;mode&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;file&lt;/span&gt; &lt;span class="k"&gt;unless&lt;/span&gt; &lt;span class="nb"&gt;block_given?&lt;/span&gt;
    &lt;span class="k"&gt;yield&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;ensure&lt;/span&gt;
    &lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;close&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;ol&gt;
&lt;li&gt;Let&amp;rsquo;s go through the answers in order:&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;a) Does &lt;code&gt;run&lt;/code&gt; require a block to be passed in?&lt;/p&gt;

&lt;p&gt;Yes. There is no &lt;code&gt;block_given?&lt;/code&gt;, and &lt;code&gt;yield&lt;/code&gt; is called without any conditionals.&lt;/p&gt;

&lt;p&gt;b) How is the return result of the block used? &lt;/p&gt;

&lt;p&gt;The return result of the block is compared with &lt;code&gt;:exit&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;c) How could this code be called? &lt;/p&gt;

&lt;p&gt;The key here is that the block passed has exactly one argument:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="no"&gt;Server&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;run&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;session&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
  &lt;span class="c1"&gt;# do something with session&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2&gt;Thanks for Reading!&lt;/h2&gt;

&lt;p&gt;Hope you learned something! For more block patterns and other fun learnings, do check out the &lt;a href="https://leanpub.com/therubyclosuresbook"&gt;book&lt;/a&gt; that I put together.&lt;/p&gt;
</content>
  </entry>
  <entry>
    <title>How Does Symbol#to_proc Work?</title>
    <link rel="alternate" href="http://benjamintan.io/blog/2015/03/16/how-does-symbol-to_proc-work/"/>
    <id>http://benjamintan.io/blog/2015/03/16/how-does-symbol-to_proc-work/</id>
    <published>2015-03-16T10:15:00+00:00</published>
    <updated>2019-09-22T04:24:49+00:00</updated>
    <author>
      <name>Benjamin Tan Wei Hao</name>
    </author>
    <content type="html">&lt;p&gt;&lt;em&gt;This is an excerpt from my book, &lt;a href="https://leanpub.com/therubyclosuresbook"&gt;The Ruby Closures Book&lt;/a&gt;. If you like it, consider &lt;a href="https://leanpub.com/therubyclosuresbook"&gt;buying&lt;/a&gt; it! &amp;lt;3&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Symbol#to_proc&lt;/code&gt; is one of the finest examples of the flexibility and beauty of Ruby. This syntax sugar allows us to take a statement such as&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;words&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;length&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;and turn it into&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;words&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="ss"&gt;:length&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Let&amp;rsquo;s unravel this syntactical sleight of hand, by figuring out how this works.&lt;/p&gt;

&lt;h2&gt;What does the &amp;amp;:symbol do?&lt;/h2&gt;

&lt;p&gt;How does Ruby even know that it has to call a &lt;code&gt;to_proc&lt;/code&gt; method, and why is this only specific to the &lt;code&gt;Symbol&lt;/code&gt; class? &lt;/p&gt;

&lt;p&gt;When Ruby sees an &lt;code&gt;&amp;amp;&lt;/code&gt; and an object – &lt;em&gt;any&lt;/em&gt; object – &lt;strong&gt;it will try to turn it into a block&lt;/strong&gt;. This is simply form of type coercion. &lt;/p&gt;

&lt;p&gt;Take &lt;code&gt;to_s&lt;/code&gt; for example. We can do &lt;code&gt;2.to_s&lt;/code&gt;, which returns the string representation of the integer &amp;lsquo;2&amp;rsquo;. Similarly, &lt;code&gt;to_proc&lt;/code&gt; will attempt to turn an object – again, &lt;em&gt;any&lt;/em&gt; object – into a proc.&lt;/p&gt;

&lt;h2&gt;Reimplementing Symbol#to_proc&lt;/h2&gt;

&lt;p&gt;Let&amp;rsquo;s see what this means. Let&amp;rsquo;s create an object, and plop it into &lt;code&gt;each&lt;/code&gt;:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;obj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="c1"&gt;#&amp;lt;Object:0x007ff4218761b8&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;obj&lt;/span&gt; 
&lt;span class="no"&gt;TypeError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;wrong&lt;/span&gt; &lt;span class="n"&gt;argument&lt;/span&gt; &lt;span class="n"&gt;type&lt;/span&gt; &lt;span class="no"&gt;Object&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;expected&lt;/span&gt; &lt;span class="no"&gt;Proc&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;That&amp;rsquo;s awesome! Our error message is telling us exactly what we need to know: it&amp;rsquo;s saying that &lt;code&gt;obj&lt;/code&gt; is well, an &lt;em&gt;Object&lt;/em&gt; and not a &lt;code&gt;Proc&lt;/code&gt;. The fix is simple: the &lt;code&gt;Object&lt;/code&gt; class must have a &lt;code&gt;to_proc&lt;/code&gt; method that &lt;em&gt;returns a proc&lt;/em&gt;. Let&amp;rsquo;s do the simplest thing possible:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Object&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;to_proc&lt;/span&gt;
    &lt;span class="nb"&gt;proc&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="n"&gt;some_obj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;
&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;obj&lt;/span&gt; &lt;span class="c1"&gt;#=&amp;gt; [nil, nil, nil]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now when we run this again, we get no errors. Almost there! How can we then access each element, and say, print it? We need to let out &lt;code&gt;proc&lt;/code&gt; accept a parameter:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Object&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;to_proc&lt;/span&gt;
    &lt;span class="nb"&gt;proc&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="s2"&gt;"Here's &lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;!"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="n"&gt;some_obj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;
&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;obj&lt;/span&gt; &lt;span class="c1"&gt;#=&amp;gt; ["Here's 1!", "Here's 2!", "Here's 3!"]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This hints at a possible implementation of &lt;code&gt;Symbol#to_proc&lt;/code&gt;. Let&amp;rsquo;s start with what we know, and &lt;em&gt;redefine&lt;/em&gt; &lt;code&gt;to_proc&lt;/code&gt;:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Symbol&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;to_proc&lt;/span&gt;
    &lt;span class="nb"&gt;proc&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;obj&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;We know that in an expression such as&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;words&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="ss"&gt;:length&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;is equivalent to&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;words&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;length&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Here, the symbol &lt;em&gt;instance&lt;/em&gt; is &lt;code&gt;:length&lt;/code&gt;. This &lt;em&gt;value&lt;/em&gt; of the symbol corresponds to the &lt;em&gt;name&lt;/em&gt; of the method. We have previous found out how to access each yielded object – by making the proc return value in &lt;code&gt;to_proc&lt;/code&gt; take in an argument.&lt;/p&gt;

&lt;p&gt;We want to achieve this effect:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Symbol&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;to_proc&lt;/span&gt;
    &lt;span class="nb"&gt;proc&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;length&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;How can we use the name of the symbol to call a method on &lt;code&gt;obj&lt;/code&gt;? &lt;code&gt;send&lt;/code&gt; to the rescue! I hereby present you our own implementation of &lt;code&gt;Symbol#to_proc&lt;/code&gt;:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Symbol&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;to_proc&lt;/span&gt;
    &lt;span class="nb"&gt;proc&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Here, &lt;code&gt;self&lt;/code&gt; is the symbol object (&lt;code&gt;:length&lt;/code&gt; in our example), which is exactly what &lt;code&gt;#send&lt;/code&gt; expects.&lt;/p&gt;

&lt;iframe width="160" height="400" src="https://leanpub.com/therubyclosuresbook/embed" frameborder="0" allowtransparency="true" style="float: left; margin-top: 40px; margin-right: 20px;"&gt;&lt;/iframe&gt;

&lt;h2&gt;Improving on our Symbol#to_proc&lt;/h2&gt;

&lt;p&gt;Our initial implementation of &lt;code&gt;Symbol#to_proc&lt;/code&gt; is naïve. The reason is that we only consider the &lt;code&gt;obj&lt;/code&gt; in the body of the &lt;code&gt;proc&lt;/code&gt;, and totally ignore its arguments. &lt;/p&gt;

&lt;p&gt;Recall that unlike lambdas, procs are more relaxed when it comes to the number of arguments it is given. We can therefore easily expose this limitation. &lt;/p&gt;

&lt;p&gt;First, we return a lambda instead of a proc in &lt;code&gt;to_proc&lt;/code&gt;. Recall that a lambda is a proc, so everything should work as per normal: &lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Symbol&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;to_proc&lt;/span&gt;
    &lt;span class="nb"&gt;lambda&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="n"&gt;words&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sx"&gt;%w(underwear should be worn on the inside)&lt;/span&gt;
&lt;span class="n"&gt;words&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="ss"&gt;:length&lt;/span&gt; &lt;span class="c1"&gt;# =&amp;gt; [9, 6, 2, 4, 2, 3, 6]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Since we know lambdas are picky when it comes to the number of arguments, is there a method that requires &lt;em&gt;two&lt;/em&gt; arguments? Of course: &lt;code&gt;inject/reduce&lt;/code&gt;. The usual way of writing &lt;code&gt;reduce&lt;/code&gt; is:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nf"&gt;inject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;element&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;element&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="c1"&gt;# =&amp;gt; 6&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;As you can see, the block in inject takes in two arguments. Let&amp;rsquo;s see how our implementation does, by using the &lt;code&gt;&amp;amp;:symbol&lt;/code&gt; notation:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nf"&gt;inject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Here&amp;rsquo;s the error we get:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="no"&gt;ArgumentError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;wrong&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt; &lt;span class="n"&gt;of&lt;/span&gt; &lt;span class="n"&gt;arguments&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;from&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;irb&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="ss"&gt;:in&lt;/span&gt; &lt;span class="sb"&gt;`block in to_proc'
from (irb):14:in `&lt;/span&gt;&lt;span class="n"&gt;each&lt;/span&gt;&lt;span class="s1"&gt;'
from (irb):14:in `inject'&lt;/span&gt;
&lt;span class="o"&gt;...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;We can now clearly see that we are missing an argument. The lambda currently accepts only 1 argument, but what it received was 2 arguments. We need to allow the lambda to take in arguments:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Symbol&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;to_proc&lt;/span&gt;
    &lt;span class="nb"&gt;lambda&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nf"&gt;inject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# =&amp;gt; 6&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now it works as expected! We use the splat operator (that&amp;rsquo;s the &amp;ldquo;&lt;em&gt;&amp;rdquo; in `&lt;/em&gt;args`) to support a variable number of arguments. We have one problem though. This doesn&amp;rsquo;t work anymore:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;words&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sx"&gt;%w(underwear should be worn on the inside)&lt;/span&gt;
&lt;span class="n"&gt;words&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="ss"&gt;:length&lt;/span&gt; &lt;span class="c1"&gt;# =&amp;gt; [9, 6, 2, 4, 2, 3, 6]&lt;/span&gt;

&lt;span class="no"&gt;ArgumentError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;wrong&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt; &lt;span class="n"&gt;of&lt;/span&gt; &lt;span class="n"&gt;arguments&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;from&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;irb&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="ss"&gt;:in&lt;/span&gt; &lt;span class="sb"&gt;`block in to_proc'
from (irb):8:in `&lt;/span&gt;&lt;span class="n"&gt;map&lt;/span&gt;&lt;span class="err"&gt;'&lt;/span&gt;
&lt;span class="o"&gt;...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;There are two ways to fix this. First, we can give &lt;code&gt;args&lt;/code&gt; a default value:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Symbol&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;to_proc&lt;/span&gt;
    &lt;span class="nb"&gt;lambda&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kp"&gt;nil&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="n"&gt;words&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sx"&gt;%w(underwear should be worn on the inside)&lt;/span&gt;
&lt;span class="n"&gt;words&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="ss"&gt;:length&lt;/span&gt; &lt;span class="c1"&gt;# =&amp;gt; [9, 6, 2, 4, 2, 3, 6]&lt;/span&gt;

&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nf"&gt;inject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# =&amp;gt; 6&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Or, we can just make it a &lt;code&gt;Proc&lt;/code&gt; again:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Symbol&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;to_proc&lt;/span&gt;
    &lt;span class="nb"&gt;proc&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="n"&gt;words&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sx"&gt;%w(underwear should be worn on the inside)&lt;/span&gt;
&lt;span class="n"&gt;words&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="ss"&gt;:length&lt;/span&gt; &lt;span class="c1"&gt;# =&amp;gt; [9, 6, 2, 4, 2, 3, 6]&lt;/span&gt;

&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nf"&gt;inject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# =&amp;gt; 6&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This is one of the rare cases when being less picky about arity helps.&lt;/p&gt;

&lt;h2&gt;Thanks for Reading!&lt;/h2&gt;

&lt;p&gt;Hope you learned something – I sure did when putting together the &lt;a href="https://leanpub.com/therubyclosuresbook"&gt;book&lt;/a&gt;.&lt;/p&gt;
</content>
  </entry>
  <entry>
    <title>How to build Streams in Elixir easily with Stream.resource/3 Awesomeness</title>
    <link rel="alternate" href="http://benjamintan.io/blog/2015/02/05/how-to-build-streams-in-elixir-easily-with-stream-resource-awesomeness/"/>
    <id>http://benjamintan.io/blog/2015/02/05/how-to-build-streams-in-elixir-easily-with-stream-resource-awesomeness/</id>
    <published>2015-02-05T06:32:00+00:00</published>
    <updated>2019-09-22T04:24:49+00:00</updated>
    <author>
      <name>Benjamin Tan Wei Hao</name>
    </author>
    <content type="html">&lt;p&gt;Streams are fascinating because they allow us to model infinite data. In addition, a stream can be &lt;em&gt;composed&lt;/em&gt; together to form another stream. The possibilities are &lt;em&gt;endless&lt;/em&gt; – pun intended!&lt;/p&gt;

&lt;p&gt;I have covered streams in a &lt;a href="/blog/2013/08/14/elixir-for-the-lazy-impatient-and-busy-part-5-streams-streaming-dynamo/"&gt;previous&lt;/a&gt; post many moons ago. But let&amp;rsquo;s revisit the basics a bit, because why not?&lt;/p&gt;

&lt;p&gt;After a quick revision, we will learn how to create our own streams, along with a very fun project to make sure the concepts sink in. Let&amp;rsquo;s do this! &lt;/p&gt;

&lt;p&gt;If you are impatient, here&amp;rsquo;s a taste:&lt;/p&gt;

&lt;iframe width="420" height="315" src="https://www.youtube.com/embed/wnI0z514jmA" frameborder="0" allowfullscreen&gt;&lt;/iframe&gt;

&lt;h2&gt;Stream Basics&lt;/h2&gt;

&lt;p&gt;Streams are enumerables that are &lt;em&gt;composable&lt;/em&gt; and &lt;em&gt;lazy&lt;/em&gt;. Let&amp;rsquo;s talk being lazy first, or rather, what it means to be &lt;em&gt;not&lt;/em&gt; lazy.&lt;/p&gt;

&lt;h3&gt;Streams are Lazy&lt;/h3&gt;

&lt;p&gt;When you use &lt;code&gt;map/2&lt;/code&gt; of the &lt;code&gt;Enum&lt;/code&gt; module, the values are &lt;em&gt;eagerly&lt;/em&gt; computed. That&amp;rsquo;s just a fancy of saying that the results return immediately:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="n"&gt;iex&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;Enum&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;&amp;amp;1&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nv"&gt;&amp;amp;1&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;By contrast, what happens when we use &lt;code&gt;Stream&lt;/code&gt; module instead?&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;Stream&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;&amp;amp;1&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nv"&gt;&amp;amp;1&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="c1"&gt;#Stream&amp;lt;[enum: [1, 2, 3],&lt;/span&gt;
 &lt;span class="ss"&gt;funs:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="c1"&gt;#Function&amp;lt;45.29647706/1 in Stream.map/2&amp;gt;]]&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Well, we see a Stream being returned, but no &lt;code&gt;[1, 4, 9]&lt;/code&gt; in sight. This is what is meant by being lazy. &lt;em&gt;Unless absolutely necessary&lt;/em&gt;, the stream will not return a value. There are a couple of ways to compel the Stream to return a result. All of them involve a call to one of the functions in &lt;code&gt;Enum&lt;/code&gt;:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="n"&gt;iex&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;Stream&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;&amp;amp;1&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nv"&gt;&amp;amp;1&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="o"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;Enum&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;take&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;            &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Calling the eager functions in &lt;code&gt;Enum&lt;/code&gt; basically breaks the chain of laziness of the stream. This brings us to the next point.&lt;/p&gt;

&lt;h3&gt;Streams are Composable&lt;/h3&gt;

&lt;p&gt;We know that streams are lazy. Because streams don&amp;rsquo;t evaluate immediately, we can happily compose them together to combine computations. But can&amp;rsquo;t we do this with existing &lt;code&gt;Enum&lt;/code&gt; functions? Sure you can, if your input is manageable. Let&amp;rsquo;s imagine we have a crazy large input:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="n"&gt;wikipedia_titles&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;...&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="c1"&gt;# A list of a gazillion elements in it&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Then let&amp;rsquo;s say we wanted to capitalize the Wikipedia titles. Therefore, we need to &lt;code&gt;map/2&lt;/code&gt; the &lt;code&gt;String.upcase/1&lt;/code&gt; function on &lt;code&gt;wikipedia_titles&lt;/code&gt;:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="n"&gt;wikipedia_titles&lt;/span&gt; &lt;span class="o"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;Enum&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;String&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;upcase&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;&amp;amp;1&lt;/span&gt;&lt;span class="p"&gt;)))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Finally, let&amp;rsquo;s just assume that we are interested in the &lt;em&gt;first  10&lt;/em&gt; articles. &lt;code&gt;take/2&lt;/code&gt; is just the function for that:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="n"&gt;wikipedia_titles&lt;/span&gt; 
    &lt;span class="o"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;Enum&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;String&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;upcase&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;&amp;amp;1&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; 
    &lt;span class="o"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;Enum&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;String&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;reverse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;&amp;amp;1&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; 
    &lt;span class="o"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;Enum&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;take&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Trace the program and think about what would happen. You would realise that although we only wanted 10 capitalized Wikipedia titles, because of eager evaluation, we had to capitalized &lt;em&gt;all gazillion&lt;/em&gt; entries first, &lt;em&gt;then&lt;/em&gt; reverse &lt;em&gt;all gazillion entries&lt;/em&gt;, before picking out the first 10. That is just plain wasteful.&lt;/p&gt;

&lt;p&gt;Consider this version instead using &lt;code&gt;Stream&lt;/code&gt; instead:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="n"&gt;wikipedia_titles&lt;/span&gt; 
    &lt;span class="o"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;Stream&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;String&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;upcase&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;&amp;amp;1&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; 
    &lt;span class="o"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;Stream&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;String&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;reverse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;&amp;amp;1&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; 
    &lt;span class="o"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;Enum&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;take&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;When we replace &lt;code&gt;Enum.map/2&lt;/code&gt; with &lt;code&gt;Stream.map/2&lt;/code&gt;, both &lt;code&gt;String.upcase/2&lt;/code&gt; and &lt;code&gt;String.reverse/2&lt;/code&gt; are invoked only 10 times each. We don&amp;rsquo;t have to worry that mapping will take long because we are doing the &lt;em&gt;bare minimum&lt;/em&gt; when we use the &lt;code&gt;Stream&lt;/code&gt; module. This is why streams are awesome!&lt;/p&gt;

&lt;h2&gt;Building Your Own Streams&lt;/h2&gt;

&lt;p&gt;We are now going to learn how to create our own streams. We will go through two examples. The first example will be lame, but it will give you a feel of the general idea. The second is even more impractical, but will be very fun to play with.&lt;/p&gt;

&lt;h3&gt;An Infinite Number Stream&lt;/h3&gt;

&lt;p&gt;Let&amp;rsquo;s assume for a moment that we are blissfully unaware of functions that allow us to create infinite streams of numbers. Therefore, we are going to implement this functionality in a module called &lt;code&gt;Streamy&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;If you are following along go ahead and save the following in a file called &lt;code&gt;streamy.ex&lt;/code&gt;:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="k"&gt;defmodule&lt;/span&gt; &lt;span class="no"&gt;Streamy&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="no"&gt;Stream&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;resource&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt; &lt;span class="k"&gt;end&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="k"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; 
        &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; 
          &lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="ow"&gt;when&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; 
            &lt;span class="p"&gt;{[&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; 
          &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;
            &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;:halt&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;end&lt;/span&gt;
      &lt;span class="k"&gt;end&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="k"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="k"&gt;end&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Here&amp;rsquo;s what I am after:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="p"&gt;%&lt;/span&gt; &lt;span class="n"&gt;iex&lt;/span&gt; &lt;span class="n"&gt;streamy&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ex&lt;/span&gt;
&lt;span class="n"&gt;iex&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;Streamy&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;Stream&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;&amp;amp;1&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="o"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;Enum&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;take&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;
&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;22&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;24&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;26&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;28&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h3&gt;Building Streams with Stream.resource/3&lt;/h3&gt;

&lt;p&gt;The key to creating your own streams is &lt;code&gt;Stream.resource/3&lt;/code&gt;. The key to &lt;em&gt;understanding&lt;/em&gt; &lt;code&gt;Stream.resource/3&lt;/code&gt; is to pay attention to the three input arguments. In particular, &lt;em&gt;the inputs and outputs of the functions that are to be passed to &lt;code&gt;Stream.resource/3&lt;/code&gt;&lt;/em&gt;:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="no"&gt;Stream&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;resource&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;start_function&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
                &lt;span class="n"&gt;next_function&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
                &lt;span class="n"&gt;after_function&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Time to learn the rules.&lt;/p&gt;

&lt;h4&gt;Argument 1 – The Start Function&lt;/h4&gt;

&lt;p&gt;The &lt;code&gt;start_function&lt;/code&gt; that you pass into &lt;code&gt;Stream.resource/3&lt;/code&gt; sets up the resource and returns it. This function:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;doesn&amp;rsquo;t take any arguments&lt;/li&gt;
&lt;li&gt;returns the resource&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The resource could be a file handle, socket connection, or in our case, the initial number:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt; &lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;See? Super simple.&lt;/p&gt;

&lt;h4&gt;Argument 3 – The After Function&lt;/h4&gt;

&lt;p&gt;Let&amp;rsquo;s do the &lt;code&gt;after_function&lt;/code&gt; before doing &lt;code&gt;next_function&lt;/code&gt;. It is called &amp;ldquo;after&amp;rdquo; because this is the final function that is called once the stream is done spitting out values.&lt;/p&gt;

&lt;p&gt;This function:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;takes the resource as the input argument&lt;/li&gt;
&lt;li&gt;handles any cleanup that is required&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="n"&gt;resource&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;
  &lt;span class="c1"&gt;# handle clean up of the resource&lt;/span&gt;
  &lt;span class="c1"&gt;# e.g. File.close(resource), IO.close(resource)&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h4&gt;Argument 2 - The Next Function&lt;/h4&gt;

&lt;p&gt;The &lt;code&gt;next_function&lt;/code&gt; is where all the action is at. Conceptually, it is simple: This function:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;takes the resource as the input argument&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The return value must &lt;strong&gt;conform to a specific format&lt;/strong&gt;. In particular, it must:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;return a &lt;strong&gt;tuple&lt;/strong&gt; that contains a &lt;em&gt;list&lt;/em&gt; of items to be emitted (I was tempted to say &amp;ldquo;spitted&amp;rdquo;) and &lt;strong&gt;the next accumulator&lt;/strong&gt;. Therefore, the function has the following shape:&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="n"&gt;resource&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;read_from_resource&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{[&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;accumulator&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The point here is to take note of the &lt;em&gt;return value&lt;/em&gt;, because that is what &lt;code&gt;Stream.resource/3&lt;/code&gt; expects you to return. Any deviation and you get weird errors.&lt;/p&gt;

&lt;p&gt;Let&amp;rsquo;s say you encounter an error, or you have exhausted the resource (end of file, for example). In that case, you should return &lt;code&gt;{:halt, accumulator}&lt;/code&gt;&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="n"&gt;resource&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;read_from_resource&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{[&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;accumulator&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;_&lt;/span&gt;    &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;:halt&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;accumulator&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h4&gt;What is the Accumulator?&lt;/h4&gt;

&lt;p&gt;Accumulator means different things given different situations. Here&amp;rsquo;s the way I like to think about it:&lt;/p&gt;

&lt;p&gt;If you are setting up a file, socket, database connection etc, then &lt;code&gt;accumulator&lt;/code&gt; is that file handle, socket or database connection. There&amp;rsquo;s nothing to accumulate per se, but it&amp;rsquo;s more like &amp;ldquo;bringing forward&amp;rdquo; to the next invocation of the stream.&lt;/p&gt;

&lt;p&gt;Here&amp;rsquo;s an example take right out of the docs:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="no"&gt;Stream&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;resource&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;File&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;open!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"sample"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;end&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="n"&gt;file&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;
                  &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="no"&gt;IO&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;read&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:line&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
                    &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="ow"&gt;when&lt;/span&gt; &lt;span class="n"&gt;is_binary&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{[&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
                    &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;:halt&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
                  &lt;span class="k"&gt;end&lt;/span&gt;
                &lt;span class="k"&gt;end&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="n"&gt;file&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;File&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;close&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;end&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Notice that the accumulator is always &lt;code&gt;file&lt;/code&gt;. The only sane thing to do is pass along the file handle each time we request a value from the stream. Also notice that &lt;code&gt;data&lt;/code&gt; is wrapped in a &lt;em&gt;list&lt;/em&gt;. I got stung by this a few times before – you&amp;rsquo;ve been warned.&lt;/p&gt;

&lt;p&gt;On the other hand, if the value you generate now depends on the previous value generated, then &lt;code&gt;accumulator&lt;/code&gt; functions in the truest sense of the word. Here&amp;rsquo;s the infinite number generator again:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="k"&gt;defmodule&lt;/span&gt; &lt;span class="no"&gt;Streamy&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="no"&gt;Stream&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;resource&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt; &lt;span class="k"&gt;end&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="k"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; 
        &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; 
          &lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="ow"&gt;when&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; 
            &lt;span class="p"&gt;{[&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; 
          &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;
            &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;:halt&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;end&lt;/span&gt;
      &lt;span class="k"&gt;end&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="k"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="k"&gt;end&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Notice what happens when we purposely halt the stream by starting with a number close to 1000:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="n"&gt;iex&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;Streamy&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;996&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;Stream&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;&amp;amp;1&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="o"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;Enum&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;take&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;
&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1994&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1996&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1998&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2000&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In this case, only four values were generated. Hopefully by now, the infinite number generator makes sense, and you have a better idea how it works.&lt;/p&gt;

&lt;h3&gt;Click Stream&lt;/h3&gt;

&lt;p&gt;In the next example, we going to generate a stream using &lt;em&gt;mouse coordinates&lt;/em&gt; as the data source. The full source code can be found on &lt;a href="https://github.com/benjamintanweihao/click_stream"&gt;GitHub&lt;/a&gt;.&lt;/p&gt;

&lt;h4&gt;Setting Up the Project&lt;/h4&gt;

&lt;p&gt;The first order of things is to set up a project with &lt;code&gt;mix&lt;/code&gt;:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;% mix new click_stream
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Here&amp;rsquo;s the source in its entirety, to be placed in &lt;code&gt;lib/click_stream.ex&lt;/code&gt;:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="k"&gt;defmodule&lt;/span&gt; &lt;span class="no"&gt;ClickStream&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="kn"&gt;require&lt;/span&gt; &lt;span class="no"&gt;Record&lt;/span&gt;
  &lt;span class="no"&gt;Record&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;defrecordp&lt;/span&gt; &lt;span class="ss"&gt;:wx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;Record&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;extract&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:wx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;from_lib:&lt;/span&gt; &lt;span class="s2"&gt;"wx/include/wx.hrl"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

  &lt;span class="nv"&gt;@title&lt;/span&gt; &lt;span class="s1"&gt;'Click Stream'&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;create_stream_x&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="no"&gt;Stream&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;resource&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;create_frame&lt;/span&gt; &lt;span class="k"&gt;end&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                    &lt;span class="k"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;frame&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;loop_x&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;frame&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;end&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                    &lt;span class="k"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;frame&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;destroy_frame&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;frame&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;end&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt; 

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;create_stream_y&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="no"&gt;Stream&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;resource&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;create_frame&lt;/span&gt; &lt;span class="k"&gt;end&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                    &lt;span class="k"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;frame&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;loop_y&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;frame&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;end&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                    &lt;span class="k"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;frame&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;destroy_frame&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;frame&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;end&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt; 

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;create_frame&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="n"&gt;wx&lt;/span&gt;    &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="ss"&gt;:wx&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;new&lt;/span&gt;
    &lt;span class="n"&gt;frame&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="ss"&gt;:wxFrame&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;wx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;@title&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="ss"&gt;:wxWindow&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;frame&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:close_window&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="ss"&gt;:wxWindow&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;frame&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:motion&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="ss"&gt;:wxFrame&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;show&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;frame&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;frame&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;loop_x&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;frame&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="k"&gt;receive&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;:wx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;:wxMouse&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:motion&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_y&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;}}&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;
        &lt;span class="no"&gt;IO&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"x: &lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
        &lt;span class="p"&gt;{[&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;frame&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;:wx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;:wx_ref&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:wxFrame&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[]},&lt;/span&gt; &lt;span class="p"&gt;[],&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;:wxClose&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:close_window&lt;/span&gt;&lt;span class="p"&gt;}}&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;:halt&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;frame&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;:halt&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;frame&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;loop_y&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;frame&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="k"&gt;receive&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;:wx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;:wxMouse&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:motion&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;}}&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;
        &lt;span class="no"&gt;IO&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"y: &lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
        &lt;span class="p"&gt;{[&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;frame&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;:wx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;:wx_ref&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:wxFrame&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[]},&lt;/span&gt; &lt;span class="p"&gt;[],&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;:wxClose&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:close_window&lt;/span&gt;&lt;span class="p"&gt;}}&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;:halt&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;frame&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;:halt&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;frame&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;destroy_frame&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;frame&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="ss"&gt;:wxFrame&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;destroy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;frame&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Although the file seems relatively lengthy, a closer inspection would reveal that &lt;code&gt;create_stream_x/0&lt;/code&gt; and &lt;code&gt;create_stream_y/0&lt;/code&gt; are almost identical, except for the invocation of the &lt;code&gt;loop_x/0&lt;/code&gt; and &lt;code&gt;loop_y/0&lt;/code&gt; respectively.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;create_stream_x/0&lt;/code&gt; reports the mouse&amp;rsquo;s x-coordinates. Same story for &lt;code&gt;create_stream_y/0.&lt;/code&gt; &lt;/p&gt;

&lt;h3&gt;A Quick Demo&lt;/h3&gt;

&lt;p&gt;Here is what we want to do:&lt;/p&gt;

&lt;iframe width="420" height="315" src="https://www.youtube.com/embed/wnI0z514jmA" frameborder="0" allowfullscreen&gt;&lt;/iframe&gt;

&lt;h3&gt;A Revisit to Stream.resource/3&lt;/h3&gt;

&lt;p&gt;Let&amp;rsquo;s take a closer look at &lt;code&gt;create_stream_x/0&lt;/code&gt;:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;create_stream_x&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="no"&gt;Stream&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;resource&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;create_frame&lt;/span&gt; &lt;span class="k"&gt;end&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                  &lt;span class="k"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;frame&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;loop_x&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;frame&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;end&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                  &lt;span class="k"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;frame&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;destroy_frame&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;frame&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;end&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Recall that the first argument of &lt;code&gt;Stream.resource/3&lt;/code&gt; takes in a function that sets up and returns the resource. In this case, the resource is a wxWidget frame. No worries if you have no idea what wxWidget is. All you have to know is that wxWidget is a GUI library, and &lt;code&gt;frame&lt;/code&gt; is a reference to a GUI window. &lt;/p&gt;

&lt;p&gt;The last argument tears down the resource. In this case, we teardown the resource by destroying the frame.&lt;/p&gt;

&lt;p&gt;The fun part is in &lt;code&gt;loop_x/1&lt;/code&gt;:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;loop_x&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;frame&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="k"&gt;receive&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;:wx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;:wxMouse&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:motion&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_y&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;}}&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;
      &lt;span class="no"&gt;IO&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"x: &lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
      &lt;span class="p"&gt;{[&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;frame&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;:wx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;:wx_ref&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:wxFrame&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[]},&lt;/span&gt; &lt;span class="p"&gt;[],&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;:wxClose&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:close_window&lt;/span&gt;&lt;span class="p"&gt;}}&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;:halt&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;frame&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;:halt&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;frame&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The loop here executes the receive block each time it is called.  In wXwidget, messages are sent to &lt;code&gt;self&lt;/code&gt; whenever an event is triggered. &lt;/p&gt;

&lt;p&gt;What kinds of events are there? In the above code, we are only concerned with two kinds – the motion event triggered by mouse movement, and the closing of the window.&lt;/p&gt;

&lt;p&gt;I&amp;rsquo;d be the first to admit that the pattern to be matched looks extremely funky, but hey, it gets the job done. When the first pattern matches (&lt;code&gt;{:wx, _, _, _, {:wxMouse, :motion, x, _y, _, _, _, _, _, _, _, _, _, _}}&lt;/code&gt;), the tuple &lt;code&gt;{[x], frame}&lt;/code&gt; is returned. Once again, &lt;code&gt;x&lt;/code&gt; – the x-coordinate of the mouse position` – is wrapped in a list. It is followed by the accumulator, which in this case, is the frame – the resource.&lt;/p&gt;

&lt;p&gt;If the window is closed, or we get an unexpected message, we simply signal a close of the stream by returning &lt;code&gt;{:halt, frame}&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;That is really all to it! &lt;/p&gt;

&lt;h3&gt;Running Click Stream&lt;/h3&gt;

&lt;p&gt;Let&amp;rsquo;s run the project:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;% iex -S mix
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Next, we will create a stream that &lt;em&gt;lazily&lt;/em&gt; reports the x-coordinates of the mouse movement:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;iex&amp;gt; stream = ClickStream.create_stream_x
#Function&amp;lt;25.29647706/2 in Stream.resource/3&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Let&amp;rsquo;s only display the first 10 x coordinates that are less than 100. When you run the code, you will see a window appear. Run your mouse over it. &lt;/p&gt;

&lt;p&gt;Because we placed an &lt;code&gt;IO.puts/1&lt;/code&gt;, we can see all the &lt;code&gt;x&lt;/code&gt; values that are being reported. Once 10 eligible values are created, the window is closed, and the return result is displayed:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="n"&gt;iex&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;stream&lt;/span&gt; &lt;span class="o"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;Stream&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt; &lt;span class="k"&gt;end&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;Enum&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;take&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;
&lt;span class="ss"&gt;x:&lt;/span&gt; &lt;span class="mi"&gt;376&lt;/span&gt;
&lt;span class="ss"&gt;x:&lt;/span&gt; &lt;span class="mi"&gt;376&lt;/span&gt;
&lt;span class="ss"&gt;x:&lt;/span&gt; &lt;span class="mi"&gt;376&lt;/span&gt;
&lt;span class="ss"&gt;x:&lt;/span&gt; &lt;span class="mi"&gt;13&lt;/span&gt;
&lt;span class="ss"&gt;x:&lt;/span&gt; &lt;span class="mi"&gt;13&lt;/span&gt;
&lt;span class="ss"&gt;x:&lt;/span&gt; &lt;span class="mi"&gt;15&lt;/span&gt;
&lt;span class="ss"&gt;x:&lt;/span&gt; &lt;span class="mi"&gt;16&lt;/span&gt;
&lt;span class="ss"&gt;x:&lt;/span&gt; &lt;span class="mi"&gt;17&lt;/span&gt;
&lt;span class="ss"&gt;x:&lt;/span&gt; &lt;span class="mi"&gt;18&lt;/span&gt;
&lt;span class="ss"&gt;x:&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;
&lt;span class="ss"&gt;x:&lt;/span&gt; &lt;span class="mi"&gt;21&lt;/span&gt;
&lt;span class="ss"&gt;x:&lt;/span&gt; &lt;span class="mi"&gt;22&lt;/span&gt;
&lt;span class="ss"&gt;x:&lt;/span&gt; &lt;span class="mi"&gt;22&lt;/span&gt;
&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;13&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;13&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;15&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;17&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;18&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;21&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;22&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;22&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Recall that streams are &lt;em&gt;composable&lt;/em&gt;. This means that we can create another stream from existing ones. One way we can do that is through the &lt;code&gt;Stream.zip/2&lt;/code&gt; function, which takes in two streams and zips them up. &lt;/p&gt;

&lt;p&gt;First, we create a new stream from two other streams:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="n"&gt;iex&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;new_stream&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Stream&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;zip&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;ClickStream&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;create_stream_x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;ClickStream&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;create_stream_y&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;#Function&amp;lt;6.29647706/2 in Stream.zip/2&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Let&amp;rsquo;s take 10 values from this new stream. Just like in the previous case, a window pops up. Fiddle with the mouse a bit until 10 values are generated:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="n"&gt;iex&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;new_stream&lt;/span&gt; &lt;span class="o"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;Enum&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;take&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;
&lt;span class="ss"&gt;x:&lt;/span&gt; &lt;span class="mi"&gt;380&lt;/span&gt;
&lt;span class="ss"&gt;y:&lt;/span&gt; &lt;span class="mi"&gt;144&lt;/span&gt;
&lt;span class="ss"&gt;x:&lt;/span&gt; &lt;span class="mi"&gt;359&lt;/span&gt;
&lt;span class="ss"&gt;y:&lt;/span&gt; &lt;span class="mi"&gt;147&lt;/span&gt;
&lt;span class="ss"&gt;x:&lt;/span&gt; &lt;span class="mi"&gt;340&lt;/span&gt;
&lt;span class="ss"&gt;y:&lt;/span&gt; &lt;span class="mi"&gt;150&lt;/span&gt;
&lt;span class="ss"&gt;x:&lt;/span&gt; &lt;span class="mi"&gt;329&lt;/span&gt;
&lt;span class="ss"&gt;y:&lt;/span&gt; &lt;span class="mi"&gt;153&lt;/span&gt;
&lt;span class="ss"&gt;x:&lt;/span&gt; &lt;span class="mi"&gt;316&lt;/span&gt;
&lt;span class="ss"&gt;y:&lt;/span&gt; &lt;span class="mi"&gt;157&lt;/span&gt;
&lt;span class="ss"&gt;x:&lt;/span&gt; &lt;span class="mi"&gt;307&lt;/span&gt;
&lt;span class="ss"&gt;y:&lt;/span&gt; &lt;span class="mi"&gt;160&lt;/span&gt;
&lt;span class="ss"&gt;x:&lt;/span&gt; &lt;span class="mi"&gt;299&lt;/span&gt;
&lt;span class="ss"&gt;y:&lt;/span&gt; &lt;span class="mi"&gt;160&lt;/span&gt;
&lt;span class="ss"&gt;x:&lt;/span&gt; &lt;span class="mi"&gt;298&lt;/span&gt;
&lt;span class="ss"&gt;y:&lt;/span&gt; &lt;span class="mi"&gt;156&lt;/span&gt;
&lt;span class="ss"&gt;x:&lt;/span&gt; &lt;span class="mi"&gt;296&lt;/span&gt;
&lt;span class="ss"&gt;y:&lt;/span&gt; &lt;span class="mi"&gt;154&lt;/span&gt;
&lt;span class="ss"&gt;x:&lt;/span&gt; &lt;span class="mi"&gt;282&lt;/span&gt;
&lt;span class="ss"&gt;y:&lt;/span&gt; &lt;span class="mi"&gt;148&lt;/span&gt;
&lt;span class="p"&gt;[{&lt;/span&gt;&lt;span class="mi"&gt;380&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;144&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;359&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;147&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;340&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;150&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;329&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;153&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;316&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;157&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
 &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;307&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;160&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;299&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;160&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;298&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;156&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;296&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;154&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;282&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;148&lt;/span&gt;&lt;span class="p"&gt;}]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h4&gt;Other Examples of Streams (Or: When GitHub &amp;gt; Google)&lt;/h4&gt;

&lt;p&gt;When I was learning how &lt;code&gt;Stream.resource/3&lt;/code&gt; worked, &lt;em&gt;looking at other people&amp;rsquo;s code and finding patterns&lt;/em&gt; between them helped a lot.&lt;/p&gt;

&lt;p&gt;Turns out, GitHub has a very handy feature that lets you search through code and filter it by language: &lt;/p&gt;

&lt;p&gt;&lt;img alt="Xpra6sn" src="http://i.imgur.com/xpra6SN.png" /&gt;&lt;/p&gt;

&lt;p&gt;For example, &lt;a href="https://github.com/search?utf8=%E2%9C%93&amp;amp;q=Stream+resource+language%3AElixir&amp;amp;type=Code&amp;amp;ref=searchresults"&gt;here&amp;rsquo;s the search results&lt;/a&gt; for &lt;code&gt;Stream.resource/3&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;There are a few interesting examples. Here are two of my favourites:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://github.com/parroty/extwitter/blob/88096589f774e9b087a62766580ac3605a1dff4e/lib/extwitter/api/streaming.ex"&gt;ExTwitter&lt;/a&gt; is a wonderful example to see how Elixir Streams work with Twitter&amp;rsquo;s Streaming API.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://github.com/pragdave/dir_walker/blob/0aa035c2a4ce457694cd8a82c350e9084b5f9d04/lib/dir_walker.ex"&gt;DirWalker&lt;/a&gt; by Dave Thomas is a file-system directory tree walker that can handle large filesystems by traversing the directory tree lazily.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Thanks for reading! &amp;lt;3!&lt;/p&gt;
</content>
  </entry>
  <entry>
    <title>2014 Year in Review</title>
    <link rel="alternate" href="http://benjamintan.io/blog/2014/12/31/2014-year-in-review/"/>
    <id>http://benjamintan.io/blog/2014/12/31/2014-year-in-review/</id>
    <published>2014-12-31T04:12:00+00:00</published>
    <updated>2019-09-22T04:24:49+00:00</updated>
    <author>
      <name>Benjamin Tan Wei Hao</name>
    </author>
    <content type="html">&lt;p&gt;Tis&amp;rsquo; the season for year-end reviews! Since I didn&amp;rsquo;t do one last year, this is a good time as any to start. 2014 was the &lt;em&gt;best&lt;/em&gt; year I had so far, and probably also the busiest ever. Here&amp;rsquo;s the 10 best things that happened in 2014:&lt;/p&gt;

&lt;h2&gt;#1: Got Married!&lt;/h2&gt;

&lt;p&gt;&lt;img alt="Vzyxeye" src="http://i.imgur.com/vZyxEyE.jpg" /&gt;&lt;/p&gt;

&lt;p&gt;My biggest achievement of 2014 has been to marry the love of my life. That&amp;rsquo;s Hui Ling, my lovely wife. Now that the wedding preparations are behind us, I can finally get back to learning and doing awesome stuff.&lt;/p&gt;

&lt;h2&gt;#2. Elixir&lt;/h2&gt;

&lt;p&gt;&lt;img src="/images/coding/elixir.png" width="300px"&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="http://www.elixir-lang.org"&gt;Elixir&lt;/a&gt; finally hit 1.0, and started gaining more attention. We had our very first &lt;a href="http://elixirconf.com/"&gt;ElixirConf 2014&lt;/a&gt;. I was pretty sad I couldn&amp;rsquo;t make it for the first one, but I&amp;rsquo;m crossing my fingers and toes for my talk submission to get accepted &lt;a href="http://elixirconf.eu"&gt;ElixirConf EU 2015&lt;/a&gt;! Slowly but surely, articles, talks and even books are springing up all over. There&amp;rsquo;s no better time to be part of this awesome community.&lt;/p&gt;

&lt;h2&gt;#3. Writing for SitePoint&lt;/h2&gt;

&lt;p&gt;&lt;img src="/images/articles/sitepoint.png" width="300px"/&gt;&lt;/p&gt;

&lt;p&gt;I wrote &lt;a href="http://www.sitepoint.com/author/btan/"&gt;6 articles&lt;/a&gt; for SitePoint in 2014, 6 shy of my personal target. Writing for SitePoint gave me lots of practice in technical writing, and for that, I&amp;rsquo;m very grateful. I have since gained a renewed appreciation for the writers who can deliver well-written technical pieces.&lt;/p&gt;

&lt;p&gt;SitePoint is always on the &lt;a href="http://www.sitepoint.com/write-for-us/"&gt;lookout for more authors&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;#4. Bought So Many Books&lt;/h2&gt;

&lt;p&gt;With the money I earned from writing for SitePoint, I bought myself &lt;em&gt;lots of books&lt;/em&gt;! As my wife can attest to, I have a pretty dismal buy-to-read ratio. Nonetheless, I&amp;rsquo;m pretty proud of my little library:&lt;/p&gt;

&lt;p&gt;&lt;img alt="My Awesome Library" src="http://i.imgur.com/xz1h1xy.jpg" /&gt;&lt;/p&gt;

&lt;h2&gt;#5. Speaking at Conferences&lt;/h2&gt;

&lt;p&gt;I had the privilege of speaking at 2 Ruby conferences this year. &lt;a href="http://www.reddotrubyconf.com/"&gt;RedDot RubyConf 2014&lt;/a&gt; was the first even conference talk I&amp;rsquo;ve given. Even more awesome was  when my &lt;a href="http://rubyconf.org/"&gt;RubyConf 2014&lt;/a&gt; got accepted. The best part of it all was that I got to speak about Elixir at Ruby  conferences, and I think this goes a long way to demonstrate the Ruby communities&amp;rsquo; openness and willingness to explore new technologies.&lt;/p&gt;

&lt;p&gt;&lt;img alt="9x4jpuq" src="http://i.imgur.com/9X4jPuq.png" /&gt;&lt;/p&gt;

&lt;p&gt;One of the best things to come out of the conferences were making new friends, especially getting to meet all the people I&amp;rsquo;ve only interacted from Twitter. Special shout out to &lt;a href="https://twitter.com/jimfreeze"&gt;Jim Freeze&lt;/a&gt; who was a very welcoming figure at RubyConf. What made RubyConf 2014 extra special was the little Elixir gathering we had during lunches.&lt;/p&gt;

&lt;p&gt;You can catch the presentations I gave &lt;a href="http://confreaks.com/presenters/2702-benjamin-tan"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;#6. New Job at Neo Innovation&lt;/h2&gt;

&lt;p&gt;&lt;img src="http://www.neo.com/images/logo-5fdc26a5.svg" width="300px"/&gt;&lt;/p&gt;

&lt;p&gt;I joined &lt;a href="http://www.neo.com"&gt;Neo Innovation&lt;/a&gt; in June 2014, and I couldn&amp;rsquo;t be happier working with people that are &lt;em&gt;wayyy&lt;/em&gt; smarter than I am.&lt;/p&gt;

&lt;p&gt;We are &lt;a href="http://jobs.neo.com/"&gt;hiring&lt;/a&gt;!&lt;/p&gt;

&lt;h2&gt;#7. Kept Blogging&lt;/h2&gt;

&lt;p&gt;I am pretty proud that I continued to blog in 2014, and while my readership is pretty modest, it has grown significantly from when I first started. It also makes me very happy when people tweet/write to me and say that the posts have helped them in some way or another.&lt;/p&gt;

&lt;h2&gt;#8. The Little Elixir and OTP Guidebook&lt;/h2&gt;

&lt;p&gt;&lt;img alt="The Little Elixir and OTP Guidebook" src="/images/writing/exotpbook.png" /&gt;&lt;/p&gt;

&lt;p&gt;This was a wild ride. One year ago, I wouldn&amp;rsquo;t even dare dream to start on a book project, but I&amp;rsquo;m glad I did this year. I&amp;rsquo;ve made quite a few mistakes along the way, and at the same time, extremely humbled by the amount of support the community is willing to give. Getting the book deal with &lt;a href="http://www.manning.com"&gt;Manning Co.&lt;/a&gt; was extremely exciting too. The entire process taught me a lot about pitching and marketing a book. &lt;/p&gt;

&lt;p&gt;The book is far from done, mainly because now I have to get sign off from my editor, and also because of speaking, getting married, and honeymooning. On the bright side, I am starting to see vast improvements in the drafts.&lt;/p&gt;

&lt;h2&gt;#9. The Ruby Closures Book&lt;/h2&gt;

&lt;p&gt;As if one book wasn&amp;rsquo;t enough, I am starting on another book project. Writing is still in its early stages, but there&amp;rsquo;s a &lt;a href="http://www.rubyclosur.es"&gt;landing page&lt;/a&gt; for it.&lt;/p&gt;

&lt;p&gt;&lt;img alt="The Ruby Closures Book" src="http://rubyclosur.es/images/ruby-closures-cover.png" /&gt;&lt;/p&gt;

&lt;p&gt;Here&amp;rsquo;s why I&amp;rsquo;m writing this book:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;I have been a Ruby programmer for quite a number of years, but until recently, I have been blissfully ignorant of one of Ruby&amp;rsquo;s most powerful features. That is, until one day, I saw a co-worker casually write a method that took in a block. Holy ****! I realized at that moment, I had a major gap in my Ruby knowledge.&lt;/p&gt;

&lt;p&gt;Sure, I knew how to use blocks, and vaguely knew how to use lambdas in Rails scopes. But who was I kidding? I was never going to be a competent Ruby programmer if I didn&amp;rsquo;t fix this.&lt;/p&gt;

&lt;p&gt;Here&amp;rsquo;s the thing. I spoke to a few of my Ruby programmer friends, and guess what, the situation is the same. If you are like that then this book is for you.&lt;/p&gt;

&lt;p&gt;This book will help you gain a deep understanding of Ruby&amp;rsquo;s closures, and more importantly, how to harness its power.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Through writing this book, I&amp;rsquo;d be forcing myself to dive deep into the world of Ruby&amp;rsquo;s blocks, &lt;code&gt;proc&lt;/code&gt;s and &lt;code&gt;lambda&lt;/code&gt;s, and be one step closer to a more competent Ruby developer.&lt;/p&gt;

&lt;h2&gt;10. Blog and Landing Page Redesign&lt;/h2&gt;

&lt;p&gt;I wanted to redesign my blog for the longest time. Unfortunately, due to my lack of CSS skills, this was on the back burner for a while. Since November, I resolved to work on my CSS, and as a form of personal motivation, I became my own client and decided to not only redesign my blog, but also do up a &lt;a href="http://benjamintan.io"&gt;landing page&lt;/a&gt; for myself.&lt;/p&gt;

&lt;p&gt;&lt;img alt="My landing page" src="http://i.imgur.com/OYJjbFU.png" /&gt;&lt;/p&gt;

&lt;p&gt;This is the first website that I did for myself that I didn&amp;rsquo;t hate. I also got sign off from my wife, so that&amp;rsquo;s another plus.&lt;/p&gt;

&lt;h2&gt;Hello 2015!&lt;/h2&gt;

&lt;p&gt;I couldn&amp;rsquo;t be more excited for 2015. I&amp;rsquo;m looking forward to improving myself as a software developer, author and speaker. Thank you for supporting my work with your tweets, emails and comments. It meant a lot to me.&lt;/p&gt;

&lt;p&gt;Have an awesome 2015!&lt;/p&gt;
</content>
  </entry>
  <entry>
    <title>Running JUnit tests from the Command Line</title>
    <link rel="alternate" href="http://benjamintan.io/blog/2014/10/14/running-junit-test-from-the-command-line/"/>
    <id>http://benjamintan.io/blog/2014/10/14/running-junit-test-from-the-command-line/</id>
    <published>2014-10-14T11:15:00+00:00</published>
    <updated>2019-09-22T04:24:49+00:00</updated>
    <author>
      <name>Benjamin Tan Wei Hao</name>
    </author>
    <content type="html">&lt;p&gt;At work, we had trouble figuring out how to run JUnit tests outside of the IDE, in this case, IntellJ. After some fumbling around, here&amp;rsquo;s how we got it to work:&lt;/p&gt;

&lt;h2&gt;Step 1: Compile the Test class&lt;/h2&gt;

&lt;p&gt;Navigate to the &lt;code&gt;src/&lt;/code&gt; folder where all the Java classes are, then run &lt;code&gt;javac&lt;/code&gt; to compile the test file.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;% javac -cp .:"/Applications/IntelliJ IDEA 13 CE.app/Contents/lib/*" SetTest.java
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Notice a couple of things.&lt;/p&gt;

&lt;p&gt;Look at the flag passed in &lt;code&gt;-cp&lt;/code&gt; (classpath). We are setting the &lt;em&gt;current&lt;/em&gt; directory (thats the &amp;ldquo;.&amp;rdquo;) and also &lt;em&gt;all&lt;/em&gt; the libraries needed (that&amp;rsquo;s the &amp;ldquo;*&amp;rdquo;). &lt;/p&gt;

&lt;p&gt;Once that&amp;rsquo;s done, you will get a bunch of &lt;code&gt;*.class&lt;/code&gt; files generated. If your test makes use of the class you want to test, then that will be compiled too.&lt;/p&gt;

&lt;h2&gt;Step 2: Run the Test&lt;/h2&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;% java -cp .:"/Applications/IntelliJ IDEA 13 CE.app/Contents/lib/*" org.junit.runner.JUnitCore SetTest
JUnit version 4.11
.
Time: 0.007

OK (1 test)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This looks almost the same, except that we need to specify the JUnit test runner (&lt;code&gt;org.junit.runner.JUnitCore&lt;/code&gt;) and supply the class we are test (without the &lt;code&gt;.java&lt;/code&gt; extenstion).&lt;/p&gt;

&lt;p&gt;That&amp;rsquo;s it! Hopefully this helped you save some time! &lt;/p&gt;

&lt;p&gt;Thanks for reading!&lt;/p&gt;
</content>
  </entry>
  <entry>
    <title>Producer-Consumer problem in Elixir</title>
    <link rel="alternate" href="http://benjamintan.io/blog/2014/10/11/producer-consumer-problem-in-elixir/"/>
    <id>http://benjamintan.io/blog/2014/10/11/producer-consumer-problem-in-elixir/</id>
    <published>2014-10-11T14:53:00+00:00</published>
    <updated>2019-09-22T04:24:49+00:00</updated>
    <author>
      <name>Benjamin Tan Wei Hao</name>
    </author>
    <content type="html">&lt;p&gt;I was searching for an implementation for the &lt;a href="en.wikipedia.org/wiki/Producer–consumer_problem"&gt;Producer-Consumer&lt;/a&gt; problem in &lt;em&gt;Erlang&lt;/em&gt;, but apparently Google wasn&amp;rsquo;t on my side:&lt;/p&gt;

&lt;p&gt;&lt;img src="http://i.imgur.com/TWIpBxO.png" style="width:500px"/&gt;&lt;/p&gt;

&lt;p&gt;Having had no choice, and seeing enough examples in Java that made my eyes bleed, I was left to try out an implementation on my own. &lt;/p&gt;

&lt;h2&gt;Here is how it looks like&lt;/h2&gt;

&lt;p&gt;Here&amp;rsquo;s a running example of the implementation I did, with 2 producers and 5 workers:&lt;/p&gt;

&lt;p&gt;&lt;img src="https://camo.githubusercontent.com/6678fc22d00b7c630244ce233d243dcf9c7f5d9c/687474703a2f2f692e696d6775722e636f6d2f6932526f5965792e676966" style="width:500px"/&gt;&lt;/p&gt;

&lt;p&gt;If you are interested, the repo is located on &lt;a href="https://github.com/benjamintanweihao/procon/"&gt;GitHub&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;The Moving Parts&lt;/h2&gt;

&lt;p&gt;My implementation consists of 3 moving parts: the &lt;em&gt;producer&lt;/em&gt;, &lt;em&gt;consumer&lt;/em&gt;, and &lt;em&gt;manager&lt;/em&gt;. Let&amp;rsquo;s start with the &lt;code&gt;manager.ex&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;&lt;code&gt;manager.ex&lt;/code&gt;&lt;/h2&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="k"&gt;defmodule&lt;/span&gt; &lt;span class="no"&gt;Manager&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num_workers&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="n"&gt;spawn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; 
      &lt;span class="n"&gt;consumer_pids&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Enum&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="n"&gt;num_workers&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;Consumer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;end&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="k"&gt;loop&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;consumer_pids&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="k"&gt;loop&lt;/span&gt;&lt;span class="p"&gt;([])&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="k"&gt;receive&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;:request&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;producer_pid&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;
        &lt;span class="k"&gt;loop&lt;/span&gt;&lt;span class="p"&gt;([])&lt;/span&gt;

      &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;:done&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;consumer_pid&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;
        &lt;span class="k"&gt;loop&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="n"&gt;consumer_pid&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="k"&gt;loop&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="n"&gt;first&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;rest&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;consumer_pids&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="k"&gt;receive&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;:request&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;producer_pid&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;
        &lt;span class="n"&gt;send&lt;/span&gt; &lt;span class="n"&gt;producer_pid&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;:ok&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;first&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;loop&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;rest&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

      &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;:done&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;consumer_pid&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;
        &lt;span class="k"&gt;loop&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="n"&gt;consumer_pid&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;consumer_pids&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;We begin with &lt;code&gt;Manager.start/1&lt;/code&gt;:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="k"&gt;defmodule&lt;/span&gt; &lt;span class="no"&gt;Manager&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num_workers&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="n"&gt;spawn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; 
      &lt;span class="n"&gt;consumer_pids&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Enum&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="n"&gt;num_workers&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;Consumer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;end&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="k"&gt;loop&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;consumer_pids&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
  &lt;span class="c1"&gt;# ...&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The &lt;code&gt;start/1&lt;/code&gt; function takes in a non-negative integer, and in return, spawns up &lt;code&gt;num_workers&lt;/code&gt; of &lt;code&gt;Consumer&lt;/code&gt;s. The resulting &lt;code&gt;consumer_pids&lt;/code&gt;, a list of consumer pids, that are the passed in as the argument for &lt;code&gt;loop/1&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The main idea is consumers that are &lt;em&gt;available&lt;/em&gt; will be part of the loo&amp;rsquo;s arguments. This leads naturally to 2 cases, either we have consumers, or we don&amp;rsquo;t:&lt;/p&gt;

&lt;h4&gt;Case 1: No more consumers&lt;/h4&gt;

&lt;p&gt;This means that all our consumers are busy.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="k"&gt;defmodule&lt;/span&gt; &lt;span class="no"&gt;Manager&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="c1"&gt;# ...&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="k"&gt;loop&lt;/span&gt;&lt;span class="p"&gt;([])&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="k"&gt;receive&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;:request&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;producer_pid&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;
        &lt;span class="k"&gt;loop&lt;/span&gt;&lt;span class="p"&gt;([])&lt;/span&gt;

      &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;:done&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;consumer_pid&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;
        &lt;span class="k"&gt;loop&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="n"&gt;consumer_pid&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Whenever a producer had something to produce, it will send a &lt;code&gt;{:request, producer_pid}&lt;/code&gt; message to the manager. In this case, the manager is busy, so it will simply not respond, and call loop on itself.&lt;/p&gt;

&lt;p&gt;Whenever a consumer had completed processing something, it will send a message to the manager with a &lt;code&gt;{:done, consumer_pid}&lt;/code&gt;. This also means that the said consumer is available for a new job. Therefore the loop contains that &lt;code&gt;consumer_pid&lt;/code&gt;.&lt;/p&gt;

&lt;h4&gt;Case 2: Happy path&lt;/h4&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="k"&gt;defmodule&lt;/span&gt; &lt;span class="no"&gt;Manager&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="c1"&gt;# ...&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="k"&gt;loop&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="n"&gt;first&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;rest&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;consumer_pids&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="k"&gt;receive&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;:request&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;producer_pid&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;
        &lt;span class="n"&gt;send&lt;/span&gt; &lt;span class="n"&gt;producer_pid&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;:ok&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;first&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;loop&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;rest&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

      &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;:done&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;consumer_pid&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;
        &lt;span class="k"&gt;loop&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="n"&gt;consumer_pid&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;consumer_pids&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Compare this with the previous case. Here, we can reply to the producer with &lt;code&gt;{:of, first}&lt;/code&gt;, where &lt;code&gt;first&lt;/code&gt; is the first pid in the list of &lt;code&gt;consumer_pids&lt;/code&gt;. When &lt;code&gt;{:done, consumer_pid}&lt;/code&gt; is received from the consumer, then &lt;code&gt;consumer_pid&lt;/code&gt; is prepended to the list of &lt;code&gt;consumer_pids&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;&lt;code&gt;producer.ex&lt;/code&gt;&lt;/h2&gt;

&lt;p&gt;Now let&amp;rsquo;s turn our attention to the producer:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="k"&gt;defmodule&lt;/span&gt; &lt;span class="no"&gt;Producer&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="nv"&gt;@timeout&lt;/span&gt; &lt;span class="mi"&gt;2000&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;manager_pid&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="n"&gt;spawn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;manager_pid&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;end&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;manager_pid&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="no"&gt;Stream&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;repeatedly&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;
      &lt;span class="n"&gt;send&lt;/span&gt; &lt;span class="n"&gt;manager_pid&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;:request&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; 

      &lt;span class="k"&gt;receive&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;:ok&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;consumer_pid&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;
          &lt;span class="ss"&gt;:random&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;seed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:erlang&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;now&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
          &lt;span class="n"&gt;send&lt;/span&gt; &lt;span class="n"&gt;consumer_pid&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;:run&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:random&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;uniform&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)}&lt;/span&gt;

      &lt;span class="k"&gt;after&lt;/span&gt; &lt;span class="nv"&gt;@timeout&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;
        &lt;span class="n"&gt;send&lt;/span&gt; &lt;span class="n"&gt;manager_pid&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;:request&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; 
      &lt;span class="k"&gt;end&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;Enum&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;to_list&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;When we start the producer, we supply the &lt;code&gt;manager_pid&lt;/code&gt;, so that both parties can communicate:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="k"&gt;defmodule&lt;/span&gt; &lt;span class="no"&gt;Producer&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="nv"&gt;@timeout&lt;/span&gt; &lt;span class="mi"&gt;2000&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;manager_pid&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="n"&gt;spawn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;manager_pid&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;end&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
  &lt;span class="c1"&gt;# omitted ...&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In order to simulate a never ending stream, I used &lt;code&gt;Stream.repeatedly/1&lt;/code&gt; and &lt;code&gt;Enum.to_list&lt;/code&gt;:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="k"&gt;defmodule&lt;/span&gt; &lt;span class="no"&gt;Producer&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="c1"&gt;# ....&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;manager_pid&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="no"&gt;Stream&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;repeatedly&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;
      &lt;span class="c1"&gt;# ...&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;Enum&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;to_list&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now, let&amp;rsquo;s take a look at the messages being sent and received:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="k"&gt;defmodule&lt;/span&gt; &lt;span class="no"&gt;Producer&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="c1"&gt;# ....&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;manager_pid&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="no"&gt;Stream&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;repeatedly&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;
      &lt;span class="n"&gt;send&lt;/span&gt; &lt;span class="n"&gt;manager_pid&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;:request&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; 

      &lt;span class="k"&gt;receive&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;:ok&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;consumer_pid&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;
          &lt;span class="ss"&gt;:random&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;seed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:erlang&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;now&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
          &lt;span class="n"&gt;send&lt;/span&gt; &lt;span class="n"&gt;consumer_pid&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;:run&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:random&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;uniform&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)}&lt;/span&gt;

      &lt;span class="k"&gt;after&lt;/span&gt; &lt;span class="nv"&gt;@timeout&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;
        &lt;span class="n"&gt;send&lt;/span&gt; &lt;span class="n"&gt;manager_pid&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;:request&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; 
      &lt;span class="k"&gt;end&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;Enum&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;to_list&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Just &lt;em&gt;before&lt;/em&gt; the &lt;code&gt;receive&lt;/code&gt; block, the producer first sends a request to the manager for a &lt;code&gt;consumer_pid&lt;/code&gt;. If it gets one, then it will send a message to the consumer to run the job.&lt;/p&gt;

&lt;p&gt;Here&amp;rsquo;s the slightly more interesting bit: If it &lt;em&gt;doesn&amp;rsquo;t&lt;/em&gt; receive a reply, we &lt;em&gt;timeout&lt;/em&gt; after &lt;code&gt;@timeout&lt;/code&gt; (2 seconds, in this case), &lt;em&gt;and&lt;/em&gt; send the another request again. Why will this timeout in the first place? That&amp;rsquo;s because the manager will simply not answer a request if there are &lt;em&gt;no consumers available&lt;/em&gt;.&lt;/p&gt;

&lt;h2&gt;&lt;code&gt;consumer.ex&lt;/code&gt;&lt;/h2&gt;

&lt;p&gt;Implementing the consumer is simple:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="k"&gt;defmodule&lt;/span&gt; &lt;span class="no"&gt;Consumer&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;manager_pid&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="n"&gt;spawn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;loop&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;manager_pid&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;end&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="k"&gt;loop&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;manager_pid&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="k"&gt;receive&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;:run&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;work&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;
        &lt;span class="ss"&gt;:timer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sleep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;work&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;send&lt;/span&gt; &lt;span class="n"&gt;manager_pid&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;:done&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;loop&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;manager_pid&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Similar to the producer, we pass in the &lt;code&gt;manager_pid&lt;/code&gt; because the consumer needs to talk to the manager. The consumer only responds to one message, &lt;code&gt;{:run, work}&lt;/code&gt;. All it does is sleep a couple of seconds (to simulate doing work), then inform the manager that it should be added back into the list of available &lt;code&gt;consumer_pids&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;That&amp;rsquo;s it!&lt;/h2&gt;

&lt;p&gt;This little exercise took me quite a while to complete, because I hit into starvation issues along the way, although that was due to a bad pattern match on my part.&lt;/p&gt;

&lt;p&gt;This technique might come in useful when the producer produces more data than the consumers can handle, and we have to tell the producer to slow down somehow.&lt;/p&gt;

&lt;h2&gt;One more thing &amp;hellip;&lt;/h2&gt;

&lt;p&gt;&lt;img alt="Book cover" src="https://s3.amazonaws.com/erlang-in-anger/book-cover.png" /&gt;&lt;/p&gt;

&lt;p&gt;I&amp;rsquo;ve started reading &lt;a href="http://www.erlang-in-anger.com/"&gt;Erlang in Anger&lt;/a&gt;, &lt;strong&gt;a free ebook&lt;/strong&gt; by Fred Hebert, of Learn You Some Erlang for Great Good! fame. Lots of interesting stuff and advice on running Erlang in production. Highly recommend reading it.&lt;/p&gt;

&lt;p&gt;Thanks for reading!&lt;/p&gt;
</content>
  </entry>
</feed>
