Blog NameBlog subtitlehttp://blog.url.com/2016-03-04T01:00:00+01:00Blog AuthorHiring for Clojurehttp://blog.url.com/2016-03-hiring-clojure2016-03-04T01:00:00+01:002018-10-19T14:51:11+02:00Article Author<p>I recently got an email of a friend of a friend looking for Clojure opportunities. I asked around on Twitter to find out who's currently hiring, and got a lot of responses. If you were thinking of making the jump to doing Clojure professionally there's no better time than now.</p>
<p>Here's the <a href="https://twitter.com/plexus/status/704842358534242304">original tweet and replies</a>.</p>
<p>Here are some of the companies that responded or got mentioned:</p>
<ul>
<li>Zalando (<a href="https://twitter.com/ZalandoTech">@ZalandoTech</a>) <a href="https://en.wikipedia.org/wiki/Zalando">wikipedia</a></li>
</ul>
<p>Zalando SE is a European electronic commerce company seated in Berlin. The company maintains a cross-platform online store that sells shoes, clothing and other fashion items.</p>
<p><a href="https://tech.zalando.com/jobs/tech/">Job ads</a></p>
<p>Based in Berlin, jobs might be in various locations.</p>
<ul>
<li>The Climate Corporation (<a href="https://twitter.com/ClimateCorpEng">@ClimateCorpEng</a>) <a href="https://en.wikipedia.org/wiki/The_Climate_Corporation">wikipedia</a></li>
</ul>
<p>The Climate Corporation is a San Francisco-based company that examines weather data to provide insurance to farmers who can lock in profits even in the case of drought, heavy rains or other adverse weather conditions.</p>
<p><a href="http://jobs.jobvite.com/the-climate-corporation?d=Engineering">Job ads</a></p>
<p>Based in San Francisco, CA. Has openings in various locations in the US as well Sao Paulo, Brazil.</p>
<ul>
<li>Democracy.works (<a href="https://twitter.com/TurboVote">@TurboVote</a>)</li>
</ul>
<p>http://democracy.works/</p>
<p>Democracy Works is a nonpartisan, 501(c)(3) nonprofit organization dedicated to the idea that voting should fit the way we live. We're a team of software developers, public policy wonks, and civic organizers working together to build the tools a modern democracy needs.</p>
<p><a href="http://democracy.works/blog/2016/2/15/job-software-developer-professional-development-leader">Job ad</a></p>
<ul>
<li>USwitch</li>
</ul>
<p>uSwitch is an online and telephone comparison and switching service that helps you to compare prices on a range of products and services.</p>
<p>London based</p>
<p><a href="http://www.uswitch.com/vacancies/">Job ads</a></p>
<ul>
<li>LambdaWerk (<a href="https://twitter.com/lambdawerk">@lambdawerk</a>]</li>
</ul>
<p>LambdaWerk develops software products for healthcare providers. Our specialty is data exchange between medical care organizations, healthcare providers and clearing houses.</p>
<p><a href="https://www.lambdawerk.com/jobs">Job ads</a></p>
<ul>
<li>Skillable (<a href="https://twitter.com/skillable">@Skillable</a>)</li>
</ul>
<p>Discover insights on your favourite companies, match against their culture, find out which are the best employers for you.</p>
<p><a href="https://clojurework.com/jobs/17592186045582">Job ad</a></p>
<p>Based in Stockholm, Sweden.</p>
<ul>
<li>Room Key</li>
</ul>
<p>Room Key is a hotel search engine founded by six of the world's leading hotel companies.</p>
<p><a href="http://www.roomkey.com/careers.html">Job ads</a></p>
<ul>
<li>Atomic Object</li>
</ul>
<p>Custom software development for web, mobile, desktop, and devices. Atomic is an employee-owned software development company in Michigan.</p>
<p><a href="https://atomicobject.com/careers">Job ads</a></p>
<ul>
<li>Style.com</li>
</ul>
<p><a href="https://www.linkedin.com/jobs2/view/104820684?trk=biz-overview-job-post">Job ad</a></p>
<p>Based in London, UK.</p>
<ul>
<li>Stylefruits</li>
</ul>
<p>Stylefruits is a german social shopping community, where you can easily create own outfits and trends.</p>
<p>For more info you can contact <a href="https://twitter.com/janstepien">Jan Stępień</a></p>
<p>Based in Munich.</p>
<p><strong>Update 2015-03-15</strong></p>
<p><a href="http://signal.uk.com/">Signal</a> In London, UK is doing a "big hiring sprint".</p>
<p>Signal is a platform built for analysing text and discovering market intelligence.</p>
<p><a href="https://signal.workable.com/jobs/201996">Job ad</a></p>
Exploring Ruby Macroshttp://blog.url.com/2015-10-Exploring-Ruby-Macros2015-10-19T02:00:00+02:002018-10-19T14:51:11+02:00Article Author<p>Macros are one of those language features that divide programmer communities. Some swear by their stupendous power, others reject them with a ferocity as if they are the breed of Satan.</p>
<p>When asked Matz has always made his position clear: there is no place in Ruby for macros. I think he's right, the Ruby language has very little to gain by adding them. It would only clutter up a language with an already massive syntax, and encourage people to make a huge cryptic mess.</p>
<p>But that doesn't mean we can't try it out anyway :D, so during the Euruko conference I coded up a proof of concept of the <a href="https://github.com/plexus/macros">"macros" gem.</a></p>
<p>Macros are syntax tree transformations that are applied before code is evaluated. Ruby doesn't expose its parse tree directly, but we can parse the code ourselves with the <a href="https://github.com/whitequark/parser">Parser</a> gem, apply macros, then turn the result into Ruby code again with <a href="https://github.com/mbj/unparser">Unparser</a>, which is exactly what the <a href="https://github.com/plexus/macros">Macros</a> gem does.</p>
<p>Macros are most common in homoiconic languages (i.e. LISPs), where the source code directly corresponds with the parsed syntax tree. If you know how to program in such a language, you also know how to write or manipulate syntax trees. This is not the case in Ruby. Knowing how code constructs map to AST nodes is a skill in itself.</p>
<p>For example:</p>
<pre class="highlight ruby"><code><span class="no">Macros</span><span class="p">.</span><span class="nf">parse</span><span class="p">(</span><span class="s1">'collection.inject({}) {|acc, el| acc.merge(el.name => el) }'</span><span class="p">)</span>
<span class="c1"># =></span>
<span class="n">s</span><span class="p">(</span><span class="ss">:block</span><span class="p">,</span>
<span class="n">s</span><span class="p">(</span><span class="ss">:send</span><span class="p">,</span>
<span class="n">s</span><span class="p">(</span><span class="ss">:send</span><span class="p">,</span> <span class="kp">nil</span><span class="p">,</span> <span class="ss">:collection</span><span class="p">),</span> <span class="ss">:inject</span><span class="p">,</span>
<span class="n">s</span><span class="p">(</span><span class="ss">:hash</span><span class="p">)),</span>
<span class="n">s</span><span class="p">(</span><span class="ss">:args</span><span class="p">,</span>
<span class="n">s</span><span class="p">(</span><span class="ss">:arg</span><span class="p">,</span> <span class="ss">:acc</span><span class="p">),</span>
<span class="n">s</span><span class="p">(</span><span class="ss">:arg</span><span class="p">,</span> <span class="ss">:el</span><span class="p">)),</span>
<span class="n">s</span><span class="p">(</span><span class="ss">:send</span><span class="p">,</span>
<span class="n">s</span><span class="p">(</span><span class="ss">:lvar</span><span class="p">,</span> <span class="ss">:acc</span><span class="p">),</span> <span class="ss">:merge</span><span class="p">,</span>
<span class="n">s</span><span class="p">(</span><span class="ss">:hash</span><span class="p">,</span>
<span class="n">s</span><span class="p">(</span><span class="ss">:pair</span><span class="p">,</span>
<span class="n">s</span><span class="p">(</span><span class="ss">:send</span><span class="p">,</span>
<span class="n">s</span><span class="p">(</span><span class="ss">:lvar</span><span class="p">,</span> <span class="ss">:el</span><span class="p">),</span> <span class="ss">:name</span><span class="p">),</span>
<span class="n">s</span><span class="p">(</span><span class="ss">:lvar</span><span class="p">,</span> <span class="ss">:el</span><span class="p">)))))</span>
</code></pre>
<h2 id="why-macros">Why macros</h2>
<p>Macros have several use cases. Let's see how these could apply to Ruby.</p>
<h3 id="introducing-custom-syntax">Introducing custom syntax</h3>
<p>This is one of the main reasons people reach for macros, they allow you to extend a language in a way that blends in seamlessly with the existing language constructs.</p>
<p>For example, most LISPs have three conditional constructs, <code>if</code>, <code>when</code>, and <code>cond</code>. Often <code>if</code> and <code>when</code> are macros that rely on <code>cond</code>, but it's impossible to tell. They all look like regular language features.</p>
<pre class="highlight plaintext"><code>(if x
a
b)
(when x
a)
(cond
x a
y b)
</code></pre>
<p>Because LISP has macros, it is possible to add languages features in 3rd party code, features that would have to be provided by the language implementor otherwise. There are LISP libraries that add object orientation, CSP channels, or logic programming, introducing new forms like <code>defmethod</code>, <code>defclass</code>, or Go loops.</p>
<p>We can do similar things with Ruby macros, but because we are bound by the existing parser we can't introduce new forms. So our macro calls will resemble method calls, rather than built-in keywords. The issue here is that the parser is aware of keywords like <code>class</code> or <code>def</code>, but it doesn't know about the keywords our macros try to introduce.</p>
<p>As an illustration, originally I thought of having macros look like this:</p>
<pre class="highlight ruby"><code><span class="n">defmacro</span> <span class="n">foo</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="c1">#...</span>
<span class="k">end</span>
</code></pre>
<p>This is not syntactically valid because the parser doesn't know that defmacro starts a block, so the trailing end causes it to blow up. This would have worked</p>
<pre class="highlight ruby"><code><span class="n">defmacro</span> <span class="n">foo</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="k">do</span>
<span class="c1"># ...</span>
<span class="k">end</span>
</code></pre>
<p>But the <code>do</code> makes this look inconsistent, and exposes it as the hack it is, since the parser will consider this two function calls.</p>
<p>This all felt a bit too hodgepodgey to me, so I settled on using regular <code>def</code>, but inside a <code>Macros do ; end</code> block.</p>
<pre class="highlight ruby"><code><span class="no">Macros</span> <span class="k">do</span>
<span class="k">def</span> <span class="nf">foo</span><span class="p">(</span><span class="n">ast</span><span class="p">)</span>
<span class="k">end</span>
<span class="k">end</span>
</code></pre>
<h3 id="performance">Performance</h3>
<p>Macros are expanded at load time, the running system is no longer aware of them. This makes it possible to apply optimizations. One example would be stripping out debug calls.</p>
<p>Ruby has elaborate support for introspection and meta programming, but it introduces an extra layer of interpretation that takes up CPU cycles. Because of this some existing, popular Ruby projects prefer mashing strings together and eval'ing the result, instead of using existing tools like define_method, thus using a half assed, informally specified, bug ridden implementation of macros. In this case they probably should just use actual macros.</p>
<h3 id="meta-programming">Meta programming</h3>
<p>There are certain problems that can perfectly be solved without macros, but somehow the level of "programming programs" makes their solution so much more elegant and concise.</p>
<p>This is probably the most contentious case to discuss. Your macro based solution may seem pure genius, but will you still be able to follow it in a few weeks or months? How about your colleagues?</p>
<p>It wouldn't be the first LISP programmer to throw away their tangled mess of macros and start over with good old reliable functions. With great power comes a great ability to screw up.</p>
<h2 id="conclusion">Conclusion</h2>
<p>Bringing macros to Ruby has been a great thought exercise, and has really made it clear to me why the two aren't a good match. Then again, maybe there are genuinely defendable use cases. Clojure's <code>core.async</code> hinges on the <code>go</code> macro, which rewrites code using async channels into a state machine. Could we port that to ruby? How about logic programming?</p>
<p>So maybe they will become just another tool in my toolbox. One I should never, ever need. Except when I do.</p>
The Amazing LISP Vimhttp://blog.url.com/2014-08-amazing-lisp-vim2014-08-31T02:00:00+02:002018-10-19T14:51:11+02:00Article Author<p>Why Vim users will switch to Emacs in the coming years.</p>
<p>I've recently stated that within the next five years half of Vim users will switch to Emacs. And while the exact time span and percentage are debatable, I stand by that statement. I think a number of factors are coming together that will make switching very appealing.</p>
<p>Editor flame wars have been around for decades. Let me do my share to fan the flames by saying this: of all modern editors, Vim has the superior editing model. The modal text-object style of editing, where few if any modifier keys are used, but instead commands are composed of keys typed in sequence is better for reasons of economy, ergonomics, and sheer expressive power.</p>
<p>Compare this with Emacs's keybindings that will never improve, and its exclusionary culture. Let's pick on the culture first.</p>
<p>Emacs is rooted in old-school hacker narrative. Some of the developers have been around for over three decades, and they like things just the way they are, making Emacs possibly the most closed open source project.</p>
<p>The community is filled with the type of people who yell heresy when the <code>Reply-to</code> of a mailing list goes back to the list. The type of people who moan about Eternal September ad nauseam. This feeds into the other sticking point: the keybindings will never, ever change. Out of respect for many man centuries of muscle memory, the defauts are set in stone.</p>
<p>Luckily both of these issues can be ignored. While Emacs is still being developed and evolving in interesting ways, the really exciting stuff is happening on top of Emacs. By now we have a very solid platform, and a new generation of developers is taking it to the next level.</p>
<p>Keep in mind that it's only a configuration detail that Emacs functions as an editor. In essence it's a VM that runs LISP. As far as UI goes you're stuck with rectangles representing buffers, but at least they're capable of displaying graphics and proportional text. It's not iOS, but for building the workbench of a software developer it's a solid base.</p>
<h2 id="the-perfect-storm">The Perfect Storm</h2>
<p>All of that isn't new, what's different this time around?</p>
<h3 id="evil-is-growing-up">Evil is Growing Up</h3>
<p>If Emacs's editing model and keybindings are so atrocious, then why not reprogram it? Evil is an Emacs extension that implements Vim in Emacs. It's been around for a while, and from what I can gather, it's quite complete. You have all the basic editing commands, can define leader key combinations, the ex commands (the <code>:</code> prompt) are implemented. An advanced Vim user will probably find some things missing. If that's you please do let me know what it is! I've been asking my Vimmy friends and so far haven't gotten much concrete feedback.</p>
<p>For an upper intermediate user it seems Evil is more than good enough, and it's only getting better.</p>
<p>Apart from the usual Vim modes (insert, visual, etc.) Evil also has an Emacs mode (toggled with Ctrl-Z), for using existing functionality. Although you can also just create custom bindings or leader key commands.</p>
<p>Evil has been developed since 2011, and is currently at version 1.0.9. It weighs about 30K SLOC, has over 1400 commits, and is actively maintained.</p>
<h3 id="elpamelpa">Elpa/Melpa</h3>
<p>Only recently has Emacs spawned a package manager. Before people would manually copy source files into their <code>~/.emacs.d/</code>. MELPA is a community-managed, curated repository that grows by the day. This makes it much more appealing for people to create packages, and easier for others to try them out. I feel it's a major catalyst for innovation.</p>
<h3 id="clojure">Clojure</h3>
<p>With Clojure, for the first time in ages we have a Lisp that is actually gaining in popularity, and for good reason: it sits on top of the most advanced VM around, it has a compelling story around concurrency and parallelism, it benefits from the huge Java/JVM ecosystem. Long story short: we are gaining Lisp developers again.</p>
<p>This is great news for Emacs. Instead of having people that know just enough Lisp to solve their immediate problems, we have a new generation of developers that is capable of developing high-quality Emacs extensions. They are challenging old ways of doing things.</p>
<h3 id="emacs-revival">Emacs Revival</h3>
<p>Watch this space, Emacs is more alive than ever. Sign of the times: only in the past year Emacs user groups were founded in Berlin, London, Paris, Madrid, Bangalore, and St. Louis.</p>
<h2 id="the-perfect-setup">The Perfect Setup</h2>
<p>So it's no secret by now: I strongly believe that where we are now, Emacs+Evil is the holy grail of editing. But if it's just a Vim clone, why bother switching?</p>
<p>This section should really be a post in itself, but I'll give a few reasons in brief.</p>
<h3 id="a-sensible-process-model">A Sensible Process Model</h3>
<p>While Emacs is single-threaded like Vim, you can spawn processes asynchronously and communicate with them from Elisp. No awkward hybrid scripting of Vim+Tmux; integrate your tests, REPL, compiler, linter, right into your Emacs workflow.</p>
<h3 id="org-mode">org-mode</h3>
<p>One of the coolest "apps" to be built on top of Emacs. Org-mode is plain text that fell in the cauldron of magic potion as a child. Keep todos and calendar items, do reproducible research and literate programming, manipulate tables with spreadsheet-like operations, capture links, snippets, todos, without disrupting your workflow, and have all of it in simple plain text, exportable to HTML, LaTeX, PDF, iCalendar and more.</p>
<h3 id="paredit--smartparens">Paredit / Smartparens</h3>
<p>Don't edit text, edit syntax trees. Paredit has been around for a long time and has always been popular with Lisp programmers. Smartparens is usable for other languages as well, and once you get used to it you'll never go back.</p>
<p>You can watch <a href="https://www.youtube.com/watch?v=D6h5dFyyUX0">this short demo</a> by <a href="https://twitter.com/emacsrocks">Emacs Rocks!</a> to get an idea.</p>
<h3 id="all-the-rest">All the rest</h3>
<p>Just some of the things that amaze the first time people see them:</p>
<p>editable-dired, expand region, multiple cursors, REPL driven development, artist-mode, tramp, magit, flycheck, the list goes on. Even Tetris.</p>
<h2 id="getting-started">Getting started</h2>
<p>There doesn't seem to be a go-to Emacs+Evil starter kit available yet, but you don't need much to get started.</p>
<p>You'll need Emacs, preferably the "real" GNU Emacs, so no Aquamacs, XEmacs or NTEmacs. Try <code>emacs --version</code> to see if it's already available on your system. You want to have at least version 24.</p>
<p>Note that pre-installed versions might not have GUI extensions compiled. This is fine for running from a terminal, but does limit things somewhat, so you might want to get a fuller version later on.</p>
<p>For Mac users, <code>brew install emacs</code> should do the trick. Debian-based systems such as Ubuntu can do <code>apt-get install emacs</code>.</p>
<p>Put this in <code>~/.emacs.d/init.el</code> (you're allowed to use Vim for this).</p>
<pre class="highlight common_lisp"><code><span class="p">(</span><span class="nb">require</span> <span class="ss">'package</span><span class="p">)</span>
<span class="p">(</span><span class="k">setq</span> <span class="nv">package-archives</span>
<span class="o">'</span><span class="p">((</span><span class="s">"gnu"</span> <span class="o">.</span> <span class="s">"http://elpa.gnu.org/packages/"</span><span class="p">)</span>
<span class="p">(</span><span class="s">"melpa"</span> <span class="o">.</span> <span class="s">"http://melpa.milkbox.net/packages/"</span><span class="p">)))</span>
<span class="p">(</span><span class="nv">package-initialize</span><span class="p">)</span>
<span class="p">(</span><span class="nb">when</span> <span class="p">(</span><span class="nb">not</span> <span class="p">(</span><span class="nv">package-installed-p</span> <span class="ss">'evil</span><span class="p">))</span>
<span class="p">(</span><span class="nv">package-refresh-contents</span><span class="p">)</span>
<span class="p">(</span><span class="nv">package-install</span> <span class="ss">'evil</span><span class="p">))</span>
<span class="p">(</span><span class="nv">evil-mode</span><span class="p">)</span>
</code></pre>
<p>Now simply launch <code>emacs</code>. It will install Evil on its first run, and enable it in all buffers. Use Ctrl-Z to switch back to Emacs mode if necessary.</p>
<p>Some things to try:</p>
<p>Follow the Vim tutor: <code>:edit /usr/share/vim/vim74/tutor/tutor</code>, or read the Evil manual: <code>C-h i m Evil <RET></code>.</p>
<p>Enjoy your Amazing LISP Vim.</p>
The Unhappy Lambdahttp://blog.url.com/2014-06-the-unhappy-lambda2014-06-04T02:00:00+02:002018-10-19T14:51:11+02:00Article Author<p>tl;dr: Progress on The Happy Lambda is currently blocked, because I didn't think it through before starting, and because functional programming in Ruby sucks.</p>
<p>It's over half a year now since I started this project. I've gotten a lot of support since then, and I'm very grateful for that. However lately progress has been slacking. I still intend to finish the book, but I need to take a step back and rethink some assumptions.</p>
<p>I'm much more a Rubyist than a functional programmer, however I've dabbled with Haskell and a few LISPs, and I find a lot to like there. When I started work on <a href="https://github.com/plexus/hexp">Hexp</a> I decided to make all the core types immutable. This forced me to do certain things in a functional style, and I came up with some interesting patterns and techniques along the way. Parallel to that I spent time toying around trying to bring more ideas from functional languages to Ruby, getting creative with lambdas, that sort of thing. I combined all this in a lightning talk and delivered it at a few conferences, and people were enthusiastic. So on the long train ride from ArrrrCamp back to Berlin I started writing The Happy Lambda.</p>
<p>My original scope for the book was roughly this</p>
<ul>
<li>explain and demystify FP terms and concepts</li>
<li>a "practical" part with patterns and tips on how to use those FP concepts to write better code</li>
<li>an "experimental" part where I use Ruby's malleability to make it resemble as much a functional programming language as possible</li>
</ul>
<p>I've written little bits of all three parts, but now I find I'm unsure how to continue. What I really didn't think through enough is who this book is for, and what the book's goal is.</p>
<p>One of the things I had in mind originally was that I would teach functional programming using Ruby, so you can learn all that stuff "in the comfort of your own home", your known language, so to speak. The thing is that Ruby makes doing stuff in a functional way very complicated. I'll get to that in a bit but basically there is a lot of syntax that just works against you. So while it's possible to explain and showcase a lot of the concepts in Ruby, it just seems really pointless. It's hard to demonstrate the benefits of a technique when taken at face value all it does is make your code more cryptic.</p>
<p>I also tried to make the book very beginner friendly, assuming nothing but a basic knowledge of Ruby. But even for beginners, maybe especially for beginners, it makes more sense to explain laziness, partial application, functional composition, etc, in a language that makes these things elegant. So if one of the book's goals is teaching these concepts to people that never came across them, maybe I should be using Haskell or Clojure or ML for my code samples.</p>
<p>So what is it that sucks so much? Well let's see, if FP is about <em>functions</em>, and the elegance of treating functions as first class citizens, then Ruby surely offers us <em>even more than we could wish for</em> (note my sarcasm). Instead of the unifying concept of a function, and the simple mechanism of passing functions around, we have procs, lambdas, methods and method objects. Three of these are kind of like first class functions. Each is very much unlike the other. We have a million different syntactic constructs to arrive at them, and two separate mechanisms for passing a <em>function-like-thing</em> to another <em>function-like-thing</em>.</p>
<p>On the other hand we have very little general purpose higher order functions, unless you count the stuff in Enumerator. There's not even a general purpose "compose" (f comes after g). Let alone a "juxtapose", "bind", whatever. Procs and lambdas have "curry", except that it throws together currying and partial application, has pretty funny semantics when used on varargs, and is described by core developers as an "<a href="https://bugs.ruby-lang.org/issues/6253">easter egg</a>". But it's better than nothing. A <a href="https://bugs.ruby-lang.org/issues/9783">patch</a> to add "curry" to Method, so at least these three <em>function-like-things</em> are a little more like one another, hasn't received any feedback more than a month later. Which just goes to show that while the situation is bad, no one in a position to do something about it cares a single fuck.</p>
<p>Another small but annoying thing you've probably never realized until you started making your classes immmutable, the <code>foo.bar=baz</code> syntax is completely unusable. No matter what a method ending on <code>=</code> returns, the actual return value is always what was passed in. So the only side-effect free method you can make that ends on an <code>=</code> is the identity function. A persistent hashmap structure can't use <code>hashmap[:foo]=:bar</code>, it has to use something like <code>hashmap.put(:foo, :bar)</code>. Sure it's a tiny thing, but it's the straw that breaks my pseudo-functional Ruby's back.</p>
<p>So now you're sitting back and grinning thinking, "but Arne, please, surely you could have realized all of that earlier". And fair enough, I chose Ruby+functional for the topic of this book even though it's not the most natural fit, because I felt there was something there that was worth exploring. And I still do.</p>
<p>I'm still undecided how much of the stuff you do with functions in an FP language can be easily brought to Ruby, but apart from functions FP brings something else to the table, something maybe even more important: values (the "immutable" is implied).</p>
<p>FP is a collection of techniques that together have some interesting emergent properties, but that doesn't mean it's all or nothing. And while some will say that <a href="http://queue.acm.org/detail.cfm?id=2611829">"mostly functional" programming does not work</a>, I think getting used to building systems based on value semantics is something all programmers should be doing, and it's probably the biggest lesson Ruby can take home from the functional world. If you're not following there please watch Rich Hickey's excellent <a href="http://www.youtube.com/watch?v=-6BsiVyC1kM">The Value of Values</a>.</p>
<p>I'm clearly not the only one reasoning in this direction. To have value semantics of composed data types without sacrificing too much in terms of performance you need good persistent data types, and several projects are underway to bring these to Ruby. I've started a humble effort to <a href="https://github.com/plexus/rubydataspec">coordinate and align these efforts by having shared specs and benchmarks</a>.</p>
<p>So maybe that should form the core of the book? Less about functions and lambdas, more about values? It's certainly more practical advice then trying to write lisp-with-ruby-syntax. Except there's no implementation of persistent data structures I would recommend to use in a production setting today, so how pragmatic are we talking, really? But yeah, maybe starting from the "value object" section I already have, and turn that into half a book, show how it composes into bigger systems. Demonstrate helpful gems like Anima, Adamantium, Equalizer. Show step by step how to implement a cons based list, a hash array mapped trie, a zipper, that kind of thing.</p>
<p>And for the "functional" stuff, I think for that there's a bright (or at least dimly lit) future as well. I've been trying stuff out for over a year, for Hexp, for Yaks, for other projects. I bundled a bunch of utility functions <a href="https://github.com/plexus/fp/blob/master/lib/fp.rb">here</a>, but I can't say I've found a sweet spot of expressive syntax just yet.</p>
<p>Conclusion: I need to clarify who this book is for, and what it tries to achieve. I need to write an outline, and basically (almost) start from scratch. I've also been doing a lot of traveling the past year, which has really cut into my productivity. I will be back in Berlin in a few days and plan to travel a lot less the coming months. I also went from contracting five days a week to four days a week. All of that means that I should have some time on my hands. I intend to start working a bit harder on my open source projects, especially <a href="https://github.com/plexus/hexp">Hexp</a> and <a href="https://github.com/plexus/yaks">Yaks</a>, and also on <a href="https://github.com/plexus/rubydataspec">RubyDataSpec</a>, which should indirectly keep me involved in <a href="https://github.com/hamstergem/hamster">Hamster</a>, <a href="https://github.com/Who828/persistent_data_structures">Persistent</a> (working title) and <a href="https://github.com/headius/clojr">Clojr</a>. Hopefully I can then return to writing on the Happy Lambda with more experience, a better battle plan, and higher confidence.</p>
<p>Thanks for listening.</p>
Rails is No Longer Alonehttp://blog.url.com/2014-05-rails-is-not-alone2014-05-16T02:00:00+02:002018-10-19T14:51:11+02:00Article Author<p>This is an article I've been meaning to write for a while. I was pushed over the edge by Adam Hawkin's post <a href="http://hawkins.io/2014/05/fragmentation_in_the_ruby_community/">Fragmentation in the Ruby Community</a>. Adam writes that fragmentation in the community is accelerating, and that Rails is a major fault line. I'd like to add some context and my personal view on the Rails vs "pure Ruby" debate.</p>
<p>I don't remember exactly the first time I came across Ruby. It must have been 2005 or 2006, and it most certainly was because of Rails.</p>
<p>Rails was revolutionary at the time. It combines a powerful and pleasant language with a pragmatic "rapid application development" approach. Getting so much from doing so little was truly a breakthrough. And Rails is still one of the most complete solutions out there. But it has its flaws.</p>
<p>When talking about Rails we have to remember the time it stems from. Rails chose a language that not many people were using at the time, and Ruby's ecosystem was tiny compared to what is out there now. There was no Rack, no Nokogiri, no Rspec. Chances of already being a Ruby programmer and then moving on to Rails were small. Instead people came to Rails from Java, C++, Perl, Python. They learned "Rails Ruby", the distinction didn't matter.</p>
<p>There also wasn't a whole lot available in the Ruby world to build upon when it came to web programming. People were using the standard lib's CGI module, that's pretty much as far as it went.</p>
<p>These circumstances make some choices that Rails made very reasonable. The Ruby language explicitly allows extending core classes, so why not use that capability to make it even easier, even better? There weren't that many third party libs available, so interop was only of limited concern. In fact most third party stuff that came later was built explicitly for Rails, so those libs would take care of not conflicting with Rails, with ActiveSupport.</p>
<p>The ability to reopen classes was in turn embraced by much that came after Rails, extending and changing Rails classes to make them "even better". It wasn't too hard to see this style of development would lead to a mess. But hey, it worked, startups shipped and made money. Life was good.</p>
<p>Looking at it from a 2005 perspective also explains why Rails has this "everything but the kitchen sink" approach. Rails was ambitious from the start, trying to solve as many challenges of web development it possibly could. And since there was very little to build upon, it had to do all of that itself. This explains the strong "Not Invented Here" tendencies of the Rails developers. And the fact that "adding tech available in gems to core" is an <a href="https://github.com/rails/rails/pull/12824#issuecomment-31039769">official policy</a>.</p>
<p>In short, Rails assumes it is alone in the world, whereas in fact it no longer is. We now have a rich Ruby ecosystem. Building on top of that, using a modular approach, promoting libraries over frameworks, having lean, focused components each with their own maintainers. Libraries that stick to their own namespace (sorry, major pet peeve there). That's how I envision a healthy Ruby ecosystem. I think it's what many people hunker for. It's what I see in initiatives like <a href="http://microrb.com/">micro-rb</a>.</p>
<p>I think what turns people away from Rails is basically this, the accidental complexity that grew out of being an island. The ball of mud that is ActiveSupport. I don't see this getting better. But I do see a vibrant community of people and projects with a different vision, one that grows by the day. One that values high quality Ruby code, and high quality Ruby implementations. The future is bright. Yay Ruby!</p>
HTML and URI's in Javascripthttp://blog.url.com/2014-05-HTML-and-URIs-in-Javascript2014-05-14T02:00:00+02:002018-10-19T14:51:11+02:00Article Author<p>I have <a href="http://devblog.arnebrasseur.net/2013-04-plain-text">written</a> and <a href="http://devblog.arnebrasseur.net/speaking.html#rulu2013">spoken</a> a <a href="http://devblog.arnebrasseur.net/speaking.html#eurucamp2013">few</a> <a href="http://devblog.arnebrasseur.net/speaking.html#rubyconfar2014">times</a> about the perils of string arithmetic on formal data. Those talks were focused on theory and fundamentals, in this post you'll get very practical tips.</p>
<p>When programming for the web there are two types of formal data you'll come across <em>All The Time</em>, HTML and URIs. These formats have well specified structure and semantics, so that machines can unambiguously generate and consume them. Don't try to do what the machine does better, or you'll shoot yourself in the foot.</p>
<h2 id="uris">URIs</h2>
<p>This one is easy: use <a href="https://github.com/medialize/URI.js">URI.js</a>. It is unfortunate that browsers don't have built-in APIs to deal with URIs in a sane way, but URI.js really gives you all you need.</p>
<p>Some simple examples</p>
<pre class="highlight javascript"><code><span class="c1">// bad</span>
<span class="nb">window</span><span class="p">.</span><span class="nx">location</span><span class="p">.</span><span class="nx">origin</span> <span class="o">+</span> <span class="s1">'/foo/bar'</span>
<span class="c1">//good</span>
<span class="nx">URI</span><span class="p">(</span><span class="s1">'/foo/bar'</span><span class="p">).</span><span class="nx">absoluteTo</span><span class="p">(</span><span class="nb">window</span><span class="p">.</span><span class="nx">location</span><span class="p">.</span><span class="nx">origin</span><span class="p">).</span><span class="nx">toString</span><span class="p">()</span>
<span class="c1">// bad</span>
<span class="nx">uri</span> <span class="o">=</span> <span class="s1">'http://example.com/posts/'</span> <span class="o">+</span> <span class="nx">escapeURI</span><span class="p">(</span><span class="nx">postId</span><span class="p">)</span> <span class="o">+</span> <span class="s1">'/comments/'</span> <span class="o">+</span> <span class="nx">escapeURI</span><span class="p">(</span><span class="nx">commentId</span><span class="p">)</span>
<span class="c1">// good</span>
<span class="nx">uri</span> <span class="o">=</span> <span class="nx">URI</span><span class="p">.</span><span class="nx">expand</span><span class="p">(</span><span class="s1">'http://example.com/posts/{pid}/comments/{cid}'</span><span class="p">,</span> <span class="p">{</span><span class="na">pid</span><span class="p">:</span> <span class="nx">postId</span><span class="p">,</span> <span class="na">cid</span><span class="p">:</span> <span class="nx">commentId</span><span class="p">))</span>
<span class="c1">// complete example from the README</span>
<span class="nx">URI</span><span class="p">(</span><span class="s2">"http://example.org/foo.html?hello=world"</span><span class="p">)</span>
<span class="p">.</span><span class="nx">username</span><span class="p">(</span><span class="s2">"rodneyrehm"</span><span class="p">)</span>
<span class="c1">// -> http://rodneyrehm@example.org/foo.html?hello=world</span>
<span class="p">.</span><span class="nx">username</span><span class="p">(</span><span class="s2">""</span><span class="p">)</span>
<span class="c1">// -> http://example.org/foo.html?hello=world</span>
<span class="p">.</span><span class="nx">directory</span><span class="p">(</span><span class="s2">"bar"</span><span class="p">)</span>
<span class="c1">// -> http://example.org/bar/foo.html?hello=world</span>
<span class="p">.</span><span class="nx">suffix</span><span class="p">(</span><span class="s2">"xml"</span><span class="p">)</span>
<span class="c1">// -> http://example.org/bar/foo.xml?hello=world</span>
<span class="p">.</span><span class="nx">query</span><span class="p">(</span><span class="s2">""</span><span class="p">)</span>
<span class="c1">// -> http://example.org/bar/foo.xml</span>
<span class="p">.</span><span class="nx">tld</span><span class="p">(</span><span class="s2">"com"</span><span class="p">)</span>
<span class="c1">// -> http://example.com/bar/foo.xml</span>
<span class="p">.</span><span class="nx">query</span><span class="p">({</span> <span class="na">foo</span><span class="p">:</span> <span class="s2">"bar"</span><span class="p">,</span> <span class="na">hello</span><span class="p">:</span> <span class="p">[</span><span class="s2">"world"</span><span class="p">,</span> <span class="s2">"mars"</span><span class="p">]</span> <span class="p">});</span>
</code></pre>
<p>There are tons of edge cases that this covers that your naive let's-mash-some-strings-together code does not, including proper escaping.</p>
<p><strong>Update</strong></p>
<p>I should have mentioned this earlier, <a href="http://tools.ietf.org/html/rfc6570">URI templates</a> are actually a RFC standardized mechanism for building and recognizing URIs. This is what <code>URI.expand</code> above is based on. It's a very useful and underused mechanism.</p>
<h2 id="html">HTML</h2>
<p>In contrast to URIs, browsers do come with a sane API for building HTML, it's called the DOM (Document Object Model) API.</p>
<pre class="highlight javascript"><code><span class="kd">var</span> <span class="nx">divNode</span> <span class="o">=</span> <span class="nb">document</span><span class="p">.</span><span class="nx">createElement</span><span class="p">(</span><span class="s2">"div"</span><span class="p">);</span>
<span class="kd">var</span> <span class="nx">textNode</span> <span class="o">=</span> <span class="nb">document</span><span class="p">.</span><span class="nx">createTextNode</span><span class="p">(</span><span class="s2">"We all live in happy HTML! &<>"</span><span class="p">);</span>
<span class="nx">divNode</span><span class="p">.</span><span class="nx">appendChild</span><span class="p">(</span><span class="nx">textNode</span><span class="p">);</span>
<span class="nb">document</span><span class="p">.</span><span class="nx">body</span><span class="p">.</span><span class="nx">appendChild</span><span class="p">(</span><span class="nx">divNode</span><span class="p">);</span>
</code></pre>
<p>So that's great, except that no one wants to actually write code like that, so people end up committing atrocities like setting <code>innerHTML</code> with the tagsoup of the day. Notice though how this version has already eliminated the need of manually calling escape functions.</p>
<p>The highly informative <a href="https://developer.mozilla.org">MDN</a> article <a href="https://developer.mozilla.org/en-US/Add-ons/Overlay_Extensions/XUL_School/DOM_Building_and_HTML_Insertion">DOM Building and HTML Insertion</a>. has some great tips, for instance a handy jsonToDOM function.</p>
<p>The implementation there is already quite clever, allowing one to set event handlers in one go. Since this article is meant for people building browser extensions, it also has some XUL stuff that's not relevant when programming for the web.</p>
<pre class="highlight javascript"><code><span class="nb">document</span><span class="p">.</span><span class="nx">body</span><span class="p">.</span><span class="nx">appendChild</span><span class="p">(</span><span class="nx">jsonToDOM</span><span class="p">(</span>
<span class="p">[</span><span class="s2">"div"</span><span class="p">,</span> <span class="p">{},</span>
<span class="p">[</span><span class="s2">"a"</span><span class="p">,</span> <span class="p">{</span> <span class="na">href</span><span class="p">:</span> <span class="nx">href</span><span class="p">,</span> <span class="na">onclick</span><span class="p">:</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span> <span class="p">}</span> <span class="p">},</span> <span class="nx">text</span><span class="p">])));</span>
</code></pre>
<p>Great idea, and with some tweaking very useful in a browser context. But chances are you're already using jQuery, in which case I have good news for you: jQuery has everything covered!</p>
<pre class="highlight javascript"><code><span class="kd">var</span> <span class="nx">divNode</span> <span class="o">=</span> <span class="nx">$</span><span class="p">(</span><span class="s1">'<div>'</span><span class="p">,</span> <span class="p">{</span><span class="na">class</span><span class="p">:</span> <span class="s1">'my-div'</span><span class="p">}).</span><span class="nx">append</span><span class="p">(</span><span class="nx">$</span><span class="p">(</span><span class="s1">'<a>'</span><span class="p">,</span> <span class="p">{</span><span class="na">href</span><span class="p">:</span> <span class="s1">'..'</span><span class="p">}));</span>
</code></pre>
<p>The <code>$('<tag>', {attributes})</code> syntax provides an easy way to build DOM objects. The result is a <a href="http://learn.jquery.com/using-jquery-core/jquery-object/">jQuery object</a>. You'll have to unwrap it to get to actual DOM element.</p>
<pre class="highlight javascript"><code><span class="kd">var</span> <span class="nx">domNode</span> <span class="o">=</span> <span class="nx">divNode</span><span class="p">[</span><span class="mi">0</span><span class="p">];</span>
</code></pre>
<p>You might want to convert this to an HTML string now. In that case it's highly likely you're doing it wrong, but there are some cases where this is actually legit, e.g. Ember.js Handlebars helpers don't allow returning DOM nodes. I assume this will change with HTMLBars.</p>
<p>In this case keep in mind that calling <code>html()</code> on the jQuery object will only return the <em>inner</em> HTML. You can get the full thing from the DOM node though.</p>
<pre class="highlight javascript"><code><span class="kd">var</span> <span class="nx">nodeHTML</span> <span class="o">=</span> <span class="nx">divNode</span><span class="p">[</span><span class="mi">0</span><span class="p">].</span><span class="nx">outerHTML</span><span class="p">;</span>
</code></pre>
<p>For example in Ember.js:</p>
<pre class="highlight javascript"><code><span class="nx">Ember</span><span class="p">.</span><span class="nx">Handlebars</span><span class="p">.</span><span class="nx">registerBoundHelper</span><span class="p">(</span><span class="s1">'linkToPost'</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">postId</span><span class="p">)</span> <span class="p">{</span>
<span class="kd">var</span> <span class="nx">uri</span> <span class="o">=</span> <span class="nx">URI</span><span class="p">.</span><span class="nx">expand</span><span class="p">(</span><span class="s1">'/posts/{id}'</span><span class="p">,</span> <span class="p">{</span><span class="na">id</span><span class="p">:</span> <span class="nx">postId</span><span class="p">});</span>
<span class="kd">var</span> <span class="nx">html</span> <span class="o">=</span> <span class="nx">$</span><span class="p">(</span><span class="s1">'<a>'</span><span class="p">,</span> <span class="p">{</span><span class="na">href</span><span class="p">:</span> <span class="nx">uri</span><span class="p">,</span> <span class="na">text</span><span class="p">:</span> <span class="s2">"goto post"</span><span class="p">})[</span><span class="mi">0</span><span class="p">].</span><span class="nx">outerHTML</span><span class="p">;</span>
<span class="k">return</span> <span class="k">new</span> <span class="nx">Handlebars</span><span class="p">.</span><span class="nx">SafeString</span><span class="p">(</span><span class="nx">html</span><span class="p">);</span>
<span class="p">});</span>
</code></pre>
<h2 id="putting-the-two-together">Putting the two together</h2>
<p>Take this simple function</p>
<pre class="highlight javascript"><code><span class="kd">function</span> <span class="nx">linkToPost</span><span class="p">(</span><span class="nx">postId</span><span class="p">)</span> <span class="p">{</span>
<span class="kd">var</span> <span class="nx">uri</span> <span class="o">=</span> <span class="s1">'/posts/'</span> <span class="o">+</span> <span class="nb">encodeURI</span><span class="p">(</span><span class="nx">postId</span><span class="p">);</span>
<span class="k">return</span> <span class="s1">'<a href="'</span> <span class="o">+</span> <span class="nx">uri</span> <span class="o">+</span> <span class="s1">'">goto post</a>'</span><span class="p">;</span>
<span class="p">}</span>
</code></pre>
<p>The problem here is that there are two levels of interpretation going on. While the URI is correctly escaped in itself, when placing it in the context of HTML, in particular as an attribute value, there's extra escaping that needs to happen, so the value can't break out of the attribute (by including ' or ") or out of the HTML tag (by including > or <).</p>
<p>Escaping always depends on context, and if there are multiple levels of context the manual approach will always fail, without fault. In short if you find yourself:</p>
<ul>
<li>writing HTML fragments inside strings ('<a href=…')</li>
<li>calling escape functions (e.g. for URI or HTML) manually</li>
</ul>
<p>think if you can let some other component that knows the details of the language you're generating better than you do, to do the work for you. Here's a corrected version of the above.</p>
<pre class="highlight javascript"><code><span class="kd">function</span> <span class="nx">linkToPost</span><span class="p">(</span><span class="nx">postId</span><span class="p">)</span> <span class="p">{</span>
<span class="kd">var</span> <span class="nx">uri</span> <span class="o">=</span> <span class="nx">URI</span><span class="p">.</span><span class="nx">expand</span><span class="p">(</span><span class="s1">'/posts/{id}'</span><span class="p">,</span> <span class="p">{</span><span class="na">id</span><span class="p">:</span> <span class="nx">postId</span><span class="p">});</span>
<span class="k">return</span> <span class="nx">$</span><span class="p">(</span><span class="s1">'<a>'</span><span class="p">,</span> <span class="p">{</span><span class="na">href</span><span class="p">:</span> <span class="nx">uri</span><span class="p">,</span> <span class="na">text</span><span class="p">:</span> <span class="s2">"goto post"</span><span class="p">});</span>
<span class="p">}</span>
</code></pre>
<h2 id="finally">Finally</h2>
<p>Browsers don't come with a function for manually escaping HTML. That is because <em>you don't need it</em>. Having it there might encourage bad practices and hence do more bad than good.</p>
<p>But as with everything there are exceptions. If you really need to escape HTML, and you're sure your use case is legit, there are a few options.</p>
<p>Let the browser do it for you:</p>
<pre class="highlight javascript"><code><span class="kd">var</span> <span class="nx">divNode</span> <span class="o">=</span> <span class="nb">document</span><span class="p">.</span><span class="nx">createElement</span><span class="p">(</span><span class="s2">"div"</span><span class="p">);</span>
<span class="kd">var</span> <span class="nx">textNode</span> <span class="o">=</span> <span class="nb">document</span><span class="p">.</span><span class="nx">createTextNode</span><span class="p">(</span><span class="s2">"We all live in happy HTML! &<>"</span><span class="p">);</span>
<span class="nx">divNode</span><span class="p">.</span><span class="nx">innerHTML</span> <span class="c1">// "We all live in happy HTML!We all live in happy HTML! &amp;&lt;&gt;"</span>
</code></pre>
<p>Use <a href="http://underscorejs.org/">Underscore.js</a></p>
<pre class="highlight javascript"><code><span class="nx">_</span><span class="p">.</span><span class="nx">escape</span><span class="p">(</span><span class="s2">"We all live in happy HTML! &<>"</span><span class="p">);</span>
<span class="c1">// "We all live in happy HTML!We all live in happy HTML! &amp;&lt;&gt;"</span>
</code></pre>
<p>or copy any of the functions you find on the web. Make sure it escapes < > ' " &.</p>