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

  <title><![CDATA[Leonardo Borges]]></title>
  <link href="http://www.leonardoborges.com/writings/atom.xml" rel="self"/>
  <link href="http://www.leonardoborges.com/writings/"/>
  <updated>2018-02-19T20:38:30+11:00</updated>
  <id>http://www.leonardoborges.com/writings/</id>
  <author>
    <name><![CDATA[Leonardo Borges]]></name>
    
  </author>
  <generator uri="http://octopress.org/">Octopress</generator>

  
  <entry>
    <title type="html"><![CDATA[Highlights of the Symposium on Blockchain and Distributed Ledger Technology - Day 2]]></title>
    <link href="http://www.leonardoborges.com/writings/2018/02/19/highlights-of-the-symposium-on-blockchain-and-distributed-ledger-technology-day-2/"/>
    <updated>2018-02-19T20:30:00+11:00</updated>
    <id>http://www.leonardoborges.com/writings/2018/02/19/highlights-of-the-symposium-on-blockchain-and-distributed-ledger-technology-day-2</id>
    <content type="html"><![CDATA[<p>If you haven&#8217;t yet, make sure you read my <a href="http://www.leonardoborges.com/writings/2018/02/19/highlights-of-the-symposium-on-blockchain-and-distributed-ledger-technology-day-1/">summary of Day 1</a> first.</p>

<h2>Morning Panel</h2>

<p>Day 2 started with a morning panel with:</p>

<ul>
<li>Paul Fox from <a href="http://www.blockchainpacific.capital/team/">Blockchain.capital</a></li>
<li>Leon Gerard Vandenberg from RightsFusion Pty Ltd &amp; <a href="http://www.solara.io/">Solara</a></li>
<li>Oliver Harvey from <a href="http://asic.gov.au/about-asic/what-we-do/our-structure/asic-senior-executives/asic-senior-executives-biographies/">ASIC</a></li>
<li>Ross Buckley from <a href="http://www.law.unsw.edu.au/profile/ross-buckley">UNSW Law</a></li>
</ul>


<p>The panelists took turns in giving their views around blockchain, investments and regulation, leading into a Q&amp;A at the end. I was running late to this session and believe I may have missed Ross&#8217; individual slot.</p>

<h3>Paul&#8217;s slot - I&#8217;m a VC and Bitcoin stole my job</h3>

<p>Paul raises an interesting perspective on Angel investors. They don&#8217;t invest to make money. To them it&#8217;s a hobby. There are other, provably more profitable ways to make money.</p>

<p>He also raises the fact that sure there are lots of ICOs but more and more of them seem to be missing their targets. Many reasons but one he mentioned was China&#8217;s clampdown on cryptocurrencies.</p>

<p>ICOs are so hot right now that there are companies dedicated to helping it happen with pre-packaged solutions for launching and marketing the token generation event: <a href="https://icobox.io/">https://icobox.io/</a> </p>

<p>With so many ICOs it becomes increasingly difficult to discern good ideas from bad ones and then from plain scams. Nevertheless people are still innovating and are not deterred by these challenges. One example is <a href="https://www.trusttoken.com/">TrustToken</a>, which tokenises physical assets.</p>

<h3>Leon&#8217;s slot</h3>

<p>Leon&#8217;s particularly not happy with the current Australian Guidance towards ICOs. The current challenge is how to protect consumers, while still enabling companies and entrepreneurs trying to do a legit ICO - I&#8217;m assuming Leon is referring here to the plethora of scam ICOs out there.</p>

<p>To Leon - and many in the audience - an ICO is just another tool with very desirable properties: it gives companies the means to connect with individual investors who believe in their cause without diluting equity. In addition, Leon &amp; his board had their share of bad experiences when trying to seek legal advice around ICOs and explaining their SOL Token</p>

<p>There are many types of tokens/coins: equity tokens, cause coins, utility tokens&#8230; I knew about some of these but the concept of cause coins is new to me - say, SaveTheElephantsCoin.</p>

<h3>Oliver&#8217;s slot</h3>

<p>Oliver was basically talking about capital raising. </p>

<p>In particular he points out that the number of IPOs in the US is at an all time low whereas in Australia the trajectory is in the opposite direction. Unfortunately he didn&#8217;t dive into the why of this change in scenery in the US.</p>

<p>Personally I wanted to have asked if the decline in IPOs is due to company expansion by acquisition. Companies like Google, Facebook and Atlassian have acquired dozens of smaller successful companies who could have otherwise gone public - is there a correlation here?</p>

<p>Another piece of data Oliver shared is that 25% of companies listed on the ASX have a market cap of up to $5M - he then goes to say that if a company is looking to raise about that much money crowdfunding platforms such as ICOs might just be the way to go.</p>

<h2>Audience Q&amp;A</h2>

<h3>Are ICOs making entrepreneurs soft? </h3>

<p>The problem raised by this question is whether having too much money too soon - as is the case with lots of ICOs -  can be bad for founders.</p>

<p>The panel&#8217;s general opinion is that it can be a hindrance: it may lead to scaling up too soon and losing focus as one now thinks it can do many different things at the same time. VCs in general are sceptical of this.</p>

<p>One way to mitigate this mentioned in the event is to have the funds locked up by smart contracts (potentially tied to milestones as opposed to simple time triggers). </p>

<p>The bottom line is that ICOs are still evolving.</p>

<h3>Why invest in ICOs vs traditional investments? Are we opening the floodgates for less knowledgeable, naive people to invest?</h3>

<p>Should mom and dad be investing? Maybe not - there isn&#8217;t enough information out there, not enough regulation. We&#8217;re not there yet.</p>

<p>That said, what ICOs - in particular the tokens people invest in - provide is unprecedented liquidity. Tokens aren&#8217;t going away.</p>

<p>In the midst of all this information exchange Naval Narvikant was mentioned as someone to watch. He&#8217;s got a couple of articles and podcasts which have been recommended:</p>

<ul>
<li><a href="https://medium.com/@gifted_products/cryptocurrencies-with-tim-ferriss-nick-szabo-and-naval-ravikant-51a99d037e04">https://medium.com/@gifted_products/cryptocurrencies-with-tim-ferriss-nick-szabo-and-naval-ravikant-51a99d037e04</a></li>
<li><a href="http://unchainedpodcast.co/naval-ravikant-on-how-crypto-is-squeezing-vcs-hindering-regulators-and-bringing-users-choice">http://unchainedpodcast.co/naval-ravikant-on-how-crypto-is-squeezing-vcs-hindering-regulators-and-bringing-users-choice</a></li>
</ul>


<h2>Software architecture and engineering for blockchain applications - Ingo Weber - Data61</h2>

<p>This next talk was a lot more technical and was delivered by <a href="http://people.csiro.au/W/I/Ingo-Weber">Dr Ingo Weber</a> from Data61. I didn&#8217;t take as many notes on this one as a version of the slides is <a href="https://www.slideshare.net/mobile/IngoWeber2/blockchains-and-smart-contracts-architecture-design-and-modeldriven-development">already available</a>.</p>

<p>Dr Weber started with a demo of <a href="http://ethviewer.live/">ethviewer</a> - a way to visualise the ethereum blockchain in realtime and observe how transactions are received, processed and added to blocks. Just plain cool!</p>

<p>The general message is that most blockchain applications have the blockchain as just another component in their architecture and the strengths and weaknesses of each component should be respected and planned for. e.g.: don&#8217;t do big data processing on the blockchain</p>

<p>In addition Ingo mentioned the <a href="http://redbellyblockchain.io/">Red Belly Blockchain</a>, an unforkable blockchain built down under.</p>

<h2>Blockchain risks, Opportunities and Future scenarios - Mark Staples - Data61</h2>

<p>The next speaker, <a href="http://people.csiro.au/S/M/Mark-Staples">Dr Mark Staples</a>, is also from Data61. </p>

<p>Dr Staples explores a couple of reports published by Data61 about the future of Blockchain in Australia. These reports can be found <a href="http://www.data61.csiro.au/en/Our-Work/Safety-and-security/Secure-Systems-and-Platforms/Blockchain">here</a>.</p>

<p>What&#8217;s changed since these reports:</p>

<ul>
<li>Active interest and growing understanding by regulators</li>
<li>Australia leading the <a href="https://www.iso.org/committee/6266604.html">ISO TC307</a> standard on Blockchain and DLT</li>
<li>Widespread adoption and benefits yet to come</li>
<li>Cryptocurrencies are a double-edged sword for Blockchain and DLT</li>
<li>Technological innovation: Distributed Exchanges, Scalability, Privacy&#8230;</li>
</ul>


<h2>LoyaltyX - Philip Sheller</h2>

<p>The premise for this talk is that loyalty programs are failing to deliver.</p>

<p>Philip came from Qantas&#8217; Frequent Flyer store and knows a thing or two about loyalty programs. </p>

<p>The basic idea around <a href="https://www.loyaltyx.co/">LoyaltyX</a> is that customers get rewarded in cryptocurrencies. This presents a couple of interesting benefits:</p>

<ul>
<li>They can use the crypto they earn to buy more things at the merchant</li>
<li>Since it&#8217;s crypto, they are not locked in and can take their rewards and spend them elsewhere</li>
<li>But hey, it&#8217;s still crypto so they may choose to hold and trade at a public exchange at a later time - maybe for a profit</li>
</ul>


<p>They ran a private pilot with UNSW merchants and students and the results are promising.</p>

<h2>ROI with a stable cryptocurrency - Kevin Kirchman - WorldFree</h2>

<p>Kevin presents on <a href="https://www.worldfree.com/">FreeMark</a>. It&#8217;s an alternative to blockchain technology aimed at providing a stable coin backed by a basket of commodities. One fundamental difference is that in FreeMark you don&#8217;t necessarily  know - or care - where your coin came from.</p>

<p>There are lots of startups trying to solve the stable coin problem. I believe it&#8217;s absolutely a problem that needs solving but it&#8217;s hard to tell at the moment who will come out on top.</p>

<h2>Creating and settling agricultural assets on a blockchain - Emma Weston</h2>

<p>Emma talked about <a href="https://www.agridigital.io/">AgriDigital</a>, a startup whose goal is to enable trust and transparency for global agricultural supply chains. In a nutshell I believe AgriDigital can be described as realtime payments and digital escrow for settlement with farmers. </p>

<p>The problem they are trying to solve is for farmers to get paid quicker - oftentimes the farmer who sits at the very start of the supply chain doesn&#8217;t get paid within any reasonable amount of time. Sometimes they even loose track of their goods upstream - another reverence to the provenance issue.</p>

<p>For this to work it seems Emma needs a stable coin - again, it&#8217;s a hot topic - but for their pilot with RaboBank it was simply agreed that one of their tokens is worth 1 AUD so as to validate the technology.</p>

<h2>Final panel - Payment Systems - Reserve Bank of Australia</h2>

<p>This section was delivered under the <a href="https://en.wikipedia.org/wiki/Chatham_House_Rule">Chatham House Rules</a>. It basically means that no-one can tell you who said exactly what.</p>

<p>+I really enjoyed the discussions here. One topic was the issuance of a digital version of the Aussie dollar (DAD) which would enable many types of sophisticated payment systems. While there isn&#8217;t a conclusive indication of the future no Retail Reserve Bank sanctioned DAD was envisioned, but an AUD Bank branded &#8216;Wholesale only&#8221; DAD may be possible. I am pleasantly surprised by the level of thinking on this topic from the Blockchain community and the RBA.</p>

<h2>Summary</h2>

<p>Even though cryptocurrencies and blockchain technologies aren&#8217;t new - bitcoin was created in 2009 - you can see and feel that interest from technologists, the public and regulators has grown tremendously in recent years. </p>

<p>Blockchain technology, implications and research still have a long way to go but 2018 has started on a really good note and I can&#8217;t wait to see what the future holds.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Highlights of the Symposium on Blockchain and Distributed Ledger Technology - Day 1]]></title>
    <link href="http://www.leonardoborges.com/writings/2018/02/19/highlights-of-the-symposium-on-blockchain-and-distributed-ledger-technology-day-1/"/>
    <updated>2018-02-19T20:30:00+11:00</updated>
    <id>http://www.leonardoborges.com/writings/2018/02/19/highlights-of-the-symposium-on-blockchain-and-distributed-ledger-technology-day-1</id>
    <content type="html"><![CDATA[<p>Early last week I had the chance to attend the <a href="http://blockchain.unsw.edu.au/symposium18/HTML/index.html">Symposium on Blockchain and Distributed Ledger Technology</a> organised by UNSW. I wasn&#8217;t aware that UNSW was so heavily involved in the community until this event and have since learned about their <a href="http://blockchain.unsw.edu.au/">interest group</a> which has been going on for a while.</p>

<p>The talks had a healthy mix of research and industry case studies with subjects ranging from the technical challenges of current blockchain implementations to the legal implications of distributed ledger technology.</p>

<p>I&#8217;m not sure when and if slides will be published so I&#8217;ll do my best to summarise the talks and share its highlights. Most likely this summary will reflect my own biases on the subjects which interest me the most.</p>

<h1>Day 1</h1>

<h2>An Introduction to Distributed Ledger Technology - Ron van der Meyden (UNSW)</h2>

<p>Ron is the organiser of the interest group and this particular event. In this introduction he gave an overview of what a blockchian is and how it works. A great start for the day!</p>

<h2>Ripple - Dilip Rao</h2>

<p>Dilip is Ripple&#8217;s Global Head for Infrastructure Innovation. Currently with about $94M in funding, Ripple is backed by some fairly big enterprises and banks as well as high profile customers such as Westpac, Santander, BBVA and dozens more.</p>

<p>Ripple has made some interesting trade-offs:</p>

<ul>
<li>It accepts and embraces the fact that banks <strong>will not</strong> run on a public ledger. It simply won&#8217;t happen

<ul>
<li>One of the reasons discussed is the fact that through a combination of various sources and pattern analysis one could figure out trading volumes between banks and enterprises and they are not onboard with that.</li>
</ul>
</li>
<li>As such Ripple uses its own private ledger based on the <a href="https://interledger.org/">Interledger protocol</a>. Only on-boarded participants - banks - may interact with it. 

<ul>
<li>The product is called <a href="https://ripple.com/solutions/process-payments/">xCurrent</a>. It allows banks to settle cross-border in real time.</li>
</ul>
</li>
<li>This highlights an interesting fact: <strong>banks don&#8217;t need the Ripple cryptocurrency (XRP) to settle transactions</strong></li>
</ul>


<p>The Interledger protocol allows multiple ledgers to communicate. Currently it can communicate with the XRP(Ripple) ledger to provide fast payment channels. Due to this specialised nature, XRP now has the cheapest cost per transaction as well as being the fastest clocking in at 1500 transactions per second. <a href="https://ripple.com/insights/ripple-continues-to-bring-internet-of-value-to-life-new-features-increase-transaction-throughput-to-same-level-as-visa/">This article</a> has some more information about this.</p>

<p>These are some impressive numbers - effectively they can match VISA&#8217;s throughput - and has definitely brought up Ripple into my radar once again.</p>

<p>Ripple also offers another product, <a href="https://ripple.com/solutions/source-liquidity/">xRapid</a>, aimed at enabling realtime payments and on-demand liquidity by using XRP. Dilip offered Cuallix as a case study for this product. <a href="https://ripple.com/insights/ripple-continues-to-bring-internet-of-value-to-life-new-features-increase-transaction-throughput-to-same-level-as-visa/">Cuallix is using xRapid to reduce the cost of sending cross-border payments from the US to Mexico</a>. </p>

<p>P.S.: Just as I was writing this section I came across <a href="https://www.businessinsider.com.au/ripple-xrp-western-union-payments-testing-2018-2">another article</a> indicating Western Union is doing experiments with Ripple as well.</p>

<h2>Blockchain deconstructed : contracts vs smart contracts - Fritz Henglein (University of Copenhagen)</h2>

<p>This talk focused on the technical details of smart contracts as currently implemented in platforms such as Ethereum. In particular the fact that the contract rules and its execution are intertwined.</p>

<p>Fritz began his talk with a summary slide on smart contracts which allowed him to expand on the above:</p>

<p><img class="center" src="http://www.leonardoborges.com/writings/assets/images/posts/crash-smart-contracts.jpg" width="250" height="333" title="Crash course on Smart Contracts terminology" ></p>

<p>Due to this mixing of rules (contract checking) and execution (contract actions) in the source code - as in Ethereum smart contracts - there is no possibility of having multiple strategies which may be private and can be reused in different smart contracts.</p>

<p>Fritz argues that contract actions - which he called strategies - should be stored separately to the contract rules. In doing so such strategies can be private and re-used in different contracts. He calls this model Managed Contracts which combine the contracts with join execution strategies:</p>

<p><img class="center" src="http://www.leonardoborges.com/writings/assets/images/posts/managed-contracts.png" width="250" height="333" title="Managed Contracts" ></p>

<p>He then moves on to discuss the trade-offs and vulnerabilities of Ethereum smart contracts. I particular he raises these issues:</p>

<ul>
<li>Transaction order dependency

<ul>
<li>Messages may have different effect depending on the order in which they are received</li>
</ul>
</li>
<li>Smart contracts may behave differently depending on the timestamp of a block

<ul>
<li>The timestamp of a block is controlled by miners. He raises that this exposes the contracts to clock manipulation attacks. Frankly I&#8217;m not sure if this attack can succeed if a single miner is the bad actor. I&#8217;d need to think through this but if someone has an example handy, that&#8217;d be great.</li>
</ul>
</li>
<li>Exception handling and programming language subtleties:

<ul>
<li>Fragile gas management and limited stack </li>
<li>Differences in language constructs such as send vs call</li>
</ul>
</li>
<li>Reentrancy bugs</li>
</ul>


<p>Fritz&#8217;s proposal to deal with this are Managed Contracts and he refers to a paper - which he co-authored - called Compositional Formal Contracts. The paper proposes the following properties:</p>

<ul>
<li>Separation of concerns</li>
<li>Domain-oriented code</li>
<li>Analysable</li>
<li>Composable</li>
</ul>


<p>I couldn&#8217;t find a paper with that exact title but did find two papers co-authored by him with a similar title. <a href="http://www.diku.dk/~simonsen/papers/j6.pdf">Here</a> and <a href="http://www.diku.dk/~simonsen/papers/c2.pdf">here</a>. If you know which one he was referring to, please let me know.  </p>

<h2>Rightsfusion Pty Ltd - Solara.io - <a href="http://bit.ly/fuzo-weal">Leon Gerard Vandenberg</a> | <a href="https://www.dropbox.com/s/2mgqobppc3n4etl/UNSW%20Blockchain%20Symposium%20Briefing%202018-02-12.pdf">slides</a></h2>

<p><a href="http://www.solara.io/">Solara</a> has an interesting premise.  In the energy space, consumers and traders have difficulty determining &amp; validating green, renewable energy from fossil fuel energy. Solara allows communities and prosumers (producer / consumers) to factionalize or leverage new ownership models and redistribute and/or monetise their energy &amp; their energy data.</p>

<p>In addition, SOLARA Platform aims to provide a proveably green &amp; clean digital solar asset to a variety or energy data exchanges &amp; markets. The concept of a Tokenised &#8216;Safe Haven Asset Class&#8217; based on a hybrid portfolio of Solara PAT tokens that  could be synthesised by quants to eventually provide a stable coin was contemplated by Leon. (refer to Solara White Paper for details on Project Asset Tokens - PAT tokens)</p>

<p>Another interesting aspect is that Solara is looking to provide &#8220;fit for purpose hardware&#8221; Solara Hardware Modules to bridge the needs of both the metering and the blockchain worlds - think IoT eSIM cards which are also a secure blockchain keystore (wallet).</p>

<p>Leon &amp; team is currently working towards an ICO (private pre-sale for their SOL Token is now on-going) and looking to build out more team members (including University Labs) and RightsFusion will engage in projects for Solar Communities and solar industry participants.</p>

<p>Lastly, Leon mentioned <a href="https://www.polymath.network/">Polymath</a> in the context of ICO investments: Polymath (Canada) is working on a securities token or their ST standard. Because Solara&#8217;s PAT Tokens are a type of Financial Product - PAT Tokens could embrace the Polymath ST standard to extend their eventual reach and a broader participation model.</p>

<blockquote><p>Note: I also got some <a href="https://showcase.dropbox.com/s/RightsFusion-Pty-Ltd-Public-Documents-Public-Presentations-Events-5jLk5XfBukYVeUsZm0CAB">extra resources</a> about SOLARA straight from Leon.</p></blockquote>

<h2>The power of possibilities - Niki Ariyasinghe - R3/Corda</h2>

<p>Niki gives an overview of <a href="https://www.corda.net/">Corda</a>, an open-source blockchain project designed for building financial services infrastructure.</p>

<p>The project seems to have come about from the need for banks to collaborate and understand what blockchain means for financial services.</p>

<p>Corda displays a couple of intresting properties:</p>

<ul>
<li>It allows for transaction privacy</li>
<li>It has a pluggable consensus mechanism allowing consensus algorithms to be chosen at transaction time and not at the blockchain level

<ul>
<li>I don&#8217;t know enough about corda but this particular point makes me nervous :)</li>
</ul>
</li>
</ul>


<p>The highlight of this talk for me is <a href="http://www.mas.gov.sg/Singapore-Financial-Centre/Smart-Financial-Centre/Project-Ubin.aspx">Project Ubin</a>: a project by the Monetary Authority of Singapore to provide Central Bank backed Digital cash with the goal to use Distributed Ledger Technology (DLT) to clear and settle payments and securities.</p>

<h2>Platform cooperatives - Browen Morgan from UNSW</h2>

<p>As per Wikipedia: <em>&#8220;A platform cooperative, or platform co-op, is a cooperatively-owned, democratically-governed business that uses a protocol, website or mobile app to facilitate the sale of goods and services&#8221;</em></p>

<p>I didn&#8217;t take many notes during this talk but captured a few interesting links:</p>

<ul>
<li><a href="http://culedger.com/">CULedger</a>: A permissioned, private blockchain</li>
<li><a href="https://www.loomio.org/">Loomio</a>: Collaborative decision-making software</li>
<li><a href="https://www.provenance.org/">Provenance</a>: Building trust in goods and supply chain

<ul>
<li>It&#8217;s worth noting that the issue of provenance has been mentioned quite a few times during the Symposium</li>
</ul>
</li>
</ul>


<p>I particularly found ingenious the idea behind the following projects:</p>

<ul>
<li><a href="https://arcade.city/">Arcade.city</a>: Think peer-to-peer Uber</li>
<li><a href="https://swarm.city/">Swarm.city</a>: A decentralised commerce platform </li>
</ul>


<h2>Issues for law and the legal profession - Lyria Bennet Moses - UNSW </h2>

<p>The last one for the day, this talk is the one I was looking forward to the most. It explores concerns and legal challenges with using blockchain technology. Although not related to blockchain, our work at <a href="http://www.modron.com/">MODRON</a> offers many parallels with the themes discussed here.</p>

<p>According to Lyria - and it&#8217;s certainly true in non-blockchain tech circles as well such as AI and Machine Learning - people ask if blockchain and smart contracts will replace lawyers. </p>

<p>Her short answer is no. However certain roles might be replaced. It is already happening in domains such as exchange of documents. Basically she thinks that <em>&#8220;the things that junior lawyers do&#8221;</em> will be replaced by (blockchain)technology.</p>

<p>In addition she offers certain applications where blockchain might be particularly well suited to disrupt such as Registries and Intellectual Property. In contrast, the problem of Information Sharing in law enforcement was mentioned as one needing a cultural solution, not a technological one.</p>

<p>Additional challenges with smart contracts:</p>

<ul>
<li>Liability for delays and/or errors

<ul>
<li>e.g.: wrong transaction recorded, delays affecting price points&#8230;</li>
</ul>
</li>
<li>Form of order in, say, land law</li>
<li>Jurisdiction - in which jurisdiction are you?</li>
<li>Data Protection:

<ul>
<li>The right to change data</li>
<li>The right to be forgotten</li>
<li>EU law seems to require these properties</li>
</ul>
</li>
<li>Regulation

<ul>
<li>Similar issues to when digital signatures started being used for contracts</li>
</ul>
</li>
</ul>


<p>All the above will also have implications to how lawyers are trained. I love that Lyria mentioned this as I&#8217;ve been casting what we do at <a href="http://www.modron.com/">MODRON</a> under a similar light: our human-centric approach looks to enhance what lawyers can do and over time this will invariably lead to new types of legal professionals.</p>

<p>Lyria&#8217;s closing remarks raised the question of wether blockchain even is the thing which will solve these problems. It&#8217;s too early to tell but it&#8217;s encouraging to see the legal profession as a whole taking the opportunity to re-evaluate how things are done.</p>

<p>What a day! So much to think about and so many great people to connect with. Look out for my <a href="http://www.leonardoborges.com/writings/2018/02/19/highlights-of-the-symposium-on-blockchain-and-distributed-ledger-technology-day-2/">summary of day 2</a>!</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Mission accomplished]]></title>
    <link href="http://www.leonardoborges.com/writings/2016/05/27/mission-accomplished/"/>
    <updated>2016-05-27T17:40:00+10:00</updated>
    <id>http://www.leonardoborges.com/writings/2016/05/27/mission-accomplished</id>
    <content type="html"><![CDATA[<p>A year and four months ago <a href="http://www.leonardoborges.com/writings/2014/12/27/staring-a-new-chapter/">I joined Atlassian</a> to pursue a very exciting opportunity: to deliver realtime collaborative editing to Confluence. I have talked about it last year at <a href="https://www.youtube.com/watch?v=3QR8meTrh5g&amp;index=16&amp;list=PLZdCLR02grLoBx0Y5ZrpdmLxc160PIwzQ">EuroClojure</a> and this year at <a href="http://qconsp.com/presentation/colabora%C3%A7%C3%A3o-em-tempo-real-com-clojure-e-clojurescript%E2%80%8B">QCon Brazil</a>. Even <a href="http://www.cognitect.com/">Cognitect</a> wrote <a href="http://blog.cognitect.com/blog/2015/7/21/atlassian-builds-realtime-collaboration-services-with-clojure">a few words about it</a>.</p>

<p>In a nutshell this involved writing Clojure code all day to build an awesome service we call Synchrony. Synchrony is capable of realtime data synchronisation - not just collaborative editing - and as such its applications are many! This has been proven again and again internally during the Atlassian ShipIt hackatons, and externally via <a href="https://enso.me/">Enso.me</a>, which is powered by Synchrony.</p>

<p>Throughout this journey I took on more responsibility and transitioned into the Development Team Lead role. This gave me the opportunity to help drive the product roadmap and vision within the company in addition to leading a very capable team. What an amazing learning experience it has been!</p>

<h3>Today</h3>

<p>Fast-forward to today and collaborative editing is finally being used by real customers as part of our early access program! It&#8217;s been a great journey and we have achieved a huge milestone!</p>

<p>For a more up to date look at Synchrony I highly recommend <a href="https://twitter.com/draftkraft">Haymo Meran</a>&#8217;s presentation at <a href="https://www.youtube.com/watch?v=EgCYd6ei7QI">this year&#8217;s AtlasCamp</a>. Haymo is a friend and one of the original founders of Wikidocs, the company where the technology behind Synchrony was originally developed.</p>

<p>I&#8217;m really proud of what we have accomplished and it&#8217;s a great moment to look for a change, again :)</p>

<h3>The future</h3>

<p>Yes, I am leaving Atlassian - last day is June 17th. I&#8217;ll be moving on to a new opportunity about which I&#8217;m really excited. But that is the subject of another blog post :)</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[The Clojure Sydney Meetup, four years in]]></title>
    <link href="http://www.leonardoborges.com/writings/2016/02/08/the-clojure-sydney-meetup-four-years-in/"/>
    <updated>2016-02-08T12:43:00+11:00</updated>
    <id>http://www.leonardoborges.com/writings/2016/02/08/the-clojure-sydney-meetup-four-years-in</id>
    <content type="html"><![CDATA[<h2>In the beginning</h2>

<p>Nearly 4 years ago I founded the Sydney Clojure User Group. I had been playing with Clojure for a little while
and started a small study group to learn more. Initially this group was private and held at the ThoughtWorks office.</p>

<p>After a couple of meetups and conversations I decided to open up the group to the public and then realised we weren&#8217;t the only ones interested in Clojure and, even more important, interested in meeting like-minded people, share experiences and help each other.</p>

<p>Back then none of us used Clojure for anything serious. Apart from Steve. Steve is a brave man and was building his new startup on top of Clojure. This was a great source of inspiration and learning for all of us. Steve hired Harry and Harry told us first hand what using Clojure for work could be like.</p>

<p>For all of us hobbyist Clojurists, this was a great start.</p>

<p>Since then we&#8217;ve held 37 meetups with an average of 22.25 attendees per meetup. These numbers won&#8217;t impress many people, no doubt about that. What is impressive however is how it&#8217;s changed over the years.</p>

<h2>Where are we at?</h2>

<p>Last December I sent out a survey and the result for one of the questions made me smile. The question was: <em>Do you currently use Clojure in your day job?</em></p>

<p><img class="center border" src="http://www.leonardoborges.com/writings/assets/images/working-with-clojure-survey-results.png" width="541" height="195"></p>

<p>This is absolutely amazing. It&#8217;s great to see how much the community has matured. Nearly 50% of meetup attendees currently work with Clojure in Sydney! I&#8217;m fortunate to say I can <a href="http://www.leonardoborges.com/writings/2014/12/27/staring-a-new-chapter/">include myself towards this milestone</a>!</p>

<h2>Going forward</h2>

<p>We&#8217;re now in 2016 and the group shows no signs of slowing down. In fact, we&#8217;re picking up the pace a little and starting the new year with a new venue: from now on we&#8217;ll be meeting at <a href="https://www.atlassian.com/">Atlassian</a>!</p>

<p>I would like to thank ThoughtWorks for all the support in starting and running the meetup. I wouldn&#8217;t have been able to do it if it wasn&#8217;t for them.</p>

<p>See you at the <a href="http://www.meetup.com/clj-syd/events/228691721/">next meetup</a>!</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[So Long 2015: Year Highlights]]></title>
    <link href="http://www.leonardoborges.com/writings/2016/01/04/so-long-2015-year-highlights/"/>
    <updated>2016-01-04T16:10:00+11:00</updated>
    <id>http://www.leonardoborges.com/writings/2016/01/04/so-long-2015-year-highlights</id>
    <content type="html"><![CDATA[<p>I should have published this post still in 2015 but as I was on holidays traveling around Australia I didn&#8217;t really have the time to do it. Better late than never, right? (also Linode - the provider hosting this site - has been under a <a href="https://news.ycombinator.com/item?id=10825425">severe DDoS attack</a> so the site just came back up today)</p>

<p>2015 has been a unusually quiet year in this blog. Thankfully that is because I have been extremely busy in many other areas. I&#8217;d like to take the opportunity and look back at 2015&#8217;s highlights.</p>

<h2>Atlassian</h2>

<p>I started working with Clojure full-time at Atlassian and it&#8217;s been a great experience so far. One of the big things we&#8217;re working on is bringing collaborative editing to Confluence. However we had the opportunity to put our service to the test with the launch of <a href="https://enso.me/">Project Enso</a>, an Atlassian Labs beta product. Check out <a href="https://vimeo.com/145662314">this teaser video</a> to get a quick feature run down.</p>

<h2>Book</h2>

<p>Back in March my book - <a href="http://www.amazon.com/Clojure-Reactive-Programming-Leonardo-Borges/dp/1783986662/">Clojure Reactive Programming</a> - was finally published. I have <a href="http://www.leonardoborges.com/writings/2015/03/27/clojure-reactive-programming-has-been-published/">written about it</a> before so I won&#8217;t dwell too much.</p>

<p>One last note on this though: you can <strong><a href="https://www.packtpub.com/web-development/clojure-reactive-programming">get a digital copy of my book for only $5</a></strong> as part of my publisher&#8217;s (Packt) Skill Up offer. <strong>Hurry, offer ends on the 8th of January!</strong></p>

<h2>Learning</h2>

<p>I tend to do a mix of reading, courses and deliberate practice when learning new things. In terms of reading, you can have a look at a few things I read in my <a href="https://www.goodreads.com/user/year_in_books/2015/15882031">Good Reads - 2015 in books</a> profile.</p>

<p>As for courses I decided to give <a href="https://en.wikipedia.org/wiki/Massive_open_online_course">MOOCs</a> a try and am really happy with the courses I picked:</p>

<p><strong>Coursera:</strong></p>

<ul>
<li><a href="https://www.coursera.org/learn/r-programming">R Programming</a> - This is a great source for anyone who&#8217;d like to get started with data processing. Even though R isn&#8217;t one of my favourite languages I cannot deny how easy it is to start making sense of your data.</li>
<li><a href="https://www.coursera.org/course/reactive">Principles of Reactive Programming</a> - As I have been into Reactive Programming myself for quite a while this gave me a bit of perspective on what Scala people mean by it. In particular the parts about Actor systems have been really valuable. Additionally the exercises are challenging and do drive the point home.</li>
<li><a href="https://www.coursera.org/learn/guitar">Introduction to Guitar</a> - I have been playing the guitar for a long time now but I have never had any proper education on the matter. I decided to give it a go with this MOOC by Berkeley and am pleased with the results. Especially since I can now put names to things I have done for years :)</li>
<li><a href="https://www.coursera.org/specializations/leading-teams">Leading People and Teams Specialisation</a> - Leadership is a broad area and having been under both good and bad leadership I don&#8217;t want to make some of the same mistakes I&#8217;ve seen in the past. As such, I am seeking advice from multiple different sources - such as some of the books in my GoodReads profile above - as well as this specialisation. It has 5 courses in total. I&#8217;m two in and have enjoyed it so far.</li>
</ul>


<p><strong>Edx:</strong></p>

<ul>
<li><a href="https://www.edx.org/course/introduction-functional-programming-delftx-fp101x-0">FP101x - Introduction to Functional Programming</a> - I picked this course for two reasons: (1) it&#8217;s taught by Erik Meijer and I&#8217;m fan and (2) as I don&#8217;t get to write Haskell all that often I use these courses as practice so my Haskell doesn&#8217;t get too rusty. This is an excellent course though if you&#8217;re looking to get into functional programming.</li>
</ul>


<p>I have also been dabbling more and more with <a href="http://elm-lang.org/">Elm</a> and <a href="http://www.purescript.org/">PureScript</a>. But more on that later. Maybe. :)</p>

<h2>Speaking</h2>

<p>Another good year in this area. Here&#8217;s what I spoke about this year:</p>

<ul>
<li><a href="http://www.slideshare.net/borgesleonardo/futures-e-abstrao-qcon-so-paulo-2015">Concurrent programming with Futures (in Portuguese)</a> - QCon, March 2015 - Sao Paulo, Brazil</li>
<li>Something about types being useful? - Sydney Clojure User Group, May 2015 - Sydney, Australia</li>
<li><a href="https://github.com/leonardoborges/elm-workshop-ylj2015">Workshop: From Zero to Pong Using Elm</a> - LambdaJam, May 2015 - Brisbane, Australia</li>
<li><a href="https://www.youtube.com/watch?v=3QR8meTrh5g">Realtime Collaboration with Clojure</a> - EuroClojure, June 2015 - Barcelona, Spain</li>
</ul>


<h2>Training</h2>

<p>In case you don&#8217;t know one of my hobbies is weightlifting. Names such as Clean, Clean &amp; Jerk, Snatches and Deadlifts are common in my day to day and 2015 has been a super year for my strength goals. In particular I&#8217;ve reached <strong>120kg</strong> for my Squats and <strong>140kg</strong> for my deadlifts which are my all time goals. For a bit of perspective I currently weigh <strong>69kg</strong>.</p>

<p>These numbers are <strong>good</strong> but they are not elite level. However what makes them impressive - for me anyway - is that four years ago I injured my lower back and was diagnosed with Level 1 spondylolisthesis. My doctor at the time told me I wouldn&#8217;t be able to lift anymore and I was extremely down for quite a while. It wasn&#8217;t until I switched doctors and dedicated 100% to having proper form that I was able to reach a whole new strength level.</p>

<p>These results make me extremely happy. In case you&#8217;re into this sort of thing you can check a <a href="https://www.youtube.com/watch?v=6OhYvZqPuTM">short video of my best lifts here</a>.</p>

<h3>That&#8217;s it</h3>

<p>It&#8217;s been a great year. Let 2016 be even better!</p>

<p><strong> Happy new year and keep on kicking ass :) </strong></p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Clojure Reactive Programming has been published]]></title>
    <link href="http://www.leonardoborges.com/writings/2015/03/27/clojure-reactive-programming-has-been-published/"/>
    <updated>2015-03-27T15:10:00+11:00</updated>
    <id>http://www.leonardoborges.com/writings/2015/03/27/clojure-reactive-programming-has-been-published</id>
    <content type="html"><![CDATA[<p>I&#8217;m extremely happy to let everyone know my book, <a href="https://www.packtpub.com/web-development/clojure-reactive-programming">Clojure Reactive Programming</a>, has finally been published!</p>

<p>You can get it at the <a href="https://www.packtpub.com/web-development/clojure-reactive-programming">publisher&#8217;s website</a> or on <a href="http://www.amazon.com/Clojure-Reactive-Programming-Leonardo-Borges/dp/1783986662/ref=sr_1_1?ie=UTF8&amp;qid=1427205216&amp;sr=8-1&amp;keywords=Clojure+Reactive+Programming">Amazon</a>. I had a great time writing it and I truly hope you find it useful!</p>

<p>I&#8217;ve met a few authors here and there and I heard more than once that a book is never really finished. I now know what they mean.</p>

<p>The book doesn&#8217;t cover everything I wanted to write about due to time and space limitations. Having said that, now that the book is out I do plan to expand on a few things using this blog.</p>

<p>Stay tuned!</p>

<p>Thanks to everyone who gave me feedback on early drafts of the book! :)</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[So Long 2014: Year Highlights]]></title>
    <link href="http://www.leonardoborges.com/writings/2014/12/31/so-long-2014-year-highlights/"/>
    <updated>2014-12-31T17:23:00+11:00</updated>
    <id>http://www.leonardoborges.com/writings/2014/12/31/so-long-2014-year-highlights</id>
    <content type="html"><![CDATA[<p>Another year is about to end and as it is tradition in this blog it&#8217;s time to look at the year&#8217;s highlights!</p>

<h2>Functional Programming</h2>

<p>I have been advocating Functional Programming and Clojure for a while now and 2014 has been good to me. It was the first year where I was paid to work almost entirely in functional languages. Most of it was in Scala and a little less in Clojure.</p>

<p>I did have a short 8-week gig in Javascript but even then I tried to make it as functional as possible - e.g.: making use of Reactive Programming techniques (<a href="http://reactivex.io/">Rx-style</a>)</p>

<p>I&#8217;m also thankful for the opportunity to have organised the first Sydney edition of <a href="http://www.clojurebridge.org/">ClojureBridge</a>, a workshop aimed at increasing diversity in the Clojure community by delivering free workshops for women. You can read more about what we did on the day <a href="http://www.leonardoborges.com/writings/2014/12/22/clojurebridge-sydney/">here</a>.</p>

<p>Another bit of exciting news is that I&#8217;ve decided to quit <a href="http://www.thoughtworks.com/">ThoughtWorks</a>. Starting in January I&#8217;ll be doing Clojure full-time at <a href="https://www.atlassian.com/">Atlassian</a>. I&#8217;ve written about it <a href="http://www.leonardoborges.com/writings/2014/12/27/staring-a-new-chapter/">here</a>.</p>

<p>I&#8217;m looking forward to a very functional year!</p>

<h2>Writing</h2>

<p>Early this year I was invited by <a href="https://www.packtpub.com/">Packt Publishing</a> to write a book on Reactive Programming in Clojure. I&#8217;ve <a href="http://www.leonardoborges.com/writings/2014/07/20/euroclojure-2014-and-clojure-reactive-programming/">announced it before in this blog</a> but it is definitely a highlight of this year.</p>

<p>The <a href="https://twitter.com/CljReactiveProg">book</a> has been a while in the making and I had no idea of the amount of effort involved in writing one. I&#8217;m mostly content-complete now but working through a number of useful bits of feedback I got from early reviewers. I expect to be done with these in January and then the book should go into production.</p>

<p>I&#8217;ll keep everybody posted :)</p>

<h2>Reading</h2>

<p>Most of what I read this year - with the little free time I had - were academic papers and I would like to highlight a few that I particularly enjoyed:</p>

<ul>
<li><a href="http://homepages.inf.ed.ac.uk/wadler/papers/marktoberdorf/baastad.pdf">Monads for functional programming</a></li>
<li><a href="http://homepages.inf.ed.ac.uk/wadler/papers/propositions-as-types/propositions-as-types.pdf">Propositions as Types</a></li>
<li><a href="http://ttic.uchicago.edu/~dreyer/course/papers/wadler.pdf">Theorems for free!</a></li>
<li><a href="http://www.cis.upenn.edu/~byorgey/pub/monoid-pearl.pdf">Monoids: Theme and Variations (Functional Pearl)</a></li>
<li><a href="http://conal.net/papers/icfp97/icfp97.pdf">Functional Reactive Animation</a></li>
<li><a href="http://conal.net/papers/push-pull-frp/push-pull-frp.pdf">Push-pull Functional Reactive Programming</a></li>
<li><a href="http://elm-lang.org/papers/concurrent-frp.pdf">Elm: Concurrent FRP for Functional GUIs</a></li>
</ul>


<p>Additionally I can&#8217;t help myself and recommend that you read <a href="http://www.manning.com/bjarnason/">Functional Programming in Scala</a> if you have any interest in FP and  Scala. <a href="https://twitter.com/runarorama">Rúnar</a> and <a href="https://twitter.com/pchiusano">Paul</a> have done a great job and put a lot of good advice in this book.</p>

<h2>Speaking</h2>

<p>Between work and writing the book I&#8217;m happy I managed to speak this much:</p>

<ul>
<li><a href="http://www.slideshare.net/borgesleonardo/the-algebra-of-library-design">The Algebra of Library Design</a> - Sydney Clojure User Group, October 2014 - Sydney, Australia</li>
<li><a href="http://www.slideshare.net/borgesleonardo/high-performance-web-apps-in-om-react-and-clojurescript">High Performance web apps in Om, React and ClojureScript</a> - LambdaJam, May 2014 - Brisbane, Australia</li>
<li><a href="http://www.slideshare.net/borgesleonardo/functional-reactive-programming-compositional-event-systems">Taming asynchronous workflows with Functional Reactive Programming</a> - EuroClojure, April 2014 - Krakow, Poland</li>
<li><a href="http://www.slideshare.net/borgesleonardo/programao-functional-reativa-lidando-com-cdigo-assncrono">Taming asynchronous workflows with Functional Reactive Programming (portuguese)</a> - QCon, March 2014 - São Paulo, Brazil</li>
<li><a href="http://www.slideshare.net/borgesleonardo/monads-in-clojure">Monads in Clojure</a> - Sydney Clojure User Group, February 2014 - Sydney, Australia</li>
</ul>


<h2>Content</h2>

<p>Here&#8217;s the Top 5 posts from this blog in 2014:</p>

<ul>
<li><p><a href="http://www.leonardoborges.com/writings/2014/06/17/functional-composition-with-monads-kleisli-functors/">Functional Composition With Monads, Kleislis and Functors</a> - With learning Scala and Scalaz, I&#8217;ve come to learn new ways of composing programs. This short post is a brief look on one of them, Kleisli arrows.</p></li>
<li><p><a href="http://www.leonardoborges.com/writings/2012/11/30/monads-in-small-bites-part-i-functors/">Monads in Small Bites - Part I - Functors</a> - I&#8217;m pleasantly surprised this post ranked this high. It&#8217;s from a series about Monads and I&#8217;m glad it has been helping people understand the general idea behind them.</p></li>
<li><p><a href="http://www.leonardoborges.com/writings/2013/03/25/clojure-and-why-calculating-is-better-than-scheming/">Clojure and &#8216;Why Calculating Is Better Than Scheming&#8217;</a> - A fresh look at this popular paper by <a href="http://homepages.inf.ed.ac.uk/wadler/">Philip Wadler</a> from a Clojure perspective.</p></li>
<li><p><a href="http://www.leonardoborges.com/writings/2013/07/06/clojure-core-dot-async-lisp-advantage/">Clojure, core.async and the Lisp Advantage</a> - <a href="https://github.com/clojure/core.async">core.async</a> is still all the rage in the Clojure community and this post from 2013 gives a brief introduction as to what it&#8217;s all about.</p></li>
<li><p><a href="http://www.leonardoborges.com/writings/2013/07/15/purely-functional-data-structures-in-clojure-red-black-trees/">Purely Functional Data Structures in Clojure: Red-Black Trees</a> - This post is from a series - though I only have two posts at the moment - about <a href="http://www.leonardoborges.com/writings/2013/07/15/purely-functional-data-structures-in-clojure-red-black-trees/">persistent data structures</a> in Clojure.</p></li>
</ul>


<p>If this tells me anything it&#8217;s that I need to blog more!</p>

<p><strong>Happy new year! Here&#8217;s to an amazing 2015!</strong></p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[A new chapter begins]]></title>
    <link href="http://www.leonardoborges.com/writings/2014/12/27/staring-a-new-chapter/"/>
    <updated>2014-12-27T14:04:00+11:00</updated>
    <id>http://www.leonardoborges.com/writings/2014/12/27/staring-a-new-chapter</id>
    <content type="html"><![CDATA[<h2>Thanks, ThoughtWorks</h2>

<p>After 4 years and 8 months I have made the hard decision to change jobs: Friday, 19th of December, was my last day at <a href="http://www.thoughtworks.com/">ThoughtWorks</a>.</p>

<p>In this time I have made many friends and have grown a lot as a professional. Consulting presents so many new challenges that often times the technical problems you are trying to solve are the easiest part of the whole project. I am thankful for that.</p>

<p>I am also thankful for the opportunities I&#8217;ve had.</p>

<p>A few years ago I started dedicating most, if not all, my free time to learning Functional Programming. ThoughtWorks supported and encouraged my endeavours in doing so and that is a big part of enabling me to create and grow the Clojure community in Sydney through <a href="http://www.meetup.com/clj-syd/">#cljsyd</a>.</p>

<p>Additionally ThoughtWorks has also recognised the importance of Functional Programming in modern software development and allowed me to work almost exclusively in FP languages such as Scala and Clojure for the past year and a half.</p>

<p>About three years ago I <a href="http://www.leonardoborges.com/writings/2011/04/25/one-year-of-thoughtworks-a-retrospective/">wrote a post</a> detailing my first year at ThoughtWorks. Much of it is still true so there is no point in repeating it. ThoughtWorks remains a great place to work.</p>

<h2>What&#8217;s next?</h2>

<p>My next challenge will be at <a href="https://www.atlassian.com/">Atlassian</a>, an Australian Software company best known for being the creators of <a href="https://www.atlassian.com/software/jira">JIRA</a>, <a href="https://www.atlassian.com/software/confluence">Confluence</a>, <a href="https://www.atlassian.com/software/stash">Stash</a> and <a href="https://www.atlassian.com/software">many other products</a>.</p>

<p>Over the years I have met several Atlassians - that&#8217;s what they call themselves - and many of them have been heavily involved in the Functional Programming community. So when I was contacted by a friend saying Atlassian needed an experienced Clojure developer I was intrigued.</p>

<p>A few months ago Atlassian acquired <a href="https://wikidocs.com/">WikiDocs</a>, a startup whose business is to build technology that enables <a href="http://en.wikipedia.org/wiki/Collaborative_real-time_editor">real-time collaborative editing</a>. It turns out that this product is developed in Clojure and Clojurescript. That&#8217;s essentially what I&#8217;m going to be working on - I&#8217;ll write more about it once I settle in.</p>

<p>When I started learning Clojure over 3 years ago, little did I know that I&#8217;d be working full-time with the language so soon and I am super excited about it. It&#8217;s great to see that much effort paying off like this.</p>

<p>My start date at Atlassian is the 5th of January and I am looking forward to meeting a bunch of great people and making new friends.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[ClojureBridge Sydney, vol. I]]></title>
    <link href="http://www.leonardoborges.com/writings/2014/12/22/clojurebridge-sydney/"/>
    <updated>2014-12-22T18:18:00+11:00</updated>
    <id>http://www.leonardoborges.com/writings/2014/12/22/clojurebridge-sydney</id>
    <content type="html"><![CDATA[<p>This past weekend - 19-20th of December - a group of highly motivated individuals gave up their Friday night and their entire Saturday simply to learn how to code in Clojure!</p>

<p>It was the first ever <a href="http://www.clojurebridge.org/">ClojureBridge</a> edition in Sydney and if you haven&#8217;t heard of it before, its goal is to increase diversity in the Clojure community. It does so by offering free workshops targeted at beginners.</p>

<p>You should totally read <a href="http://juliangamble.com/blog/2014/12/20/why-clojurebridge-is-awesome/">Julian Gamble&#8217;s post</a> about the event for details. He&#8217;s was one of the volunteers on the day and has done a great job of describing his experiences.</p>

<p>I just wanted to take the opportunity to highlight and thank all volunteers who also gave up their time for free to help and coach 24 people eager to learn Clojure. They are: Alexandra Luca, <a href="https://twitter.com/claudionatoli">Claudio Natoli</a>, <a href="https://twitter.com/juliansgamble">Julian Gamble</a>, Marcin Nikliborc, <a href="https://twitter.com/novemberkilo">Navin K</a>, Scott Robinson, <a href="https://twitter.com/svetixbot">Svetlana Filimonova</a> &amp; <a href="https://twitter.com/vineethvarghese">Vineeth Varghese</a>.</p>

<p><img class="left border" src="http://www.leonardoborges.com/writings/assets/images/posts/clojurebridge_cupcakes.jpg" width="231" height="346"></p>

<p>Without you, the event would not have happened!</p>

<p>A big thank you to our main sponsor - <a href="http://www.thoughtworks.com/">ThoughtWorks</a> - who provided the venue, food, drinks and, of course, Clojure cupcakes.</p>

<p>Another big thank you to <a href="https://www.atlassian.com/">Atlassian</a> who gave some cool gifts to our attendees! (Sunnies and hats!).</p>

<p>Last but not least, a <strong>huge</strong> thank you to all attendees who were absolutely stellar and made the event an amazing experience!</p>

<p>I had a blast organising and running the event and hopefully we will have a new edition soon! I hope it ignited the desire to learn even more about Clojure and Functional Programming!</p>

<p>Happy holidays!</p>

<p>λ♥</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[EuroClojure 2014 and announcing my book]]></title>
    <link href="http://www.leonardoborges.com/writings/2014/07/20/euroclojure-2014-and-clojure-reactive-programming/"/>
    <updated>2014-07-20T15:50:00+10:00</updated>
    <id>http://www.leonardoborges.com/writings/2014/07/20/euroclojure-2014-and-clojure-reactive-programming</id>
    <content type="html"><![CDATA[<p>A bit late for a EuroClojure 2014 post but I suppose &#8220;better late than never&#8221; applies here.</p>

<p>The best part of every conference is the networking. Meeting new and interesting people is priceless and I did plenty of that - mostly over Polish beer and food. Polish beer isn&#8217;t the greatest but if you must have it, better stick to Żywiec. It&#8217;s widely available and was the one which didn&#8217;t give me a headache :) - I&#8217;ve been told there are amazing microbreweries though I didn&#8217;t get a chance to try any while in Krákow.</p>

<p>The food on the other hand was excellent every single time. But I digress.</p>

<p>There&#8217;s no point in me describing the talks I watched as someone else has already done a
much better job of it: it&#8217;s all <a href="https://gist.github.com/philandstuff/299cda371c7e74b03f18">in this gist</a> by <a href="https://twitter.com/philandstuff">Philip Potter</a>.</p>

<p>I gave a talk titled <em>Taming Asynchronous Workflows with Functional Reactive Programming</em>. You can check out the <a href="http://www.slideshare.net/borgesleonardo/functional-reactive-programming-compositional-event-systems">slides here</a>. The video will be available in  <a href="https://vimeo.com/100688924">this link</a> soon. I&#8217;ve received a lot of great feedback on it both at the event and afterwards through different channels. I&#8217;m really happy with how it turned out.</p>

<p>In this talk I mentioned publicly for the first time* that I am working on a book called <strong>Clojure Reactive Programming</strong> to be published by <a href="http://www.packtpub.com/">PacktPub</a>. It seemed appropriate to announce it here as well.</p>

<p>As of yet there&#8217;s no set date but I&#8217;m spending most of my free time working on it. I&#8217;d say I have 55% of it done. Feel free to ping me directly if you&#8217;d like to know more. Alternatively you might want to follow the <a href="https://twitter.com/CljReactiveProg">twitter account I created for the book</a>.</p>

<p>There&#8217;s not much there yet but I&#8217;ll try and post book updates somewhat regularly.</p>

<p>See you around.</p>

<p>* <em>Strictly not true as I&#8217;ve announced it before but this was the first time for a wider Clojure audience and the first time captured on camera so there&#8217;s no denying now ;)</em></p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Functional composition with Monads, Kleislis and Functors]]></title>
    <link href="http://www.leonardoborges.com/writings/2014/06/17/functional-composition-with-monads-kleisli-functors/"/>
    <updated>2014-06-17T11:00:00+10:00</updated>
    <id>http://www.leonardoborges.com/writings/2014/06/17/functional-composition-with-monads-kleisli-functors</id>
    <content type="html"><![CDATA[<p>I&#8217;ve been learning Scala for my current client project and I find writing to be a great tool to test my understanding of any given topic. This means there might be a few Scala posts coming up soon as I keep learning interesting things.</p>

<p>Today I&#8217;ll be exploring a few different ways in which you can compose programs. I&#8217;ll be using <a href="https://github.com/scalaz/scalaz">Scalaz</a> in this post.</p>

<p>The examples that follow all deal with Vehicles - more specifically makes and parts:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='scala'><span class='line'>  <span class="k">import</span> <span class="nn">scalaz._</span><span class="o">,</span> <span class="nc">Scalaz</span><span class="o">.</span><span class="k">_</span>
</span><span class='line'>  <span class="k">import</span> <span class="nn">scalaz.Kleisli._</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">case</span> <span class="k">class</span> <span class="nc">Make</span><span class="o">(</span><span class="n">id</span><span class="k">:</span> <span class="kt">Int</span><span class="o">,</span> <span class="n">name</span><span class="k">:</span> <span class="kt">String</span><span class="o">)</span>
</span><span class='line'>  <span class="k">case</span> <span class="k">class</span> <span class="nc">Part</span><span class="o">(</span><span class="n">id</span><span class="k">:</span> <span class="kt">Int</span><span class="o">,</span> <span class="n">name</span><span class="k">:</span> <span class="kt">String</span><span class="o">)</span>
</span></code></pre></td></tr></table></div></figure>


<p>Next we have a couple of functions which interact with these case classes:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='scala'><span class='line'>  <span class="k">val</span> <span class="n">make</span><span class="k">:</span> <span class="o">(</span><span class="kt">Int</span><span class="o">)</span> <span class="o">=&gt;</span> <span class="nc">Make</span> <span class="k">=</span> <span class="o">(</span><span class="n">_</span><span class="o">)</span> <span class="k">=&gt;</span> <span class="nc">Make</span><span class="o">(</span><span class="mi">1</span><span class="o">,</span> <span class="s">&quot;Suzuki&quot;</span><span class="o">)</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">val</span> <span class="n">parts</span><span class="k">:</span> <span class="kt">Make</span> <span class="o">=&gt;</span> <span class="nc">List</span><span class="o">[</span><span class="kt">Part</span><span class="o">]</span> <span class="k">=</span> <span class="o">{</span>
</span><span class='line'>    <span class="k">case</span> <span class="nc">Make</span><span class="o">(</span><span class="mi">1</span><span class="o">,</span> <span class="n">_</span><span class="o">)</span> <span class="k">=&gt;</span> <span class="nc">List</span><span class="o">(</span><span class="nc">Part</span><span class="o">(</span><span class="mi">1</span><span class="o">,</span> <span class="s">&quot;Gear Box&quot;</span><span class="o">),</span> <span class="nc">Part</span><span class="o">(</span><span class="mi">2</span><span class="o">,</span> <span class="s">&quot;Clutch cable&quot;</span><span class="o">))</span>
</span><span class='line'>  <span class="o">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>So we have a function from <code>Int</code> to <code>Make</code> and then a function from <code>Make</code> to <code>List[Part]</code>. From set theory we know this implies we must have a function from <code>Int</code> to <code>List[Part]</code>. This is nothing more than simple function composition:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
</pre></td><td class='code'><pre><code class='scala'><span class='line'>  <span class="k">val</span> <span class="n">f</span> <span class="k">=</span> <span class="n">parts</span> <span class="n">compose</span> <span class="n">make</span>
</span><span class='line'>  <span class="n">f</span><span class="o">(</span><span class="mi">1</span><span class="o">)</span>
</span><span class='line'>  <span class="c1">// List[Part] = List(Part(1,Gear Box), Part(2,Clutch cable))</span>
</span><span class='line'>
</span><span class='line'> <span class="c1">// alternatively you can use &#39;andThen&#39; which works like compose, but with the arguments flipped:</span>
</span><span class='line'> <span class="k">val</span> <span class="n">g</span> <span class="k">=</span> <span class="n">make</span> <span class="n">andThen</span> <span class="n">parts</span>
</span><span class='line'> <span class="n">g</span><span class="o">(</span><span class="mi">1</span><span class="o">)</span>
</span><span class='line'> <span class="c1">// List[Part] = List(Part(1,Gear Box), Part(2,Clutch cable))</span>
</span></code></pre></td></tr></table></div></figure>


<p>Pretty boring stuff.</p>

<p>A more realistic example accounts for failure in our functions. One way we can encode this is using the <code>Option</code> data type:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='scala'><span class='line'><span class="k">val</span> <span class="n">make</span>  <span class="k">=</span> <span class="o">(</span><span class="n">x</span><span class="k">:</span> <span class="kt">Int</span><span class="o">)</span> <span class="k">=&gt;</span> <span class="o">(</span><span class="n">x</span> <span class="o">==</span> <span class="mi">1</span><span class="o">).</span><span class="n">option</span><span class="o">(</span><span class="nc">Make</span><span class="o">(</span><span class="mi">1</span><span class="o">,</span> <span class="s">&quot;Suzuki&quot;</span><span class="o">))</span>
</span><span class='line'>
</span><span class='line'><span class="k">val</span> <span class="n">parts</span> <span class="k">=</span> <span class="o">(</span><span class="n">x</span><span class="k">:</span> <span class="kt">Make</span><span class="o">)</span> <span class="k">=&gt;</span>
</span><span class='line'>  <span class="o">(</span><span class="n">x</span><span class="o">.</span><span class="n">id</span> <span class="o">==</span> <span class="mi">1</span><span class="o">).</span><span class="n">option</span><span class="o">(</span><span class="nc">NonEmptyList</span><span class="o">(</span><span class="nc">Part</span><span class="o">(</span><span class="mi">1</span><span class="o">,</span> <span class="s">&quot;Gear Box&quot;</span><span class="o">),</span> <span class="nc">Part</span><span class="o">(</span><span class="mi">2</span><span class="o">,</span> <span class="s">&quot;Clutch cable&quot;</span><span class="o">)))</span>
</span></code></pre></td></tr></table></div></figure>


<p>Now we have a function <code>make: Int =&gt; Option[Make]</code> and a function <code>parts: Make =&gt; Option[NonEmptyList[Part]]</code>. Based on our first example we should have a way to create a function from <code>Int</code> to <code>Option[NonEmptyList[Part]]</code>. This isn&#8217;t immediately obvious however.</p>

<p>While <code>make</code> does return a <code>Make</code>, it is wrapped inside an <code>Option</code> so we need to account for a possible failure. This leads to our first attempt:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
</pre></td><td class='code'><pre><code class='scala'><span class='line'>  <span class="k">val</span> <span class="n">f</span><span class="k">:</span> <span class="kt">Option</span><span class="o">[</span><span class="kt">Make</span><span class="o">]</span> <span class="k">=&gt;</span> <span class="nc">Option</span><span class="o">[</span><span class="kt">NonEmptyList</span><span class="o">[</span><span class="kt">Part</span><span class="o">]]</span> <span class="k">=</span> <span class="o">{</span>
</span><span class='line'>    <span class="k">case</span> <span class="nc">Some</span><span class="o">(</span><span class="n">m</span><span class="o">)</span> <span class="k">=&gt;</span> <span class="n">parts</span><span class="o">(</span><span class="n">m</span><span class="o">)</span>
</span><span class='line'>    <span class="k">case</span> <span class="k">_</span> <span class="k">=&gt;</span> <span class="nc">None</span>
</span><span class='line'>  <span class="o">}</span>
</span><span class='line'>  <span class="k">val</span> <span class="n">g</span> <span class="k">=</span> <span class="n">f</span> <span class="n">compose</span> <span class="n">make</span>
</span><span class='line'>  <span class="n">g</span><span class="o">(</span><span class="mi">1</span><span class="o">)</span> <span class="c1">// Some(NonEmptyList(Part(1,Gear Box), Part(2,Clutch cable)))  </span>
</span></code></pre></td></tr></table></div></figure>


<p>While this works, we had to manually create the plumbing between the two functions.  You can imagine that with different return and input types, this plubming would have to be rewritten over and over.</p>

<p>All the function <code>f</code> above is doing is serving as an <em>adapter</em> for <code>parts</code>. It turns out there is a couple of ways in which this pattern can be generalised.</p>

<h2>Monadic bind</h2>

<p><code>Option</code> is a <a href="http://www.leonardoborges.com/writings/2012/12/08/monads-in-small-bites-part-iv-monads/">monad</a> so we can define <em>f</em> using a for comprehension:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='scala'><span class='line'>  <span class="k">val</span> <span class="n">f</span> <span class="k">=</span> <span class="o">(</span><span class="n">x</span><span class="k">:</span> <span class="kt">Int</span><span class="o">)</span> <span class="k">=&gt;</span> <span class="k">for</span> <span class="o">{</span>
</span><span class='line'>    <span class="n">m</span> <span class="k">&lt;-</span> <span class="n">make</span><span class="o">(</span><span class="n">x</span><span class="o">)</span>
</span><span class='line'>    <span class="n">p</span> <span class="k">&lt;-</span> <span class="n">parts</span><span class="o">(</span><span class="n">m</span><span class="o">)</span>
</span><span class='line'>  <span class="o">}</span> <span class="k">yield</span> <span class="n">p</span>
</span><span class='line'>  <span class="n">f</span><span class="o">(</span><span class="mi">1</span><span class="o">)</span> <span class="c1">// Some(NonEmptyList(Part(1,Gear Box), Part(2,Clutch cable)))</span>
</span></code></pre></td></tr></table></div></figure>


<p>Which is simply syntactic sugar for:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
</pre></td><td class='code'><pre><code class='scala'><span class='line'><span class="k">val</span> <span class="n">g</span> <span class="k">=</span> <span class="n">make</span><span class="o">(</span><span class="n">_:</span><span class="nc">Int</span><span class="o">)</span> <span class="n">flatMap</span> <span class="o">(</span><span class="n">m</span> <span class="k">=&gt;</span>
</span><span class='line'>    <span class="n">parts</span><span class="o">(</span><span class="n">m</span><span class="o">).</span><span class="n">map</span><span class="o">(</span><span class="n">p</span> <span class="k">=&gt;</span> <span class="n">p</span><span class="o">))</span>
</span><span class='line'><span class="n">g</span><span class="o">(</span><span class="mi">1</span><span class="o">)</span>
</span><span class='line'><span class="c1">// Some(NonEmptyList(Part(1,Gear Box), Part(2,Clutch cable)))</span>
</span><span class='line'>
</span><span class='line'><span class="c1">// you can also use the symbolic alias for &#39;bind&#39;, which makes it a lot nicer</span>
</span><span class='line'><span class="k">val</span> <span class="n">h</span> <span class="k">=</span> <span class="n">make</span><span class="o">(</span><span class="n">_:</span><span class="nc">Int</span><span class="o">)</span> <span class="o">&gt;&gt;=</span> <span class="n">parts</span>
</span><span class='line'><span class="n">h</span><span class="o">(</span><span class="mi">1</span><span class="o">)</span>
</span><span class='line'><span class="c1">// Some(NonEmptyList(Part(1,Gear Box), Part(2,Clutch cable)))</span>
</span></code></pre></td></tr></table></div></figure>


<p>The reason this is better is that <code>make</code> and <code>parts</code> could operate under a different monad but the client code would not need to change. In the example below, we&#8217;re operating under the <code>List</code> monad:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
</pre></td><td class='code'><pre><code class='scala'><span class='line'><span class="k">val</span> <span class="n">words</span><span class="k">:</span> <span class="o">(</span><span class="kt">String</span><span class="o">)</span> <span class="o">=&gt;</span> <span class="nc">List</span><span class="o">[</span><span class="kt">String</span><span class="o">]</span> <span class="k">=</span> <span class="n">_</span><span class="o">.</span><span class="n">split</span><span class="o">(</span><span class="s">&quot;&quot;&quot;\s&quot;&quot;&quot;</span><span class="o">).</span><span class="n">toList</span>
</span><span class='line'><span class="k">val</span> <span class="n">chars</span><span class="k">:</span> <span class="kt">String</span> <span class="o">=&gt;</span> <span class="nc">List</span><span class="o">[</span><span class="kt">Char</span><span class="o">]</span> <span class="k">=</span> <span class="n">_</span><span class="o">.</span><span class="n">toList</span>
</span><span class='line'>
</span><span class='line'><span class="k">val</span> <span class="n">f</span> <span class="k">=</span> <span class="o">(</span><span class="n">phrase</span><span class="k">:</span> <span class="kt">String</span><span class="o">)</span> <span class="k">=&gt;</span> <span class="k">for</span> <span class="o">{</span>
</span><span class='line'>  <span class="n">m</span> <span class="k">&lt;-</span> <span class="n">words</span><span class="o">(</span><span class="n">phrase</span><span class="o">)</span>
</span><span class='line'>  <span class="n">p</span> <span class="k">&lt;-</span> <span class="n">chars</span><span class="o">(</span><span class="n">m</span><span class="o">)</span>
</span><span class='line'><span class="o">}</span> <span class="k">yield</span> <span class="n">p</span>
</span><span class='line'>
</span><span class='line'><span class="n">f</span><span class="o">(</span><span class="s">&quot;Motorcycles are fun to ride!&quot;</span><span class="o">)</span>
</span><span class='line'><span class="c1">// List(M, o, t, o, r, c, y, c, l, e, s, a, r, e, f, u, n, t, o, r, i, d, e, !)</span>
</span><span class='line'>
</span><span class='line'><span class="c1">// or even:</span>
</span><span class='line'><span class="k">val</span> <span class="n">g</span> <span class="k">=</span> <span class="n">words</span><span class="o">(</span><span class="n">_:</span><span class="nc">String</span><span class="o">)</span> <span class="n">flatMap</span> <span class="o">(</span><span class="n">w</span> <span class="k">=&gt;</span>
</span><span class='line'>    <span class="n">chars</span><span class="o">(</span><span class="n">w</span><span class="o">).</span><span class="n">map</span><span class="o">(</span><span class="n">c</span> <span class="k">=&gt;</span> <span class="n">c</span><span class="o">))</span>
</span><span class='line'><span class="n">g</span><span class="o">(</span><span class="s">&quot;Motorcycles are fun to ride!&quot;</span><span class="o">)</span>
</span><span class='line'><span class="c1">// List(M, o, t, o, r, c, y, c, l, e, s, a, r, e, f, u, n, t, o, r, i, d, e, !)</span>
</span></code></pre></td></tr></table></div></figure>


<p>We used the exact same <em>for</em> comprehension syntax to compose these operations. This works because both <code>Option</code> and <code>List</code> are monads.</p>

<p>Notwithstanding, this still feels like unnecessary plumbing. All we are doing with the <em>for</em> comprehenstion / <code>flatMap</code> is extracting the values from their respective monads to simply put them back in. It would be nice if we could simply do something like <code>make compose parts</code> as we did in our first example.</p>

<h2>Kleisli Arrows</h2>

<p>A <a href="http://www.haskell.org/haskellwiki/Arrow_tutorial#Kleisli_Arrows">Kleisli arrow</a> is simply a <em>wrapper</em> for a function of type <code>A =&gt; F[B]</code>. This is the same type of the second argument to the monadic <em>bind</em> as <a href="https://github.com/scalaz/scalaz/blob/scalaz-seven/core/src/main/scala/scalaz/Bind.scala#L16">defined</a> in Scalaz:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='scala'><span class='line'><span class="n">bind</span><span class="o">[</span><span class="kt">A</span>, <span class="kt">B</span><span class="o">](</span><span class="n">fa</span><span class="k">:</span> <span class="kt">F</span><span class="o">[</span><span class="kt">A</span><span class="o">])(</span><span class="n">f</span><span class="k">:</span> <span class="kt">A</span> <span class="o">=&gt;</span> <span class="n">F</span><span class="o">[</span><span class="kt">B</span><span class="o">])</span><span class="k">:</span> <span class="kt">F</span><span class="o">[</span><span class="kt">B</span><span class="o">]</span>
</span></code></pre></td></tr></table></div></figure>


<p>By creating a Kleisli arrow from a function, we are given a function that knows how to extract the value from a Monad <code>F</code> and feed it into the underlying function, much like bind does, but without actually having to do any binding yourself.</p>

<p>To use a concrete example, let&#8217;s create a kleisli arrow from our <code>parts</code> function:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='scala'><span class='line'><span class="n">kleisli</span><span class="o">(</span><span class="n">parts</span><span class="o">)</span>
</span><span class='line'><span class="c1">// scalaz.Kleisli[Option,Make,scalaz.NonEmptyList[Part]]</span>
</span></code></pre></td></tr></table></div></figure>


<p>You can read this type as being a function which knows how to get a value of type <code>Make</code> from the <code>Option</code> monad and will ultimately return an <code>Option[NonEmptyList[Part]]</code>. Now you might be asking, why would we want to wrap our functions in a kleisli arrow?</p>

<p>By doing so, you have access to a number of useful functions defined in the Kleisli trait, one of which is <code>&lt;==&lt;</code> (aliased as <code>composeK</code>):</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='scala'><span class='line'>  <span class="k">val</span> <span class="n">f</span> <span class="k">=</span> <span class="n">kleisli</span><span class="o">(</span><span class="n">parts</span><span class="o">)</span> <span class="o">&lt;==&lt;</span> <span class="n">make</span>
</span><span class='line'>  <span class="c1">// same as   kleisli(parts) composeK make</span>
</span><span class='line'>
</span><span class='line'>  <span class="n">f</span><span class="o">(</span><span class="mi">1</span><span class="o">)</span> <span class="c1">// Some(NonEmptyList(Part(1,Gear Box), Part(2,Clutch cable)))</span>
</span></code></pre></td></tr></table></div></figure>


<p>This gives us the same result as the version using the <em>for</em> comprehension but with less work and with code that looks similar to simple function composition.</p>

<h2>Not there yet</h2>

<p>One thing that was bugging me is the return type for <code>parts</code> above:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='scala'><span class='line'><span class="nc">Make</span> <span class="k">=&gt;</span> <span class="nc">Option</span><span class="o">[</span><span class="kt">NonEmptyList</span><span class="o">[</span><span class="kt">Part</span><span class="o">]]</span>
</span></code></pre></td></tr></table></div></figure>


<p>Sure this works but since lists already represent non-deterministic results, one can make the point that the Option type there is reduntant since, for this example, we can treat both <code>None</code> and the empty List as the <em>absence of result</em>. Let&#8217;s update the code:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
</pre></td><td class='code'><pre><code class='scala'><span class='line'><span class="k">val</span> <span class="n">make</span>  <span class="k">=</span> <span class="o">(</span><span class="n">x</span><span class="k">:</span> <span class="kt">Int</span><span class="o">)</span> <span class="k">=&gt;</span> <span class="o">(</span><span class="n">x</span> <span class="o">==</span> <span class="mi">1</span><span class="o">).</span><span class="n">option</span><span class="o">(</span><span class="nc">Make</span><span class="o">(</span><span class="mi">1</span><span class="o">,</span> <span class="s">&quot;Suzuki&quot;</span><span class="o">))</span>
</span><span class='line'>
</span><span class='line'><span class="k">val</span> <span class="n">parts</span><span class="k">:</span> <span class="kt">Make</span> <span class="o">=&gt;</span> <span class="nc">List</span><span class="o">[</span><span class="kt">Part</span><span class="o">]</span> <span class="k">=</span> <span class="o">{</span>
</span><span class='line'>  <span class="k">case</span> <span class="nc">Make</span><span class="o">(</span><span class="mi">1</span><span class="o">,</span> <span class="n">_</span><span class="o">)</span> <span class="k">=&gt;</span> <span class="nc">List</span><span class="o">(</span><span class="nc">Part</span><span class="o">(</span><span class="mi">1</span><span class="o">,</span> <span class="s">&quot;Gear Box&quot;</span><span class="o">),</span> <span class="nc">Part</span><span class="o">(</span><span class="mi">2</span><span class="o">,</span> <span class="s">&quot;Clutch cable&quot;</span><span class="o">))</span>
</span><span class='line'>  <span class="k">case</span> <span class="k">_</span> <span class="k">=&gt;</span> <span class="nc">Nil</span>
</span><span class='line'><span class="o">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>It seems we&#8217;re in worse shape now! As before, <code>parts</code>&#8217;s input type doesn&#8217;t line up with <code>make</code>&#8217;s return type. Not only that, they aren&#8217;t even in the same monad anymore!</p>

<p>This clearly breaks our previous approach using a kleisli arrow to perform the composition. On the other hand it makes room for another approach: <a href="http://www.leonardoborges.com/writings/2012/11/30/monads-in-small-bites-part-i-functors/">Functor</a> <em>lifting</em>.</p>

<h2>Lifting</h2>

<p>In Scala - and category theory - monads are <a href="http://www.leonardoborges.com/writings/2012/11/30/monads-in-small-bites-part-i-functors/">functors</a>. As such both <code>Option</code> and <code>List</code> have access to a set of useful functor combinators. The one we&#8217;re interested in is called <a href="https://github.com/scalaz/scalaz/blob/scalaz-seven/core/src/main/scala/scalaz/Functor.scala#L31">lift</a>.</p>

<p>Say you have a function <code>A =&gt; B</code> and you have a functor <code>F[A]</code>. Lifting is the name of the operation that transforms the function  <code>A =&gt; B</code> into a function of type <code>F[A] =&gt; F[B]</code>.</p>

<p>This sounds useful. Here are our function types again:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='scala'><span class='line'><span class="n">make</span><span class="k">:</span> <span class="kt">Int</span> <span class="o">=&gt;</span> <span class="nc">Option</span><span class="o">[</span><span class="kt">Make</span><span class="o">]</span>
</span><span class='line'><span class="n">parts</span><span class="k">:</span> <span class="kt">Make</span> <span class="o">=&gt;</span> <span class="nc">List</span><span class="o">[</span><span class="kt">Part</span><span class="o">]</span>
</span></code></pre></td></tr></table></div></figure>


<p>We can&#8217;t get a function <code>Int =&gt; List[Part]</code> because <code>make</code> returns an <code>Option[Make]</code> meaning it can fail. We need to propagate this possibility in the composition. We can however <em>lift</em> <code>parts</code> into the <code>Option</code> monad, effectively changing its type from <code>Make =&gt; List[Part]</code> to <code>Option[Make] =&gt; Option[List[Part]]</code>:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='scala'><span class='line'><span class="k">val</span> <span class="n">f</span> <span class="k">=</span> <span class="nc">Functor</span><span class="o">[</span><span class="kt">Option</span><span class="o">].</span><span class="n">lift</span><span class="o">(</span><span class="n">parts</span><span class="o">)</span> <span class="n">compose</span> <span class="n">make</span>
</span><span class='line'><span class="n">f</span><span class="o">(</span><span class="mi">1</span><span class="o">)</span>
</span><span class='line'><span class="c1">// Some(List(Part(1,Gear Box), Part(2,Clutch cable)))</span>
</span></code></pre></td></tr></table></div></figure>


<p><code>f</code> now has the type <code>Int =&gt; Option[List[Part]]</code> and we have once again successfully composed both functions without writing any plumbing code ourselves.</p>

<p><a href="https://twitter.com/markhibberd">Mark</a> pointed out to me that <code>lift</code> is pretty much the same as <code>map</code> but with the arguments reversed. So the example above can be more succintly expressed as:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='scala'><span class='line'><span class="k">val</span> <span class="n">g</span> <span class="k">=</span> <span class="n">make</span><span class="o">(</span><span class="n">_:</span><span class="nc">Int</span><span class="o">).</span><span class="n">map</span><span class="o">(</span><span class="n">parts</span><span class="o">)</span>
</span><span class='line'><span class="n">g</span><span class="o">(</span><span class="mi">1</span><span class="o">)</span>
</span><span class='line'><span class="c1">// Some(List(Part(1,Gear Box), Part(2,Clutch cable)))</span>
</span></code></pre></td></tr></table></div></figure>


<h2>Summary</h2>

<p>If you are only now getting to this whole Functor/Monad/Kleisli <em>thing</em> this was probably quite heavy to get through. The point I am trying to make here is that learning at least <em>some</em> of the abstractions provided by Scalaz is certainly worthwhile. They encode common patterns which we would otherwise keep re-writing a lot of the time.</p>

<p>Special Thanks to <a href="https://twitter.com/markhibberd">Mark Hibberd</a> who kindly reviewed an early draft of this post.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Validation and Internationalization in Clojure with bouncer &amp; tower]]></title>
    <link href="http://www.leonardoborges.com/writings/2014/01/04/validation-and-internationalization-in-clojure-with-bouncer-and-tower/"/>
    <updated>2014-01-04T19:00:00+11:00</updated>
    <id>http://www.leonardoborges.com/writings/2014/01/04/validation-and-internationalization-in-clojure-with-bouncer-and-tower</id>
    <content type="html"><![CDATA[<p>I released <a href="https://github.com/leonardoborges/bouncer">bouncer</a> in April last year and since then it has had a small but steady growth in usage.</p>

<p>So much so that I received some community feedback in the form of emails and pull requests which is simply <em>great</em>!</p>

<p>The latest and most substantial pull request, submitted by <a href="https://github.com/dm3">Vadim Platonov</a>, added the ability to customise validation messages in anyway you like, as you can see in the section <a href="https://github.com/leonardoborges/bouncer#internationalization-and-customised-error-messages">Internationalization and customised error messages</a> of the <a href="https://github.com/leonardoborges/bouncer/blob/master/README.md">README</a>.</p>

<p>However the documentation only gives a simple example and while I believe it should show users how this opens up several different usage patterns, I thought I&#8217;d expand that in this post and integrate <a href="https://github.com/leonardoborges/bouncer">bouncer</a> with the excellent Internationalization library <a href="https://github.com/ptaoussanis/tower">tower</a> to show how validation and I18n could work together in this configuration.</p>

<blockquote><p>If you&#8217;ve never used bouncer before, I&#8217;d recommend you have a quick look at the <a href="https://github.com/leonardoborges/bouncer#basic-validations">Basic Validations</a> section of the <a href="https://github.com/leonardoborges/bouncer/blob/master/README.md">README</a> before continuing.</p></blockquote>

<h2>Setup</h2>

<p>The first thing you&#8217;ll need to do is create a new leiningen project and add both <a href="https://github.com/leonardoborges/bouncer">bouncer</a> and <a href="https://github.com/ptaoussanis/tower">tower</a> as dependencies in your <code>project.clj</code>:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">[</span><span class="nv">bouncer</span> <span class="s">&quot;0.3.1-beta1&quot;</span><span class="p">]</span>
</span><span class='line'><span class="p">[</span><span class="nv">com</span><span class="o">.</span><span class="nv">taoensso/tower</span> <span class="s">&quot;2.0.1&quot;</span><span class="p">]</span>
</span></code></pre></td></tr></table></div></figure>


<p>Next, in your <code>core</code> namespace - or wherever, really - require both libs and set up a dictionary to be used in the examples:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="nf">ns</span> <span class="nv">bouncer+tower</span><span class="o">.</span><span class="nv">core</span>
</span><span class='line'>  <span class="p">(</span><span class="nf">:require</span> <span class="p">[</span><span class="nv">taoensso</span><span class="o">.</span><span class="nv">tower</span> <span class="nv">:as</span> <span class="nv">tower</span>
</span><span class='line'>             <span class="nv">:refer</span> <span class="p">(</span><span class="nf">with-locale</span> <span class="nv">with-tscope</span> <span class="nv">t</span> <span class="nv">*locale*</span><span class="p">)]</span>
</span><span class='line'>            <span class="p">[</span><span class="nv">bouncer</span><span class="o">.</span><span class="nv">core</span> <span class="nv">:refer</span> <span class="p">[</span><span class="nv">validate</span><span class="p">]]</span>
</span><span class='line'>            <span class="p">[</span><span class="nv">bouncer</span><span class="o">.</span><span class="nv">validators</span> <span class="nv">:as</span> <span class="nv">v</span><span class="p">]))</span>
</span><span class='line'>
</span><span class='line'><span class="p">(</span><span class="k">def </span><span class="nv">my-tconfig</span>
</span><span class='line'>  <span class="p">{</span><span class="nv">:dev-mode?</span> <span class="nv">true</span>
</span><span class='line'>   <span class="nv">:fallback-locale</span> <span class="nv">:en</span>
</span><span class='line'>   <span class="nv">:dictionary</span>
</span><span class='line'>   <span class="p">{</span><span class="nv">:en</span>
</span><span class='line'>    <span class="p">{</span><span class="nv">:person</span>  <span class="p">{</span><span class="nv">:name</span> <span class="p">{</span><span class="nv">:required</span> <span class="s">&quot;A person must have a name&quot;</span><span class="p">}</span>
</span><span class='line'>               <span class="nv">:age</span>  <span class="p">{</span><span class="nv">:number</span>   <span class="s">&quot;A person&#39;s age must be a number. You provided &#39;%s&#39;&quot;</span><span class="p">}</span>
</span><span class='line'>               <span class="nv">:address</span> <span class="p">{</span><span class="nv">:postcode</span> <span class="p">{</span><span class="nv">:required</span> <span class="s">&quot;Missing postcode in address&quot;</span><span class="p">}}}</span>
</span><span class='line'>     <span class="nv">:missing</span>  <span class="s">&quot;&lt;Missing translation: [%1$s %2$s %3$s]&gt;&quot;</span><span class="p">}</span>
</span><span class='line'>    <span class="nv">:pt-BR</span>
</span><span class='line'>    <span class="p">{</span><span class="nv">:person</span>  <span class="p">{</span><span class="nv">:name</span> <span class="p">{</span><span class="nv">:required</span> <span class="s">&quot;Atributo Nome para Pessoa é obrigatório.&quot;</span><span class="p">}</span>
</span><span class='line'>               <span class="nv">:age</span>  <span class="p">{</span><span class="nv">:number</span>   <span class="s">&quot;Atributo Idade para Pessoa deve ser um número. Valor recebido foi &#39;%s&#39;&quot;</span><span class="p">}</span>
</span><span class='line'>               <span class="nv">:address</span> <span class="p">{</span><span class="nv">:postcode</span> <span class="p">{</span><span class="nv">:required</span> <span class="s">&quot;Endereço deve ter um código postal&quot;</span><span class="p">}}}</span>
</span><span class='line'>     <span class="nv">:missing</span>  <span class="s">&quot;&lt;Tradução ausente: [%1$s %2$s %3$s]&gt;&quot;</span><span class="p">}}})</span>
</span></code></pre></td></tr></table></div></figure>


<p>Also, we need someting to validate so go ahead and create a map representing a person:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="k">def </span><span class="nv">person</span> <span class="p">{</span><span class="nv">:age</span> <span class="s">&quot;NaN&quot;</span><span class="p">})</span>
</span></code></pre></td></tr></table></div></figure>


<h2>Customising bouncer</h2>

<p>Since <code>0.3.1-beta1</code>, <a href="https://github.com/leonardoborges/bouncer">bouncer</a>&#8217;s <code>validate</code> function receives an optional first argument called <em>message-fn</em>. As the name implies, it is a function from <em>error metadata</em> to, most likely, a string. This is our opportunity to use tower&#8217;s features to translate our error messages.</p>

<p>In order to accomplish that, we&#8217;ll be using this message function:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="k">defn </span><span class="nv">message-fn</span>
</span><span class='line'>  <span class="s">&quot;Receives a locale, tscope and a map containing error metadata.</span>
</span><span class='line'>
</span><span class='line'><span class="s">  Uses this information to return a I18n&#39;ed string&quot;</span>
</span><span class='line'>  <span class="p">[</span><span class="nv">locale</span> <span class="nv">tscope</span> <span class="p">{</span><span class="nv">:keys</span> <span class="p">[</span><span class="nv">path</span> <span class="nv">value</span><span class="p">]</span>
</span><span class='line'>                  <span class="p">{</span><span class="nv">validator</span> <span class="nv">:validator</span><span class="p">}</span> <span class="nv">:metadata</span><span class="p">}]</span>
</span><span class='line'>  <span class="p">(</span><span class="k">let </span><span class="p">[</span><span class="nv">tr-key</span> <span class="p">(</span><span class="nf">-&gt;&gt;</span> <span class="p">(</span><span class="nf">flatten</span> <span class="p">[</span><span class="nv">path</span> <span class="nv">validator</span><span class="p">])</span>
</span><span class='line'>                    <span class="p">(</span><span class="nb">map </span><span class="nv">name</span><span class="p">)</span>
</span><span class='line'>                    <span class="p">(</span><span class="nf">clojure</span><span class="o">.</span><span class="nv">string/join</span> <span class="s">&quot;/&quot;</span><span class="p">)</span>
</span><span class='line'>                    <span class="nv">keyword</span><span class="p">)]</span>
</span><span class='line'>    <span class="p">(</span><span class="nf">with-tscope</span> <span class="nv">tscope</span>
</span><span class='line'>      <span class="p">(</span><span class="nf">t</span> <span class="nv">locale</span> <span class="nv">my-tconfig</span> <span class="nv">tr-key</span> <span class="nv">value</span><span class="p">))))</span>
</span></code></pre></td></tr></table></div></figure>


<p>In order to understand the function above, let&#8217;s validate the person map using <code>identity</code> so we can inspect the error metadata that will be the third argument to this function:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="nf">validate</span> <span class="nv">identity</span>
</span><span class='line'>          <span class="nv">person</span>
</span><span class='line'>          <span class="nv">:name</span> <span class="nv">v/required</span><span class="p">)</span>
</span><span class='line'>
</span><span class='line'><span class="c1">;; [{:name</span>
</span><span class='line'><span class="c1">;;   ({:path [:name],</span>
</span><span class='line'><span class="c1">;;     :value nil,</span>
</span><span class='line'><span class="c1">;;     :args nil,</span>
</span><span class='line'><span class="c1">;;     :metadata</span>
</span><span class='line'><span class="c1">;;     {:optional false,</span>
</span><span class='line'><span class="c1">;;      :default-message-format &quot;%s must be present&quot;,</span>
</span><span class='line'><span class="c1">;;      :validator :bouncer.validators/required},</span>
</span><span class='line'><span class="c1">;;     :message nil})}</span>
</span><span class='line'><span class="c1">;;  {:age &quot;NaN&quot;,</span>
</span><span class='line'><span class="c1">;;   :bouncer.core/errors</span>
</span><span class='line'><span class="c1">;;   {:name</span>
</span><span class='line'><span class="c1">;;    ({:path [:name],</span>
</span><span class='line'><span class="c1">;;      :value nil,</span>
</span><span class='line'><span class="c1">;;      :args nil,</span>
</span><span class='line'><span class="c1">;;      :metadata</span>
</span><span class='line'><span class="c1">;;      {:optional false,</span>
</span><span class='line'><span class="c1">;;       :default-message-format &quot;%s must be present&quot;,</span>
</span><span class='line'><span class="c1">;;       :validator :bouncer.validators/required},</span>
</span><span class='line'><span class="c1">;;      :message nil})}}]</span>
</span></code></pre></td></tr></table></div></figure>


<p>As we&#8217;re only validating the name in this case, that&#8217;s all we get in the return value of <code>validate</code>. However you can see how we have all sorts of useful information now - hopefully this makes the <code>message-fn</code> code above easier to understand.</p>

<h2>Take it for a spin</h2>

<p>We&#8217;re now ready for a couple of examples, using two different locales. Let&#8217;s get on with it:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="nf">validate</span> <span class="p">(</span><span class="nb">partial </span><span class="nv">message-fn</span> <span class="nv">:en</span> <span class="nv">:person</span><span class="p">)</span>
</span><span class='line'>          <span class="nv">person</span>
</span><span class='line'>          <span class="nv">:name</span> <span class="nv">v/required</span>
</span><span class='line'>          <span class="nv">:age</span>  <span class="nv">v/number</span>
</span><span class='line'>          <span class="p">[</span><span class="nv">:address</span> <span class="nv">:postcode</span><span class="p">]</span> <span class="nv">v/required</span><span class="p">)</span>
</span><span class='line'>
</span><span class='line'>
</span><span class='line'><span class="c1">;; [{:address {:postcode (&quot;Missing postcode in address&quot;)},</span>
</span><span class='line'><span class="c1">;;   :age (&quot;A person&#39;s age must be a number. You provided &#39;NaN&#39;&quot;),</span>
</span><span class='line'><span class="c1">;;   :name (&quot;A person must have a name&quot;)}</span>
</span><span class='line'><span class="c1">;;  {:age &quot;NaN&quot;,</span>
</span><span class='line'><span class="c1">;;   :bouncer.core/errors</span>
</span><span class='line'><span class="c1">;;   {:address {:postcode (&quot;Missing postcode in address&quot;)},</span>
</span><span class='line'><span class="c1">;;    :age (&quot;A person&#39;s age must be a number. You provided &#39;NaN&#39;&quot;),</span>
</span><span class='line'><span class="c1">;;    :name (&quot;A person must have a name&quot;)}}]</span>
</span></code></pre></td></tr></table></div></figure>


<p>Now let&#8217;s get some messages in portuguese, shall we?</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="nf">validate</span> <span class="p">(</span><span class="nb">partial </span><span class="nv">message-fn</span> <span class="nv">:pt-BR</span> <span class="nv">:person</span><span class="p">)</span>
</span><span class='line'>          <span class="nv">person</span>
</span><span class='line'>          <span class="nv">:name</span> <span class="nv">v/required</span>
</span><span class='line'>          <span class="nv">:age</span>  <span class="nv">v/number</span>
</span><span class='line'>          <span class="p">[</span><span class="nv">:address</span> <span class="nv">:postcode</span><span class="p">]</span> <span class="nv">v/required</span><span class="p">)</span>
</span><span class='line'>
</span><span class='line'>
</span><span class='line'><span class="c1">;; [{:address {:postcode (&quot;Endereço deve ter um código postal&quot;)},</span>
</span><span class='line'><span class="c1">;;   :age</span>
</span><span class='line'><span class="c1">;;   (&quot;Atributo Idade para Pessoa deve ser um número. Valor recebido foi &#39;NaN&#39;&quot;),</span>
</span><span class='line'><span class="c1">;;   :name (&quot;Atributo Nome para Pessoa é obrigatório.&quot;)}</span>
</span><span class='line'><span class="c1">;;  {:age &quot;NaN&quot;,</span>
</span><span class='line'><span class="c1">;;   :bouncer.core/errors</span>
</span><span class='line'><span class="c1">;;   {:address {:postcode (&quot;Endereço deve ter um código postal&quot;)},</span>
</span><span class='line'><span class="c1">;;    :age</span>
</span><span class='line'><span class="c1">;;    (&quot;Atributo Idade para Pessoa deve ser um número. Valor recebido foi &#39;NaN&#39;&quot;),</span>
</span><span class='line'><span class="c1">;;    :name (&quot;Atributo Nome para Pessoa é obrigatório.&quot;)}}]</span>
</span></code></pre></td></tr></table></div></figure>


<p>In case you&#8217;re too lazy to do this all from scratch, I created a github repo containing this example ready for you to play with. <a href="https://github.com/leonardoborges/bouncer-tower">Go get it</a>.</p>

<h2>Conclusion</h2>

<p>Hopefully this gives you a little taste of what you can do with the latest version of <a href="https://github.com/leonardoborges/bouncer">bouncer</a>. Remember this is but one of many ways you could integrate the library so get creative :)</p>

<p>Once again, thanks to <a href="https://github.com/dm3">Vadim Platonov</a> for submitting this pull request.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[So Long 2013: Year Highlights]]></title>
    <link href="http://www.leonardoborges.com/writings/2014/01/01/so-long-2013-year-highlights/"/>
    <updated>2014-01-01T23:45:00+11:00</updated>
    <id>http://www.leonardoborges.com/writings/2014/01/01/so-long-2013-year-highlights</id>
    <content type="html"><![CDATA[<p>I finally got some free time so sit down and write this post. I was travelling with my girlfriend around the state of
Victoria, Australia - mostly around the <a href="http://www.visitvictoria.com/Regions/great-ocean-road">Great Ocean Road</a>. I had an amazing time and it&#8217;s a trip
I highly recommend anyone do.</p>

<p>As for this year&#8217;s highlights, let&#8217;s get started:</p>

<h2>Clojure</h2>

<p><a href="http://www.meetup.com/clj-syd/">clj-syd</a> - the Sydney Clojure User Group - continues going strong and this is in great part thanks to the amazing community behind it.
You can have a look at our <a href="https://github.com/clj-syd/clj-syd/wiki/2013">wiki page for 2013</a> to see what we&#8217;ve been up to.</p>

<p>A major highlight is the fact that we brought <a href="https://twitter.com/ambrosebs">Ambrose Bonnaire-Sergeant</a> - creator of <a href="https://github.com/clojure/core.typed">core.typed</a> - to Sydney to give a talk on his creation. You can read a summary of the talk on <a href="https://mail.google.com/mail/u/0/#search/to%3Aclj-syd%40googlegroups.com/1428350a8e888375">our mailing list</a>.</p>

<p>On a more personal note I&#8217;ve been basically living in Melbourne for the past ~6 months working on a full-time Clojure project. I have learned a lot and can&#8217;t wait to share a whole lot with the group once I&#8217;m back in Sydney.</p>

<h2>Speaking</h2>

<p>I was fortunate enough to speak at a couple of really cool events this year as well as a few user group talks. Here they are:</p>

<ul>
<li><p><a href="http://www.slideshare.net/borgesleonardo/functional-reactive-programming-in-clojurescript">Functional Reactive Programming in Clojurescript</a> - LambdaJam, May 2013 - Brisbane, Australia</p></li>
<li><p><a href="http://www.slideshare.net/borgesleonardo/clojure-macros-workshop-lambdajam-2013-cufp-2013">Clojure macros workshop</a> - LambdaJam, May 2013 - Brisbane, Australia / CUFP, October 2013 - Boston, USA</p></li>
<li><p><a href="http://www.slideshare.net/borgesleonardo/intro-to-clojures-coreasync">Intro to core.async</a> - #cljsyd - Sydney, Australia &amp; #cljmelb - Melbourne, Australia</p></li>
</ul>


<p>I have also attended Clojure/West in Portland back in March. Great conference, great content and amazing people. I put together some slides with my notes <a href="http://www.slideshare.net/borgesleonardo/clojurewest-2013-in-30-mins">on SlideShare</a>.</p>

<h2>Books</h2>

<p>2013 was a great year for Clojure as we saw quite a few new books be released during the year. I&#8217;ll briefly mention a couple:</p>

<h4><a href="http://shop.oreilly.com/product/0636920029786.do">Clojure Cookbook</a></h4>

<p>As the authors describe it: <em>&#8220;Clojure Cookbook marks Clojure&#8217;s entry in O&#8217;Reilly&#8217;s prestigious Cookbook Series. The book will contain hundreds of real-world problems and solutions, ranging from basic utilities to rich web services to heavy data processing.&#8221;</em></p>

<p>I have contributed three recipes to this book so I&#8217;m quite happy to see them make it in. You can buy the early digital edition from <a href="http://shop.oreilly.com/product/0636920029786.do">O&#8217;Reilly&#8217;s website</a>.</p>

<p>The book is the main effort of authors <a href="https://twitter.com/levanderhart">Luke Vanderhart</a> and <a href="https://twitter.com/rkneufeld">Ryan Neufeld</a> with contributions from the Clojure community around the globe.
They&#8217;ve put together quite the selection.</p>

<h4><a href="http://clojurerecipes.net/">Clojure Recipes</a></h4>

<p>Shameless plug of one of #cljsyd&#8217;s regulars, <a href="https://twitter.com/juliansgamble">Julian Gamble</a>, who&#8217;s been working on this for a while. The book should be released at the end of January 2014 and I&#8217;m looking forward to it.</p>

<h4><a href="http://www.packtpub.com/clojure-high-performance-programming/book">Clojure High Performance Programming</a></h4>

<p>I&#8217;ve only just started reading it but I already like what I see enough to mention it here.</p>

<h2>Content</h2>

<p>Here&#8217;s the Top 3 posts from this blog in 2013:</p>

<ul>
<li><p><a href="http://www.leonardoborges.com/writings/2013/03/25/clojure-and-why-calculating-is-better-than-scheming/">Clojure and &#8216;Why Calculating Is Better Than Scheming&#8217;</a> - A fresh look at this popular paper by <a href="http://homepages.inf.ed.ac.uk/wadler/">Philip Wadler</a> from a Clojure perspective.</p></li>
<li><p><a href="http://www.leonardoborges.com/writings/2013/07/06/clojure-core-dot-async-lisp-advantage/">Clojure, core.async and the Lisp Advantage</a> - This year saw much excitement around Clojure&#8217;s <a href="https://github.com/clojure/core.async">core.async</a> library and this post gives a brief introduction as to what it&#8217;s all about.</p></li>
<li><p><a href="http://www.leonardoborges.com/writings/2013/07/15/purely-functional-data-structures-in-clojure-red-black-trees/">Purely Functional Data Structures in Clojure: Red-Black Trees</a> - This post is from a series - though I only have two posts at the moment - about <a href="http://www.leonardoborges.com/writings/2013/07/15/purely-functional-data-structures-in-clojure-red-black-trees/">persistent data structures</a> in Clojure.</p></li>
</ul>


<h2>Non-tech news</h2>

<p>For those who know me my passion for sports - such as martial arts, rock climbing and strength training - is no secret and in 2013 I became a certified personal trainer in Australia. I did this mostly for personal development and to better my own training. The certification is already paying off. It was a great course.</p>

<p><strong>Happy new year! Here&#8217;s to an even better one ahead of us!</strong></p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[CUFP/ICFP 2013]]></title>
    <link href="http://www.leonardoborges.com/writings/2013/09/25/cufp-slash-icfp-2013/"/>
    <updated>2013-09-25T13:17:00+10:00</updated>
    <id>http://www.leonardoborges.com/writings/2013/09/25/cufp-slash-icfp-2013</id>
    <content type="html"><![CDATA[<p>I&#8217;m sitting in the Lobby of the Hilton in Boston and since my flight back to Australia isn&#8217;t for a few hours I thought I&#8217;d write my experience report while everything is still fresh in my mind.</p>

<p><img class="left" src="http://www.leonardoborges.com/writings/assets/images/posts/cufp2013-badge.jpg" width="318" height="400"></p>

<p><a href="http://cufp.org/">CUFP - Commercial Users of Functional Programming -</a> is a Workshop-like conference targeting the practically-minded functional programming community.</p>

<p>As it&#8217;s stated on their website, <em>&#8220;The CUFP workshop is a place where people can see how others are using functional programming to solve real world problems […]&#8221;</em>.</p>

<p>One of the things that make the event special is that it runs together with <a href="http://icfpconference.org/icfp2013/">ICFP - International Conference on Functional Programming</a> - which is an event on the far opposite side of the spectrum with language designers, professors, compiler implementors getting together and thinking about the future of their languages and fields. The diversity of the event is astonishing.</p>

<p>CUFP itself runs for three days and is divided into a traditional conference format day with several talks and two tutorial days.</p>

<p>I was there for all three days and on the last one I delivered my own tutorial about writing macros in Clojure - more on that later.</p>

<p>As far as the talks of day 1 go, someone already did a great job of summarising them. I highly recommend <a href="http://www.syslog.cl.cam.ac.uk/2013/09/22/liveblogging-cufp-2013/">you go read it</a>.</p>

<h3>Day 2</h3>

<p>In total there were 9 tutorials being offered - two of which made up a two-day Haskell tutorial. That&#8217;s the one I decided to attend.</p>

<p>In the instructions, the instructors mentioned that we could use an online Haskell IDE to follow the course should we choose not to install the Haskell platform on our laptops.</p>

<p>I decided to give it a go. The tool is called FP Haskell Center and has been developed by the awesome guys at <a href="https://www.fpcomplete.com">FP Complete</a>.</p>

<p>It&#8217;s important to note this is an online IDE - but the editor isn&#8217;t the only thing being offered though. The Haskell Centre offers a complete deployment solution as well - though I didn&#8217;t have the chance to play with it.</p>

<p>Back to the tutorial, I used the FP Haskell Centre for day one and it worked great as far as online IDEs go. Compilation and inspecting types do suffer from the round trip over the web and by the end of the day I was feeling a little frustrated with all the waiting. The tool is great and if they offered an offline version, I&#8217;m sure the experience would have been improved tenfold.</p>

<p>Day one was taught by <a href="http://www.well-typed.com/people/andres">Andres Löh</a> from <a href="http://www.well-typed.com/">Well-Typed</a>, a Haskell consultancy.</p>

<p>It was full of exercises in the various basics of Haskell such as expressions, functions, IO, pattern matching and even Monads. I had a lot of fun working through them and it reinforced my opinion about Haskell being as practical a language as any other - but with several advantages.</p>

<h3>Day 3</h3>

<p>Given my frustration with FP Haskell Center being a bit slow I decided to install the Haskell platform on my new laptop and configure emacs with haskell-mode. I was <em>much</em> happier with this setup. haskell-mode has a lot of nifty features that were extremely useful during the tutorial.</p>

<p>The second day of the Haskell tutorial gave way to <a href="http://community.haskell.org/~simonmar/">Simon Marlow</a>, a software engineer from Facebook UK and author of  <a href="http://community.haskell.org/~simonmar/pcph/">Parallel and Concurrent Programming in Haskell</a> - also available freely <a href="http://chimera.labs.oreilly.com/books/1230000000929/index.html">online</a>.</p>

<p>Not surprisingly, his half of the tutorial was about Concurrency. We started with several exercises on IO involving more Monadic functions that we hadn&#8217;t learned the previous day. We then moved on to study the basic Haskell concurrency constructs and primitives. All very interesting stuff.</p>

<p>If you have the chance to attend a tutorial by these guys, do yourself a favour and go for it.</p>

<h3>Day 3 - 2pm onwards</h3>

<p>Unfortunately I missed the second half of the second day as I, too, had to deliver my own tutorial/workshop.</p>

<p>Titled <em>&#8220;Bending Clojure to your will: Macros and Domain Specific Languages&#8221;</em>, the tutorial had participants work their way through several difference exercises aimed at teaching the various nuances of writing macros.</p>

<p>The tutorial has failing tests for all exercises so it&#8217;s dead easy to know when you have arrived at a solution - all participants seemed to have had a great time learning this stuff and I even saw a couple of positive tweets about it - I&#8217;m really happy with how everything went.</p>

<p>The best tweet though was the one saying that someone from the Haskell tutorial decided to switch to mine half-way through. So I see that as a win :) <a href="https://twitter.com/prasincs/status/382585215694413824">Here&#8217;s said tweet</a>.</p>

<p>I&#8217;ve <a href="https://github.com/leonardoborges/clojure-macros-workshop">pushed the exercises to github</a> should you want to try them by yourself. <a href="http://www.slideshare.net/borgesleonardo/clojure-macros-workshop-lambdajam-2013-cufp-2013">Slides are also available</a> though they will likely not make sense out of context.</p>

<p>Enjoy! :)</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Purely functional data structures in Clojure: Red-Black Trees]]></title>
    <link href="http://www.leonardoborges.com/writings/2013/07/15/purely-functional-data-structures-in-clojure-red-black-trees/"/>
    <updated>2013-07-15T21:42:00+10:00</updated>
    <id>http://www.leonardoborges.com/writings/2013/07/15/purely-functional-data-structures-in-clojure-red-black-trees</id>
    <content type="html"><![CDATA[<blockquote><p>This post is part of a series about Chris Okasaki&#8217;s <a href="http://amzn.to/UcIidh">Purely Functional Data Structures</a>. You can see all posts in the series by visiting the <a href="http://www.leonardoborges.com/writings/tags/functional-data-structures/">functional-data-structures</a> category in this blog.</p></blockquote>

<hr />

<p>Recently I had some free time to come back to <a href="http://amzn.to/UcIidh">Purely Functional Data Structures</a> and implement a new data structure: <a href="http://en.wikipedia.org/wiki/Red%E2%80%93black_tree">Red-black trees</a>.</p>

<h3>Red-black trees</h3>

<p><a href="http://en.wikipedia.org/wiki/Red%E2%80%93black_tree">Red-black trees</a> are a type of <a href="http://en.wikipedia.org/wiki/Self-balancing_binary_search_tree">self-balancing binary search tree</a>. Back when I first learned the balancing algorithm used in operations such as insert and delete, I remember it being a particularly tricky one.</p>

<p>Traditionally, red-black trees are implemented destructively - meaning insertions and deletions happen in place. So in imperative languages like C or Java there is a lot of node pointer manipulation floating around, making this algorithm error prone.</p>

<p>This post, as its title implies, will deal with the functional implementation which, besides simpler, is also persistent.</p>

<h4>Invariants</h4>

<p>A big disadvantage of binary search trees is that they operate poorly on sorted data with O(N) worst case, pretty much becoming nothing more than a linked list.</p>

<p>This is where Red-black trees come in: when inserting/removing new nodes, the tree balances itself thus guaranteeing search times of O(logN). It does so by satisfying two invariants:</p>

<ul>
<li>No red nodes can have red children</li>
<li>Every path from the root to an empty node contains the same number of black nodes</li>
</ul>


<p>The image below - taken from the book itself - concisely summarises the 4 cases involved in these invariants, starting at the top and then moving counter-clockwise.</p>

<p><img class="center" src="http://www.leonardoborges.com/writings/assets/images/posts/red-black-tree.jpg" width="250" height="333" title="Red-black trees invariants" ></p>

<p>With this in mind I set out to write the <em>balance</em> function in Clojure.</p>

<h3>The code</h3>

<p>In the functional version of a Red-black tree all pointer manipulation required in its destructive counterpart simply disappears, rendering the algorithm a lot simpler.</p>

<p>Nevertheless, we&#8217;re left with some complexity in the <em>balance</em> function around testing a node&#8217;s colour as well as its children&#8217;s and grandchildren&#8217;s.</p>

<p>My first attempt at writing it started like this:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="k">defn </span><span class="nv">mk-tree</span> <span class="p">[</span><span class="nv">color</span> <span class="nv">left</span> <span class="nv">value</span> <span class="nv">right</span><span class="p">]</span>
</span><span class='line'>  <span class="p">{</span><span class="nv">:color</span> <span class="nv">color</span> <span class="nv">:left</span> <span class="nv">left</span> <span class="nv">:value</span> <span class="nv">value</span> <span class="nv">:right</span> <span class="nv">right</span><span class="p">})</span>
</span><span class='line'>
</span><span class='line'><span class="p">(</span><span class="k">defn </span><span class="nv">balance</span> <span class="p">[</span><span class="nv">tree</span><span class="p">]</span>
</span><span class='line'>    <span class="c1">;; case 1</span>
</span><span class='line'>    <span class="p">(</span><span class="k">let </span><span class="p">[{</span><span class="nv">z</span> <span class="nv">:value</span> <span class="nv">d</span> <span class="nv">:right</span><span class="p">}</span> <span class="nv">tree</span>
</span><span class='line'>          <span class="p">{</span><span class="nv">x-color</span> <span class="nv">:color</span> <span class="nv">x</span> <span class="nv">:value</span> <span class="nv">a</span> <span class="nv">:left</span><span class="p">}</span> <span class="p">(</span><span class="nb">-&gt; </span><span class="nv">tree</span> <span class="nv">:left</span><span class="p">)</span>
</span><span class='line'>          <span class="p">{</span><span class="nv">y-color</span> <span class="nv">:color</span> <span class="nv">y</span> <span class="nv">:value</span> <span class="nv">b</span> <span class="nv">:left</span> <span class="nv">c</span> <span class="nv">:right</span><span class="p">}</span> <span class="p">(</span><span class="nb">-&gt; </span><span class="nv">tree</span> <span class="nv">:left</span> <span class="nv">:right</span><span class="p">)</span>
</span><span class='line'>          <span class="nv">d</span> <span class="p">(</span><span class="nf">:right</span> <span class="nv">tree</span><span class="p">)]</span>
</span><span class='line'>      <span class="p">(</span><span class="k">if </span><span class="p">(</span><span class="nb">and </span><span class="p">(</span><span class="nb">= </span><span class="nv">x-color</span> <span class="nv">:red</span><span class="p">)</span> <span class="p">(</span><span class="nb">= </span><span class="nv">y-color</span> <span class="nv">:red</span><span class="p">))</span>
</span><span class='line'>        <span class="p">(</span><span class="nf">mk-tree</span> <span class="nv">:red</span>
</span><span class='line'>                    <span class="p">(</span><span class="nf">mk-tree</span> <span class="nv">:black</span> <span class="nv">a</span> <span class="nv">x</span> <span class="nv">b</span><span class="p">)</span>
</span><span class='line'>                    <span class="nv">y</span>
</span><span class='line'>                    <span class="p">(</span><span class="nf">mk-tree</span> <span class="nv">:black</span> <span class="nv">c</span> <span class="nv">z</span> <span class="nv">d</span><span class="p">))</span>
</span><span class='line'>        <span class="nv">tree</span><span class="p">)</span>
</span><span class='line'>        <span class="o">...</span><span class="p">))</span>
</span></code></pre></td></tr></table></div></figure>


<p>I was extremely unhappy with this. A lot of boilerplate around bindings and tests. And this is only the first case.</p>

<p>But if you read Okasaki&#8217;s implementation of the algorithm in ML - or Haskell -  you&#8217;ll quickly realise how concise and elegant it is. The main reason for that is pattern matching, something we don&#8217;t have built-in in Clojure.</p>

<p>However, Clojure is a Lisp and has a powerful macros system at its disposal. That has given us <a href="https://github.com/clojure/core.match">core.match</a>, a pattern matching library for Clojure.</p>

<h4>core.match</h4>

<p>Using core.match, I rewrote my <em>balance</em> function:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="k">defn </span><span class="nv">balance</span> <span class="p">[</span><span class="nv">tree</span><span class="p">]</span>
</span><span class='line'>  <span class="p">(</span><span class="nf">match</span> <span class="p">[</span><span class="nv">tree</span><span class="p">]</span>
</span><span class='line'>         <span class="p">[(</span><span class="nf">:or</span> <span class="p">{</span><span class="nv">:left</span> <span class="p">{</span><span class="nv">:color</span> <span class="nv">:red</span>
</span><span class='line'>                       <span class="nv">:left</span> <span class="p">{</span><span class="nv">:color</span> <span class="nv">:red</span> <span class="nv">:left</span> <span class="nv">a</span> <span class="nv">:value</span> <span class="nv">x</span> <span class="nv">:right</span> <span class="nv">b</span><span class="p">}</span>
</span><span class='line'>                       <span class="nv">:value</span> <span class="nv">y</span> <span class="nv">:right</span> <span class="nv">c</span><span class="p">}</span>
</span><span class='line'>                <span class="nv">:value</span> <span class="nv">z</span> <span class="nv">:right</span> <span class="nv">d</span><span class="p">}</span>
</span><span class='line'>
</span><span class='line'>               <span class="p">{</span><span class="nv">:left</span> <span class="p">{</span><span class="nv">:color</span> <span class="nv">:red</span>
</span><span class='line'>                       <span class="nv">:left</span>  <span class="nv">a</span> <span class="nv">:value</span> <span class="nv">x</span>
</span><span class='line'>                       <span class="nv">:right</span> <span class="p">{</span><span class="nv">:color</span> <span class="nv">:red</span> <span class="nv">:value</span> <span class="nv">y</span> <span class="nv">:left</span> <span class="nv">b</span> <span class="nv">:right</span> <span class="nv">c</span><span class="p">}}</span>
</span><span class='line'>                <span class="nv">:value</span> <span class="nv">z</span> <span class="nv">:right</span> <span class="nv">d</span><span class="p">}</span>
</span><span class='line'>
</span><span class='line'>               <span class="p">{</span><span class="nv">:left</span> <span class="nv">a</span> <span class="nv">:value</span> <span class="nv">x</span>
</span><span class='line'>                <span class="nv">:right</span> <span class="p">{</span><span class="nv">:color</span> <span class="nv">:red</span>
</span><span class='line'>                        <span class="nv">:left</span> <span class="p">{</span><span class="nv">:color</span> <span class="nv">:red</span>
</span><span class='line'>                               <span class="nv">:left</span> <span class="nv">b</span> <span class="nv">:value</span> <span class="nv">y</span> <span class="nv">:right</span> <span class="nv">c</span><span class="p">}</span>
</span><span class='line'>                        <span class="nv">:value</span> <span class="nv">z</span> <span class="nv">:right</span> <span class="nv">d</span><span class="p">}}</span>
</span><span class='line'>
</span><span class='line'>               <span class="p">{</span><span class="nv">:left</span> <span class="nv">a</span> <span class="nv">:value</span> <span class="nv">x</span>
</span><span class='line'>                <span class="nv">:right</span> <span class="p">{</span><span class="nv">:color</span> <span class="nv">:red</span>
</span><span class='line'>                        <span class="nv">:left</span> <span class="nv">b</span> <span class="nv">:value</span> <span class="nv">y</span>
</span><span class='line'>                        <span class="nv">:right</span> <span class="p">{</span><span class="nv">:color</span> <span class="nv">:red</span>
</span><span class='line'>                                <span class="nv">:left</span> <span class="nv">c</span> <span class="nv">:value</span> <span class="nv">z</span> <span class="nv">:right</span> <span class="nv">d</span><span class="p">}}})]</span>
</span><span class='line'>         <span class="p">(</span><span class="nf">mk-tree</span> <span class="nv">:red</span>
</span><span class='line'>                     <span class="p">(</span><span class="nf">mk-tree</span> <span class="nv">:black</span> <span class="nv">a</span> <span class="nv">x</span> <span class="nv">b</span><span class="p">)</span>
</span><span class='line'>                     <span class="nv">y</span>
</span><span class='line'>                     <span class="p">(</span><span class="nf">mk-tree</span> <span class="nv">:black</span> <span class="nv">c</span> <span class="nv">z</span> <span class="nv">d</span><span class="p">))</span>
</span><span class='line'>
</span><span class='line'>         <span class="nv">:else</span> <span class="nv">tree</span><span class="p">))</span>
</span></code></pre></td></tr></table></div></figure>


<p>If you look closely at the patterns being matched, you&#8217;ll see they cater for all 4 cases and allow for both matching and binding in the same expressions. With only a little over double the size of a single case using the previous function, we now have a fully functioning <em>balance</em> implementation. This is better, but I wanted more.</p>

<p>What I really wanted to be able to write is this:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="k">defn </span><span class="nv">balance</span> <span class="p">[</span><span class="nv">tree</span><span class="p">]</span>
</span><span class='line'>    <span class="p">(</span><span class="nf">match</span> <span class="p">[</span><span class="nv">tree</span><span class="p">]</span>
</span><span class='line'>           <span class="p">[(</span><span class="nf">:or</span> <span class="p">(</span><span class="nf">Black</span><span class="o">.</span> <span class="p">(</span><span class="nf">Red</span><span class="o">.</span> <span class="p">(</span><span class="nf">Red</span><span class="o">.</span> <span class="nv">a</span> <span class="nv">x</span> <span class="nv">b</span><span class="p">)</span> <span class="nv">y</span> <span class="nv">c</span><span class="p">)</span> <span class="nv">z</span> <span class="nv">d</span><span class="p">)</span>
</span><span class='line'>                 <span class="p">(</span><span class="nf">Black</span><span class="o">.</span> <span class="p">(</span><span class="nf">Red</span><span class="o">.</span> <span class="nv">a</span> <span class="nv">x</span> <span class="p">(</span><span class="nf">Red</span><span class="o">.</span> <span class="nv">b</span> <span class="nv">y</span> <span class="nv">c</span><span class="p">))</span> <span class="nv">z</span> <span class="nv">d</span><span class="p">)</span>
</span><span class='line'>                 <span class="p">(</span><span class="nf">Black</span><span class="o">.</span> <span class="nv">a</span> <span class="nv">x</span> <span class="p">(</span><span class="nf">Red</span><span class="o">.</span> <span class="p">(</span><span class="nf">Red</span><span class="o">.</span> <span class="nv">b</span> <span class="nv">y</span> <span class="nv">c</span><span class="p">)</span> <span class="nv">z</span> <span class="nv">d</span><span class="p">))</span>
</span><span class='line'>                 <span class="p">(</span><span class="nf">Black</span><span class="o">.</span> <span class="nv">a</span> <span class="nv">x</span> <span class="p">(</span><span class="nf">Red</span><span class="o">.</span> <span class="nv">b</span> <span class="nv">y</span> <span class="p">(</span><span class="nf">Red</span><span class="o">.</span> <span class="nv">c</span> <span class="nv">z</span> <span class="nv">d</span><span class="p">))))]</span> <span class="p">(</span><span class="nf">Red</span><span class="o">.</span> <span class="p">(</span><span class="nf">Black</span><span class="o">.</span> <span class="nv">a</span> <span class="nv">x</span> <span class="nv">b</span><span class="p">)</span>
</span><span class='line'>                                                              <span class="nv">y</span>
</span><span class='line'>                                                              <span class="p">(</span><span class="nf">Black</span><span class="o">.</span> <span class="nv">c</span> <span class="nv">z</span> <span class="nv">d</span><span class="p">))</span>
</span><span class='line'>                 <span class="nv">:else</span> <span class="nv">tree</span><span class="p">))</span>
</span></code></pre></td></tr></table></div></figure>


<p>Unfortunately core.match doesn&#8217;t support records/protocols yet. However, while reading core.match&#8217;s source code, I found a test that implemented the balance algorithm using a different way of representing a tree.</p>

<p>You see, I went with the map approach because I like how you can ask for the specific keys that represent the parts of the tree, such as <em>:color</em>, <em>:left</em>, <em>:value</em> and <em>:right</em>. But if you&#8217;re happy with using positional elements in a vector to represent your tree, our <em>balance</em> function in Clojure becomes this:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="k">defn </span><span class="nv">balance</span> <span class="p">[</span><span class="nv">tree</span><span class="p">]</span>
</span><span class='line'>  <span class="p">(</span><span class="nf">match</span> <span class="p">[</span><span class="nv">tree</span><span class="p">]</span>
</span><span class='line'>         <span class="p">[(</span><span class="nf">:or</span> <span class="p">[</span><span class="nv">:black</span> <span class="p">[</span><span class="nv">:red</span> <span class="p">[</span><span class="nv">:red</span> <span class="nv">a</span> <span class="nv">x</span> <span class="nv">b</span><span class="p">]</span> <span class="nv">y</span> <span class="nv">c</span><span class="p">]</span> <span class="nv">z</span> <span class="nv">d</span><span class="p">]</span>
</span><span class='line'>               <span class="p">[</span><span class="nv">:black</span> <span class="p">[</span><span class="nv">:red</span> <span class="nv">a</span> <span class="nv">x</span> <span class="p">[</span><span class="nv">:red</span> <span class="nv">b</span> <span class="nv">y</span> <span class="nv">c</span><span class="p">]]</span> <span class="nv">z</span> <span class="nv">d</span><span class="p">]</span>
</span><span class='line'>               <span class="p">[</span><span class="nv">:black</span> <span class="nv">a</span> <span class="nv">x</span> <span class="p">[</span><span class="nv">:red</span> <span class="p">[</span><span class="nv">:red</span> <span class="nv">b</span> <span class="nv">y</span> <span class="nv">c</span><span class="p">]</span> <span class="nv">z</span> <span class="nv">d</span><span class="p">]]</span>
</span><span class='line'>               <span class="p">[</span><span class="nv">:black</span> <span class="nv">a</span> <span class="nv">x</span> <span class="p">[</span><span class="nv">:red</span> <span class="nv">b</span> <span class="nv">y</span> <span class="p">[</span><span class="nv">:red</span> <span class="nv">c</span> <span class="nv">z</span> <span class="nv">d</span><span class="p">]]])]</span> <span class="p">[</span><span class="nv">:red</span> <span class="p">[</span><span class="nv">:black</span> <span class="nv">a</span> <span class="nv">x</span> <span class="nv">b</span><span class="p">]</span>
</span><span class='line'>                                                            <span class="nv">y</span>
</span><span class='line'>                                                            <span class="p">[</span><span class="nv">:black</span> <span class="nv">c</span> <span class="nv">z</span> <span class="nv">d</span><span class="p">]]</span>
</span><span class='line'>               <span class="nv">:else</span> <span class="nv">tree</span><span class="p">))</span>
</span></code></pre></td></tr></table></div></figure>


<p>Which is <em>amazingly</em> similar to the protocols/records version above.</p>

<p>I guess one of the lessons here is that by carefully choosing the data structure to represent your problem, your implementation can become substantially simpler.</p>

<p>As usual, <a href="https://github.com/leonardoborges/purely-functional-data-structures">all code is on github</a>, with the Red-black tree specific section in <a href="https://github.com/leonardoborges/purely-functional-data-structures/blob/master/src/purely_functional_data_structures/ch3.clj#L384">this direct link</a>. The project also includes tests.</p>

<p>Until next time.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Clojure, core.async and the Lisp advantage]]></title>
    <link href="http://www.leonardoborges.com/writings/2013/07/06/clojure-core-dot-async-lisp-advantage/"/>
    <updated>2013-07-06T18:50:00+10:00</updated>
    <id>http://www.leonardoborges.com/writings/2013/07/06/clojure-core-dot-async-lisp-advantage</id>
    <content type="html"><![CDATA[<p>Long gone are the days when systems needed to do only one thing at a time.</p>

<p>Concurrency is the word but it often leads to complex code, dealing with locks, mutexes etc…</p>

<p>There are several different abstractions which allows us to both model and think about asynchronous code in a more sane fashion: futures, promises and events/callbacks are but a few of them.</p>

<p>I won&#8217;t get into the merits - or lack thereof - of these alternatives in this post but rather focus on a different alternative: <a href="http://en.wikipedia.org/wiki/Communicating_sequential_processes">Communicating Sequential Processes - CSP</a>.</p>

<h3>CSP and Go</h3>

<p>CSP isn&#8217;t new. It was first described in 1978 by <a href="http://en.wikipedia.org/wiki/C._A._R._Hoare">Tony Hoare</a> and languages such as <a href="http://bit.ly/14rEwxU">Occam</a> and <a href="http://bit.ly/14rEAh7">Erlang</a> stem from it.</p>

<p>It has however gained momentum by being natively supported by the <a href="http://bit.ly/11ZvMzj">Go programming language</a>.</p>

<p>I haven&#8217;t read Hoare&#8217;s paper so I&#8217;ll use a little bit of what I know about Go&#8217;s implementation of CSP.</p>

<p>Go introduced the concept of a <code>goroutine</code>. It <em>looks</em> like a function call:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='go'><span class='line'><span class="c1">// doing some stuff...</span>
</span><span class='line'><span class="k">go</span> <span class="n">myFunction</span><span class="p">(</span><span class="s">&quot;argument&quot;</span><span class="p">)</span> <span class="c1">//does stuff in the background...</span>
</span><span class='line'><span class="c1">//continuing about my business...</span>
</span></code></pre></td></tr></table></div></figure>


<p>What this does is it creates a <em>lightweight</em> process and returns control immediately to the caller.</p>

<p>It is <em>lightweight</em> because it doesn&#8217;t map 1-1 to native OS threads.</p>

<p>The reasoning behind it is that creating too many threads can bring your machine (or VM) down due to the amount of stack allocated to each one.</p>

<p><code>goroutines</code> are cheap to create so you can have hundreds of thousands of them, and the runtime will multiplex them into a thread pool.</p>

<p>The immediate advantage is that it is dead simple to achieve a higher degree of concurrency.</p>

<p>So far it sounds awfully similar to using futures with a pre-configured thread pool and a bit of syntactic sugar. But this is not the end of it.</p>

<h3>Communication</h3>

<p><code>goroutines</code> really shine when your several lightweight processes need to talk to each other. This is where a new abstraction comes into play:<code>channels</code>.</p>

<p>Channels are first-class citizens - meaning you can pass them as arguments to functions as well as the return value of functions.</p>

<p>Using them is straightforward:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
</pre></td><td class='code'><pre><code class='go'><span class='line'><span class="n">c</span> <span class="p">:=</span> <span class="nb">make</span><span class="p">(</span><span class="k">chan</span> <span class="nb">string</span><span class="p">)</span>
</span><span class='line'><span class="k">go</span> <span class="k">func</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'>  <span class="n">time</span><span class="p">.</span><span class="n">Sleep</span><span class="p">(</span><span class="n">time</span><span class="p">.</span><span class="n">Duration</span><span class="p">(</span><span class="mi">5000</span><span class="p">)</span> <span class="p">*</span> <span class="n">time</span><span class="p">.</span><span class="n">Millisecond</span><span class="p">)</span>
</span><span class='line'>  <span class="n">c</span> <span class="p">&lt;-</span> <span class="s">&quot;Leo&quot;</span>
</span><span class='line'><span class="p">}()</span>
</span><span class='line'><span class="n">fmt</span><span class="p">.</span><span class="n">Printf</span><span class="p">(</span><span class="s">&quot;Hello: %s\n&quot;</span><span class="p">,</span> <span class="p">&lt;-</span><span class="n">c</span><span class="p">)</span> <span class="c1">//this will block until the channel has something to give us </span>
</span></code></pre></td></tr></table></div></figure>


<p>The code above creates a goroutine from an anonymous-executing function that will, in the background, sleep for 5 seconds and then send the string <code>Leo</code> to the channel <code>c</code>. Since control is returned immediately after that, the call blocks on the next line where it&#8217;s trying to read a value from the channel using the <code>&lt;-c</code> statement.</p>

<h3>Lisp</h3>

<p>&#8220;But What does all this have to do with Lisp?&#8221; - ah! I&#8217;m glad you asked.</p>

<p>It actually has more to do with <a href="http://clojure.org/">Clojure</a> - and by extension, Lisp.</p>

<p><a href="http://clojure.org/">Clojure</a> is a modern Lisp for the JVM, built for concurrency. The core team recently released <a href="https://github.com/clojure/core.async">core.async</a>, a new library that adds support for asynchronous programming much in the same way Go does with goroutines.</p>

<p>To highlight the similarities, I&#8217;ll show and translate a couple of the examples from Rob Pike&#8217;s presentation, <a href="http://talks.golang.org/2012/concurrency.slide#1">Go Concurrency patterns</a>.</p>

<h4>Setting the scene</h4>

<p>Say you&#8217;re Google. And you need to write code that takes user input, - a search string - hits 3 different search services, - web, images and video - aggregates the results and presents them to the user.</p>

<p>Since they are three different services, you wish to do this concurrently.</p>

<p>To simulate these services, Rob presented a function that has unpredictable performance based of a random amount of sleep, shown below:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
</pre></td><td class='code'><pre><code class='go'><span class='line'><span class="k">var</span> <span class="p">(</span>
</span><span class='line'>    <span class="n">Web</span>   <span class="p">=</span> <span class="n">fakeSearch</span><span class="p">(</span><span class="s">&quot;web&quot;</span><span class="p">)</span>
</span><span class='line'>    <span class="n">Image</span> <span class="p">=</span> <span class="n">fakeSearch</span><span class="p">(</span><span class="s">&quot;image&quot;</span><span class="p">)</span>
</span><span class='line'>    <span class="n">Video</span> <span class="p">=</span> <span class="n">fakeSearch</span><span class="p">(</span><span class="s">&quot;video&quot;</span><span class="p">)</span>
</span><span class='line'><span class="p">)</span>
</span><span class='line'>
</span><span class='line'><span class="k">type</span> <span class="n">Search</span> <span class="k">func</span><span class="p">(</span><span class="n">query</span> <span class="nb">string</span><span class="p">)</span> <span class="n">Result</span>
</span><span class='line'>
</span><span class='line'><span class="k">func</span> <span class="n">fakeSearch</span><span class="p">(</span><span class="n">kind</span> <span class="nb">string</span><span class="p">)</span> <span class="n">Search</span> <span class="p">{</span>
</span><span class='line'>        <span class="k">return</span> <span class="k">func</span><span class="p">(</span><span class="n">query</span> <span class="nb">string</span><span class="p">)</span> <span class="n">Result</span> <span class="p">{</span>
</span><span class='line'>              <span class="n">time</span><span class="p">.</span><span class="n">Sleep</span><span class="p">(</span><span class="n">time</span><span class="p">.</span><span class="n">Duration</span><span class="p">(</span><span class="n">rand</span><span class="p">.</span><span class="n">Intn</span><span class="p">(</span><span class="mi">100</span><span class="p">))</span> <span class="p">*</span> <span class="n">time</span><span class="p">.</span><span class="n">Millisecond</span><span class="p">)</span>
</span><span class='line'>              <span class="k">return</span> <span class="n">Result</span><span class="p">(</span><span class="n">fmt</span><span class="p">.</span><span class="n">Sprintf</span><span class="p">(</span><span class="s">&quot;%s result for %q\n&quot;</span><span class="p">,</span> <span class="n">kind</span><span class="p">,</span> <span class="n">query</span><span class="p">))</span>
</span><span class='line'>        <span class="p">}</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>This is one way we could write such function in Clojure:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="k">defn </span><span class="nv">fake-search</span> <span class="p">[</span><span class="nv">kind</span><span class="p">]</span>
</span><span class='line'>  <span class="p">(</span><span class="k">fn </span><span class="p">[</span><span class="nv">query</span><span class="p">]</span>
</span><span class='line'>    <span class="p">(</span><span class="nf">Thread/sleep</span> <span class="p">(</span><span class="nb">int </span><span class="p">(</span><span class="nb">* </span><span class="p">(</span><span class="nf">java</span><span class="o">.</span><span class="nv">lang</span><span class="o">.</span><span class="nv">Math/random</span><span class="p">)</span> <span class="mi">1000</span><span class="p">)))</span>
</span><span class='line'>    <span class="p">(</span><span class="nb">str </span><span class="nv">kind</span> <span class="s">&quot; result for &quot;</span> <span class="nv">query</span><span class="p">)))</span>
</span><span class='line'>
</span><span class='line'><span class="p">(</span><span class="k">def </span><span class="nv">web</span>   <span class="p">(</span><span class="nf">fake-search</span> <span class="s">&quot;Web&quot;</span><span class="p">))</span>
</span><span class='line'><span class="p">(</span><span class="k">def </span><span class="nv">image</span> <span class="p">(</span><span class="nf">fake-search</span> <span class="s">&quot;Image&quot;</span><span class="p">))</span>
</span><span class='line'><span class="p">(</span><span class="k">def </span><span class="nv">video</span> <span class="p">(</span><span class="nf">fake-search</span> <span class="s">&quot;Video&quot;</span><span class="p">))</span>
</span></code></pre></td></tr></table></div></figure>


<h4>Google Search 2.0</h4>

<p>The first example is the simple case: we hit the services concurrently, wait for them to respond and then return the results:</p>

<blockquote><p>This example is from <a href="http://talks.golang.org/2012/concurrency.slide#46">slide #46</a>.</p></blockquote>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
</pre></td><td class='code'><pre><code class='go'><span class='line'><span class="k">func</span> <span class="n">Google</span><span class="p">(</span><span class="n">query</span> <span class="nb">string</span><span class="p">)</span> <span class="p">(</span><span class="n">results</span> <span class="p">[]</span><span class="n">Result</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>    <span class="n">c</span> <span class="p">:=</span> <span class="nb">make</span><span class="p">(</span><span class="k">chan</span> <span class="n">Result</span><span class="p">)</span>
</span><span class='line'>    <span class="k">go</span> <span class="k">func</span><span class="p">()</span> <span class="p">{</span> <span class="n">c</span> <span class="p">&lt;-</span> <span class="n">Web</span><span class="p">(</span><span class="n">query</span><span class="p">)</span> <span class="p">}</span> <span class="p">()</span>
</span><span class='line'>    <span class="k">go</span> <span class="k">func</span><span class="p">()</span> <span class="p">{</span> <span class="n">c</span> <span class="p">&lt;-</span> <span class="n">Image</span><span class="p">(</span><span class="n">query</span><span class="p">)</span> <span class="p">}</span> <span class="p">()</span>
</span><span class='line'>    <span class="k">go</span> <span class="k">func</span><span class="p">()</span> <span class="p">{</span> <span class="n">c</span> <span class="p">&lt;-</span> <span class="n">Video</span><span class="p">(</span><span class="n">query</span><span class="p">)</span> <span class="p">}</span> <span class="p">()</span>
</span><span class='line'>
</span><span class='line'>    <span class="k">for</span> <span class="n">i</span> <span class="p">:=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="p">&lt;</span> <span class="mi">3</span><span class="p">;</span> <span class="n">i</span><span class="p">++</span> <span class="p">{</span>
</span><span class='line'>        <span class="n">result</span> <span class="p">:=</span> <span class="p">&lt;-</span><span class="n">c</span>
</span><span class='line'>        <span class="n">results</span> <span class="p">=</span> <span class="n">append</span><span class="p">(</span><span class="n">results</span><span class="p">,</span> <span class="n">result</span><span class="p">)</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>    <span class="k">return</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>And here&#8217;s the Clojure version:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="k">defn </span><span class="nv">google2-0</span> <span class="p">[</span><span class="nv">query</span><span class="p">]</span>
</span><span class='line'>  <span class="p">(</span><span class="k">let </span><span class="p">[</span><span class="nv">c</span> <span class="p">(</span><span class="nf">chan</span><span class="p">)]</span>
</span><span class='line'>    <span class="p">(</span><span class="nf">go</span> <span class="p">(</span><span class="nf">&gt;!</span> <span class="nv">c</span> <span class="p">(</span><span class="nf">web</span> <span class="nv">query</span><span class="p">)))</span>
</span><span class='line'>    <span class="p">(</span><span class="nf">go</span> <span class="p">(</span><span class="nf">&gt;!</span> <span class="nv">c</span> <span class="p">(</span><span class="nf">image</span> <span class="nv">query</span><span class="p">)))</span>
</span><span class='line'>    <span class="p">(</span><span class="nf">go</span> <span class="p">(</span><span class="nf">&gt;!</span> <span class="nv">c</span> <span class="p">(</span><span class="nf">video</span> <span class="nv">query</span><span class="p">)))</span>
</span><span class='line'>    <span class="p">(</span><span class="nb">reduce </span><span class="p">(</span><span class="k">fn </span><span class="p">[</span><span class="nv">results</span> <span class="nv">_</span><span class="p">]</span>
</span><span class='line'>              <span class="p">(</span><span class="nb">conj </span><span class="nv">results</span> <span class="p">(</span><span class="nf">&lt;!!</span> <span class="p">(</span><span class="nf">go</span> <span class="p">(</span><span class="nf">&lt;!</span> <span class="nv">c</span><span class="p">)))))</span>
</span><span class='line'>            <span class="p">[]</span>
</span><span class='line'>            <span class="p">(</span><span class="nb">range </span><span class="mi">3</span><span class="p">))))</span>
</span><span class='line'>
</span><span class='line'><span class="p">(</span><span class="nf">google2-0</span> <span class="s">&quot;Clojure&quot;</span><span class="p">)</span>
</span><span class='line'><span class="c1">;; prints [&quot;Video result for Clojure&quot; &quot;Web result for Clojure&quot; &quot;Image result for Clojure&quot;]</span>
</span></code></pre></td></tr></table></div></figure>


<p>The <a href="http://clojure.github.io/core.async/#clojure.core.async/%3E!"><code>&gt;!</code></a> operator puts a value into a channel inside a <code>go</code> form. The function then uses <a href="http://clojure.github.io/core.async/#clojure.core.async/&lt;!!"><code>&lt;!!</code></a> to block on the <code>c</code> channel until it gets a value we can use.</p>

<h4>Google Search 2.1</h4>

<p>This is virtually the same example but this time we do not wish to wait on slow servers. So we&#8217;ll return whatever results we have after a pre-defined timeout.</p>

<blockquote><p>This example is from <a href="http://talks.golang.org/2012/concurrency.slide#47">slide #47</a>.</p></blockquote>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
</pre></td><td class='code'><pre><code class='go'><span class='line'><span class="n">c</span> <span class="p">:=</span> <span class="nb">make</span><span class="p">(</span><span class="k">chan</span> <span class="n">Result</span><span class="p">)</span>
</span><span class='line'><span class="k">go</span> <span class="k">func</span><span class="p">()</span> <span class="p">{</span> <span class="n">c</span> <span class="p">&lt;-</span> <span class="n">Web</span><span class="p">(</span><span class="n">query</span><span class="p">)</span> <span class="p">}</span> <span class="p">()</span>
</span><span class='line'><span class="k">go</span> <span class="k">func</span><span class="p">()</span> <span class="p">{</span> <span class="n">c</span> <span class="p">&lt;-</span> <span class="n">Image</span><span class="p">(</span><span class="n">query</span><span class="p">)</span> <span class="p">}</span> <span class="p">()</span>
</span><span class='line'><span class="k">go</span> <span class="k">func</span><span class="p">()</span> <span class="p">{</span> <span class="n">c</span> <span class="p">&lt;-</span> <span class="n">Video</span><span class="p">(</span><span class="n">query</span><span class="p">)</span> <span class="p">}</span> <span class="p">()</span>
</span><span class='line'>
</span><span class='line'><span class="n">timeout</span> <span class="p">:=</span> <span class="n">time</span><span class="p">.</span><span class="n">After</span><span class="p">(</span><span class="mi">80</span> <span class="p">*</span> <span class="n">time</span><span class="p">.</span><span class="n">Millisecond</span><span class="p">)</span>
</span><span class='line'><span class="k">for</span> <span class="n">i</span> <span class="p">:=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="p">&lt;</span> <span class="mi">3</span><span class="p">;</span> <span class="n">i</span><span class="p">++</span> <span class="p">{</span>
</span><span class='line'>    <span class="k">select</span> <span class="p">{</span>
</span><span class='line'>    <span class="k">case</span> <span class="n">result</span> <span class="p">:=</span> <span class="p">&lt;-</span><span class="n">c</span><span class="p">:</span>
</span><span class='line'>        <span class="n">results</span> <span class="p">=</span> <span class="n">append</span><span class="p">(</span><span class="n">results</span><span class="p">,</span> <span class="n">result</span><span class="p">)</span>
</span><span class='line'>    <span class="k">case</span> <span class="p">&lt;-</span><span class="n">timeout</span><span class="p">:</span>
</span><span class='line'>        <span class="n">fmt</span><span class="p">.</span><span class="n">Println</span><span class="p">(</span><span class="s">&quot;timed out&quot;</span><span class="p">)</span>
</span><span class='line'>        <span class="k">return</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'><span class="k">return</span>
</span></code></pre></td></tr></table></div></figure>


<p>You&#8217;ll notice the use of <code>select</code> here.</p>

<p><code>select</code> waits on multiple channels and returns as soon as <em>any</em> of them has something to say.</p>

<p>The trick of this example is that one of these channels times out, at which point you get the message &#8220;timed out&#8221;, effectively moving on to the next iteration and ignoring that slow server(s) response.</p>

<p>We can express the same intent in Clojure as well:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="k">defn </span><span class="nv">google2-1</span> <span class="p">[</span><span class="nv">query</span><span class="p">]</span>
</span><span class='line'>  <span class="p">(</span><span class="k">let </span><span class="p">[</span><span class="nv">c</span> <span class="p">(</span><span class="nf">chan</span><span class="p">)</span>
</span><span class='line'>        <span class="nv">t</span> <span class="p">(</span><span class="nf">timeout</span> <span class="mi">500</span><span class="p">)]</span>
</span><span class='line'>    <span class="p">(</span><span class="nf">go</span> <span class="p">(</span><span class="nf">&gt;!</span> <span class="nv">c</span> <span class="p">(</span><span class="nf">web</span> <span class="nv">query</span><span class="p">)))</span>
</span><span class='line'>    <span class="p">(</span><span class="nf">go</span> <span class="p">(</span><span class="nf">&gt;!</span> <span class="nv">c</span> <span class="p">(</span><span class="nf">image</span> <span class="nv">query</span><span class="p">)))</span>
</span><span class='line'>    <span class="p">(</span><span class="nf">go</span> <span class="p">(</span><span class="nf">&gt;!</span> <span class="nv">c</span> <span class="p">(</span><span class="nf">video</span> <span class="nv">query</span><span class="p">)))</span>
</span><span class='line'>
</span><span class='line'>    <span class="p">(</span><span class="nb">reduce </span><span class="p">(</span><span class="k">fn </span><span class="p">[</span><span class="nv">results</span> <span class="nv">_</span><span class="p">]</span>
</span><span class='line'>              <span class="p">(</span><span class="nb">conj </span><span class="nv">results</span> <span class="p">(</span><span class="nb">first </span><span class="p">(</span><span class="nf">alts!!</span> <span class="p">[</span><span class="nv">c</span> <span class="nv">t</span><span class="p">]))))</span>
</span><span class='line'>            <span class="p">[]</span>
</span><span class='line'>            <span class="p">(</span><span class="nb">range </span><span class="mi">3</span><span class="p">))))</span>
</span><span class='line'>
</span><span class='line'>
</span><span class='line'><span class="p">(</span><span class="nf">google2-1</span> <span class="s">&quot;Clojure&quot;</span><span class="p">)</span>
</span><span class='line'><span class="c1">;; prints [&quot;Video result for Clojure&quot; nil nil]</span>
</span></code></pre></td></tr></table></div></figure>


<p>Everything looks the same but we&#8217;re using <a href="http://clojure.github.io/core.async/#clojure.core.async/alts!!"><code>alts!!</code></a> to wait on the channels <code>c</code> and <code>t</code> (the timeout channel). This is analogous to Go&#8217;s <code>select</code> form in that it waits for any channel to receive a value or, in this case, to timeout.</p>

<p>Note the <code>nil</code> values. Those came from servers which did not respond in time and were simply ignored.</p>

<p>Effectively what this means is that each time you run this function you&#8217;ll likely get different results, depending on how long the <code>fake-search</code> function takes to run.</p>

<p>Amazing, huh?</p>

<h3>The big deal</h3>

<p>But here&#8217;s the <em>big deal</em> about this: although <a href="https://github.com/clojure/core.async">core.async</a> looks like it&#8217;s deeply integrated into the language, it is <em>just</em> a library!</p>

<p>It&#8217;s not a separate compiler. It&#8217;s not a new language. And it&#8217;s not a special version of Clojure.</p>

<p>Since Clojure supports macros - like all Lisps - the core team was able to create the syntax required to easily use <a href="https://github.com/clojure/core.async">core.async</a>. And that&#8217;s the beauty of it!</p>

<p><em>The Lisp advantage, once again.</em></p>

<h4>Clojure&#8217;s advantage</h4>

<p>Now one thing I haven&#8217;t mentioned is that Clojure is particularly well suited for this - and in a way even more so than Go: Clojure is opinionated and favours immutability.</p>

<p>That means that when using channels - and in fact any type of concurrent programming - you can safely share your data structures between concurrent units of work. Since they&#8217;re immutable, you can&#8217;t shoot yourself in the foot.</p>

<p>One last thing: <a href="https://github.com/clojure/core.async">core.async</a> states as one of its goals Clojurescript compatibility, bringing channel based concurrent programming to the browser. Exciting stuff.</p>

<h3>More on core.async</h3>

<p><a href="https://github.com/clojure/core.async">core.async</a> is still in alpha but you are encouraged to take it for a spin. Documentation is still lacking so I recommend you look at:</p>

<ul>
<li><a href="http://clojure.com/blog/2013/06/28/clojure-core-async-channels.html">Rich&#8217;s blog post about it</a></li>
<li><a href="https://github.com/clojure/core.async">The source code on github</a></li>
<li><a href="https://github.com/clojure/core.async/blob/master/examples/walkthrough.clj">The walkthrough namespace</a>, which showcases its features.</li>
</ul>


<p>Also, the full Clojure code I used here can be seen in <a href="https://gist.github.com/leonardoborges/5924461">this gist</a>.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Functional Programmers Unite! LambdaJam down under]]></title>
    <link href="http://www.leonardoborges.com/writings/2013/04/11/functional-programmers-unite-lambdajam-down-under/"/>
    <updated>2013-04-11T11:37:00+10:00</updated>
    <id>http://www.leonardoborges.com/writings/2013/04/11/functional-programmers-unite-lambdajam-down-under</id>
    <content type="html"><![CDATA[<p>I hinted at <a href="http://www.yowconference.com.au/lambdajam/">LambdaJam Australia</a> back in my <a href="http://www.leonardoborges.com/writings/2013/01/02/so-long-2012-year-highlights/">2012 Highlights post</a> and the dates are fast approaching so I thought appropriate to blog about it once more.</p>

<p><a href="http://www.yowconference.com.au/lambdajam/">YOW! LambdaJam</a> is a conference targeted at intermediate and advanced functional programmers and is organised in talks, jams and workshops.</p>

<ul>
<li><strong>Talks</strong> are the traditional format and are 30 minutes long;</li>
<li><strong>Workshops</strong> aim to introduce a specific subjects to attendees in great detail and are up to two hours long;</li>
<li><strong>Jams</strong> are hands-on sessions. Participants will be guided through pre-defined problems around a given subject/technology and encouraged to work through the solutions either by themselves or ideally as small groups. Jams are also 2 hours long;</li>
</ul>


<p>Sounds pretty amazing, doesn&#8217;t it?</p>

<p>If you&#8217;re still not convinced, check out the <a href="http://www.yowconference.com.au/lambdajam/Program.html">program</a>.</p>

<p>The conference will run for 2 days, May 16-17, in Brisbane. <a href="http://yowlambdajam2013.eventbrite.com.au/">Tickets are on sale</a>.</p>

<h3>Clojure</h3>

<p>Besides the conference itself, <a href="http://clojure.com/">Clojure/core</a> will be giving an Intro to Clojure workshop in Sydney, Melbourne and Brisbane. It&#8217;s a great opportunity to learn from the language maintainers.</p>

<p>If you&#8217;re attending the conference already, you&#8217;re in luck because there is a 20% off discount <a href="http://yowlambdajam2013.eventbrite.com.au/">on the link above</a> if you attend both the conference and the workshop.</p>

<p>In case you&#8217;d like to attend the workshops only, you can buy individual tickets for <a href="http://clojurecore-sydney.eventbrite.com.au/">Sydney</a>, <a href="http://clojurecore-melbourne.eventbrite.com.au/">Melbourne</a> and <a href="http://clojurecore-brisbane-eorg.eventbrite.com.au/">Brisbane</a>.</p>

<h3>Scala</h3>

<p><a href="http://tmorris.net/">Tony Morris</a> will also be delivering a Functional Programming in Scala workshop in Brisbane and 20% off combo tickets can be bought from the <a href="http://yowlambdajam2013.eventbrite.com.au/">EventBrite page for LambdaJam</a>.</p>

<h3>See you there</h3>

<p>Got tickets yet? No? What are you waiting for?</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Clojure and 'Why calculating is better than scheming']]></title>
    <link href="http://www.leonardoborges.com/writings/2013/03/25/clojure-and-why-calculating-is-better-than-scheming/"/>
    <updated>2013-03-25T11:00:00+11:00</updated>
    <id>http://www.leonardoborges.com/writings/2013/03/25/clojure-and-why-calculating-is-better-than-scheming</id>
    <content type="html"><![CDATA[<p>Last week while attending <a href="http://clojurewest.org">Clojure/West</a> in Portland I came across a paper called <a href="http://www.cs.kent.ac.uk/people/staff/dat/miranda/wadler87.pdf">Why calculating is better than scheming</a>. In a nutshell, this paper is a critique to <a href="http://en.wikipedia.org/wiki/Hal_Abelson">Abelson</a> and <a href="http://en.wikipedia.org/wiki/Gerald_Jay_Sussman">Sussman</a>&#8217;s classic textbook <a href="http://mitpress.mit.edu/sicp/">SICP - Structure and Interpretation of Computer Programs</a>,
used by MIT for many years to teach their introductory programming course.</p>

<p>If you haven&#8217;t read <a href="http://mitpress.mit.edu/sicp/">SICP</a>, you should. It&#8217;s an amazing book. It uses <a href="http://en.wikipedia.org/wiki/Scheme_(programming_language)">Scheme</a>, a dialect of Lisp, as the vehicle to present fundamental programming concepts.</p>

<p><a href="http://homepages.inf.ed.ac.uk/wadler/">Philip Wadler</a> - the author of this particular paper - contrasts teaching in <a href="http://en.wikipedia.org/wiki/Scheme_(programming_language)">Scheme</a> to teaching using <a href="http://en.wikipedia.org/wiki/Kent_Recursive_Calculator">KRC</a> and <a href="http://en.wikipedia.org/wiki/Miranda_(programming_language)">Miranda</a>, pointing out
four major features he considers important and lacking in <a href="http://en.wikipedia.org/wiki/Scheme_(programming_language)">Scheme</a>. They are:</p>

<ul>
<li>Pattern matching</li>
<li>A syntax close to traditional mathematical notation</li>
<li>A static type discipline and user-defined types</li>
<li>Lazy Evaluation</li>
</ul>


<blockquote><p>Note:  <a href="http://en.wikipedia.org/wiki/Kent_Recursive_Calculator">KRC</a> influenced <a href="http://en.wikipedia.org/wiki/Miranda_(programming_language)">Miranda</a> which in turn influenced <a href="http://www.haskell.org/haskellwiki/Haskell">Haskell</a>.  Their syntax is similiar, so where Wadler used Miranda code snippets in the paper, I&#8217;ll be using Haskell in this post.</p></blockquote>

<p>As an aside, although the paper talks specifically of <a href="http://en.wikipedia.org/wiki/Scheme_(programming_language)">Scheme</a>, the term Lisp is used quite loosely and could lead the not-so-careful reader to be misled regarding a whole family of languages. Lisps have come a long way and modern dialects - of which I&#8217;ll be focusing on <a href="http://clojure.org/">Clojure</a> - address many of the concerns raised by <a href="http://homepages.inf.ed.ac.uk/wadler/">Wadler</a>.</p>

<p>Let us begin.</p>

<h3>Pattern Matching</h3>

<p>Here Clojure, and most - all? - Lisps, are out of luck.</p>

<p>The example used in the paper is that of summing all integers in a list. First in Haskell:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='haskell'><span class='line'><span class="nf">sum</span> <span class="kt">[]</span>   <span class="ow">=</span> <span class="mi">0</span>
</span><span class='line'><span class="nf">sum</span> <span class="n">x</span><span class="kt">:</span><span class="n">xs</span> <span class="ow">=</span> <span class="n">x</span> <span class="o">+</span> <span class="n">sum</span> <span class="n">xs</span>
</span></code></pre></td></tr></table></div></figure>


<p>Now in Clojure:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="k">defn </span><span class="nv">sum</span> <span class="p">[</span><span class="nv">coll</span><span class="p">]</span>
</span><span class='line'>  <span class="p">(</span><span class="k">if </span><span class="p">(</span><span class="nf">empty?</span> <span class="nv">coll</span><span class="p">)</span>
</span><span class='line'>      <span class="mi">0</span>
</span><span class='line'>      <span class="p">(</span><span class="nb">+ </span><span class="p">(</span><span class="nb">first </span><span class="nv">coll</span><span class="p">)</span> <span class="p">(</span><span class="nf">sum</span> <span class="p">(</span><span class="nb">rest </span><span class="nv">coll</span><span class="p">)))))</span>
</span></code></pre></td></tr></table></div></figure>


<p>The question here is this: Which snippet is easier to read/reason about? the Haskell code!</p>

<p>I must confess that I, too, miss pattern matching sometimes. However we can still improve our Clojure version to read nicer on the eyes by using <a href="http://www.haskell.org/haskellwiki/Haskell">destructuring</a>:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="k">defn </span><span class="nv">sum</span> <span class="p">[[</span><span class="nv">first</span> <span class="nv">&amp;</span> <span class="nv">rest</span> <span class="nv">:as</span> <span class="nv">coll</span><span class="p">]]</span>
</span><span class='line'>  <span class="p">(</span><span class="k">if </span><span class="p">(</span><span class="nf">empty?</span> <span class="nv">coll</span><span class="p">)</span>
</span><span class='line'>      <span class="mi">0</span>
</span><span class='line'>      <span class="p">(</span><span class="nb">+ </span><span class="nv">first</span> <span class="p">(</span><span class="nf">sum</span> <span class="nv">rest</span><span class="p">))))</span>
</span></code></pre></td></tr></table></div></figure>


<p>And that&#8217;s pretty much it. Without proper pattern matching, we can&#8217;t get much better than that.</p>

<p>In addition to the Haskell snippet being easier to read, it&#8217;s also easier to prove correct by structural induction, as demonstrated in Wadler&#8217;s paper.</p>

<blockquote><p>Note: <a href="https://github.com/clojure/core.match">core.match</a> adds support to pattern matching in Clojure. At the time of this writing, it&#8217;s considered &#8220;alpha quality&#8221;</p></blockquote>

<h3>Data structures</h3>

<p>The paper continues to discuss exercise 2-27 from the <a href="http://mitpress.mit.edu/sicp/">SICP</a>, where the reader has to write code to represent a binary mobile, which consists of a left and right branch with each branch being a rod of certain length, from which hangs either a weight or another binary mobile.</p>

<p>Translating the Scheme example to Clojure, such a structure is represented using lists, like so:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="k">defn </span><span class="nv">make-mobile</span> <span class="p">[</span><span class="nv">left</span> <span class="nv">right</span><span class="p">]</span>
</span><span class='line'>  <span class="p">(</span><span class="nb">list </span><span class="nv">left</span> <span class="nv">right</span><span class="p">))</span>
</span><span class='line'>  
</span><span class='line'><span class="p">(</span><span class="k">defn </span><span class="nv">make-branch</span> <span class="p">[</span><span class="nv">length</span> <span class="nv">structure</span><span class="p">]</span>
</span><span class='line'>  <span class="p">(</span><span class="nb">list </span><span class="nv">length</span> <span class="nv">structure</span><span class="p">))</span>
</span></code></pre></td></tr></table></div></figure>


<p>Wadler then contrasts this with the equivalent Miranda code, translated below to Haskell, taking advantage of <a href="http://www.haskell.org/haskellwiki/Algebraic_data_type">algebraic data types</a>:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='haskell'><span class='line'><span class="kr">data</span> <span class="kt">Structure</span> <span class="ow">=</span> <span class="kt">Weight</span> <span class="kt">Int</span> <span class="o">|</span> <span class="kt">Mobile</span> <span class="kt">Branch</span> <span class="kt">Branch</span>
</span><span class='line'><span class="kr">data</span> <span class="kt">Branch</span> <span class="ow">=</span> <span class="kt">Branch</span> <span class="kt">Int</span> <span class="kt">Structure</span>
</span></code></pre></td></tr></table></div></figure>


<p>The first claim is that the Haskell/Miranda data type declaration makes it clearer what the data structure looks like, which is fair.</p>

<p>Also, the compiler can catch errors early on.</p>

<p>However, when writing idiomatic Clojure code, here&#8217;s how I&#8217;d actually create this structure:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="k">defn </span><span class="nv">make-mobile</span> <span class="p">[</span><span class="nv">left</span> <span class="nv">right</span><span class="p">]</span>
</span><span class='line'>  <span class="p">{</span><span class="nv">:left</span> <span class="nv">left</span> <span class="nv">:right</span> <span class="nv">right</span><span class="p">})</span>
</span><span class='line'>  
</span><span class='line'><span class="p">(</span><span class="k">defn </span><span class="nv">make-branch</span> <span class="p">[</span><span class="nv">length</span> <span class="nv">structure</span><span class="p">]</span>
</span><span class='line'>  <span class="p">{</span><span class="nv">:length</span> <span class="nv">length</span> <span class="nv">:structure</span> <span class="nv">structure</span><span class="p">})</span>
</span></code></pre></td></tr></table></div></figure>


<p>Granted, it&#8217;s still not as clear and the compiler can&#8217;t validate the shape of our data structure.</p>

<p>This is however cleaner than the previous version and drives home the point that Clojure isn&#8217;t limited to lists, having literals for other data types such as the hash maps used in this example.</p>

<p>The second part of this claim is that through custom data types and pattern mathing, extracting values from those structures becomes simpler:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='haskell'><span class='line'><span class="nf">totalWeight</span> <span class="p">(</span><span class="kt">Weight</span> <span class="n">w</span><span class="p">)</span> <span class="ow">=</span> <span class="n">w</span>
</span><span class='line'><span class="nf">totalWeight</span> <span class="p">(</span><span class="kt">Mobile</span> <span class="n">l</span> <span class="n">r</span><span class="p">)</span> <span class="ow">=</span> <span class="n">totalWeightBranch</span> <span class="n">l</span> <span class="o">+</span> <span class="n">totalWeightBranch</span> <span class="n">r</span>
</span><span class='line'>
</span><span class='line'><span class="nf">totalWeightBranch</span><span class="p">(</span><span class="kt">Branch</span> <span class="n">d</span> <span class="n">s</span><span class="p">)</span> <span class="ow">=</span> <span class="n">totalWeight</span> <span class="n">s</span>
</span></code></pre></td></tr></table></div></figure>


<p>Once again Clojure can improve things by taking advantage of its builtin data structures and destructuring:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="k">defn </span><span class="nv">total-weight</span> <span class="p">[{</span><span class="nv">:keys</span> <span class="p">[</span><span class="nv">left</span> <span class="nv">right</span><span class="p">]</span> <span class="nv">:as</span> <span class="nv">structure</span><span class="p">}]</span>
</span><span class='line'>  <span class="p">(</span><span class="k">if </span><span class="p">(</span><span class="nf">number?</span> <span class="nv">structure</span><span class="p">)</span>
</span><span class='line'>      <span class="nv">structure</span>
</span><span class='line'>      <span class="p">(</span><span class="nb">+ </span><span class="p">(</span><span class="nf">total-weight-branch</span> <span class="nv">left</span><span class="p">)</span>
</span><span class='line'>         <span class="p">(</span><span class="nf">total-weight-branch</span> <span class="nv">right</span><span class="p">))))</span>
</span><span class='line'>      
</span><span class='line'><span class="p">(</span><span class="k">defn </span><span class="nv">total-weight-branch</span> <span class="p">[{</span><span class="nv">structure</span> <span class="nv">:structure</span><span class="p">}]</span>
</span><span class='line'>  <span class="p">(</span><span class="nf">total-weight</span> <span class="nv">structure</span><span class="p">))</span>     
</span></code></pre></td></tr></table></div></figure>


<p>For a language with no pattern matching nor algebraic data types, this snippet is clear, concise and elegant - and a real improvement
over the Scheme version discussed in the paper - which was essentially handicapped by the use of lists to simulate &#8216;structs&#8217;.</p>

<p>As far as Clojure goes, this claim ends here: the next point in the paper, about changing from using <code>list</code> to using <code>cons</code>, is rendered moot since
we&#8217;re using hash maps to represent our mobiles.</p>

<h3>Lisp lists are not self-escaping</h3>

<p>Creating lists in Clojure goes like this:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="nb">list </span><span class="p">(</span><span class="nb">list </span><span class="mi">1</span> <span class="mi">2</span><span class="p">)</span> <span class="nv">nil</span><span class="p">)</span> <span class="c1">;; ((1 2) nil)</span>
</span><span class='line'>
</span><span class='line'><span class="o">&#39;</span><span class="p">((</span><span class="mi">1</span> <span class="mi">2</span><span class="p">)</span> <span class="nv">nil</span><span class="p">)</span> <span class="c1">;; ((1 2) nil)</span>
</span></code></pre></td></tr></table></div></figure>


<p>Both statements above are equivalent, with the second one being clearly more concise.</p>

<p>The claim here is that the fact that you need to either use the <code>list</code> function or quote the form is cumbersome and can be confusing to beginners.</p>

<p>Clojure solves this by providing literals to another data structure - vectors:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">[[</span><span class="mi">1</span> <span class="mi">2</span><span class="p">]</span> <span class="nv">nil</span><span class="p">]</span>
</span></code></pre></td></tr></table></div></figure>


<p>Simple and concise - in fact, in idiomatic Clojure code, you&#8217;ll rarely see quoted lists where a vector will do.</p>

<p>This is possible because both lists and vectors conform to a higher level abstraction called a <a href="http://clojure.org/sequences#Sequences-The%20Seq%20library-Seq%20in,%20Seq%20out">Seq</a>, in terms of which most list
operations are defined.</p>

<p>This eliminates the two following points mentioned in the paper as it allows a beginner to defer his/her understanding of quoted forms
to more advanced lessons/usages.</p>

<h3>Programs that Manipulate Programs - the interpreter example</h3>

<p>Here Wadler shows a simple grammar for an interpreter in both Miranda and Scheme.</p>

<p>He claims that since Haskell/Miranda have free data types, representing such grammar becomes simpler:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
</pre></td><td class='code'><pre><code class='haskell'><span class='line'><span class="kr">data</span> <span class="kt">Term</span> <span class="ow">=</span> <span class="kt">Var</span> <span class="p">[</span><span class="kt">Char</span><span class="p">]</span>
</span><span class='line'>          <span class="o">|</span> <span class="kt">Lambda</span> <span class="kt">Var</span> <span class="kt">Term</span>
</span><span class='line'>          <span class="o">|</span> <span class="kt">Apply</span> <span class="kt">Term</span> <span class="kt">Term</span>
</span><span class='line'>          <span class="o">|</span> <span class="kt">Closure</span> <span class="kt">Env</span> <span class="kt">Var</span> <span class="kt">Term</span>
</span><span class='line'><span class="kr">type</span> <span class="kt">Env</span> <span class="ow">=</span> <span class="p">[(</span><span class="kt">Var</span><span class="p">,</span> <span class="kt">Term</span><span class="p">)]</span>
</span><span class='line'><span class="kr">type</span> <span class="kt">Var</span> <span class="ow">=</span> <span class="p">[</span><span class="kt">Char</span><span class="p">]</span>
</span></code></pre></td></tr></table></div></figure>


<p>This is true in that one can easily scan the snippet above and deduce quickly what <code>Term</code> looks like.</p>

<p>Then, by using pattern matching, <code>eval</code> could be implemented like so:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='haskell'><span class='line'><span class="nf">eval</span> <span class="n">e</span> <span class="p">(</span><span class="kt">Var</span> <span class="n">v</span><span class="p">)</span> <span class="ow">=</span> <span class="n">lookup</span> <span class="n">e</span> <span class="n">v</span>
</span><span class='line'><span class="nf">eval</span> <span class="n">e</span> <span class="p">(</span><span class="kt">Lambda</span> <span class="n">v</span> <span class="n">t</span><span class="p">)</span> <span class="ow">=</span> <span class="kt">Closure</span> <span class="n">e</span> <span class="n">v</span> <span class="n">t</span>
</span><span class='line'><span class="nf">eval</span> <span class="n">e</span> <span class="p">(</span><span class="kt">Apply</span> <span class="n">t0</span> <span class="n">t1</span><span class="p">)</span> <span class="ow">=</span> <span class="n">apply</span> <span class="p">(</span><span class="n">eval</span> <span class="n">e</span> <span class="n">t0</span><span class="p">)</span> <span class="p">(</span><span class="n">eval</span> <span class="n">e</span> <span class="n">t1</span><span class="p">)</span>
</span><span class='line'>
</span><span class='line'><span class="o">...</span>
</span></code></pre></td></tr></table></div></figure>


<p>I do believe this makes Haskell an excellent choice for writing interpreters and compilers.</p>

<p>However, the flip side is that entering such terms in Haskell is cumbersome. Consider the term below:</p>

<blockquote><p>(λx.(x x)) (λx.(x x))</p></blockquote>

<p>This is how to represent this term using the grammar defined above:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='haskell'><span class='line'><span class="p">(</span><span class="n">apply</span> <span class="p">(</span><span class="kt">Lambda</span> <span class="s">&quot;x&quot;</span> <span class="p">(</span><span class="n">apply</span> <span class="p">(</span><span class="kt">Var</span> <span class="s">&quot;x&quot;</span><span class="p">)</span> <span class="p">(</span><span class="kt">Var</span> <span class="s">&quot;x&quot;</span><span class="p">)))</span>
</span><span class='line'>       <span class="p">(</span><span class="kt">Lambda</span> <span class="s">&quot;x&quot;</span> <span class="p">(</span><span class="n">apply</span> <span class="p">(</span><span class="kt">Var</span> <span class="s">&quot;x&quot;</span><span class="p">)</span> <span class="p">(</span><span class="kt">Var</span> <span class="s">&quot;x&quot;</span><span class="p">))))</span>
</span></code></pre></td></tr></table></div></figure>


<p>The strength in Lisp lies elsewhere. Since we have quoted forms, entering a similar term is a lot less verbose and closer to its intended representation:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="o">&#39;</span><span class="p">((</span><span class="nf">lambda</span> <span class="p">[</span><span class="nv">x</span><span class="p">]</span> <span class="p">(</span><span class="nf">x</span> <span class="nv">x</span><span class="p">))</span> <span class="p">(</span><span class="nf">lambda</span> <span class="p">[</span><span class="nv">x</span><span class="p">]</span> <span class="p">(</span><span class="nf">x</span> <span class="nv">x</span><span class="p">)))</span>
</span></code></pre></td></tr></table></div></figure>


<p>This is, of course, at the expense of making <code>eval</code> a more complicated function:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="k">defn </span><span class="nv">eval</span> <span class="p">[</span><span class="nv">e</span> <span class="nv">t</span><span class="p">]</span>
</span><span class='line'>  <span class="p">(</span><span class="k">cond </span><span class="p">(</span><span class="nf">variable?</span> <span class="nv">t</span><span class="p">)</span>
</span><span class='line'>        <span class="p">(</span><span class="nf">lookup</span> <span class="nv">e</span> <span class="p">(</span><span class="nf">variable-name</span> <span class="nv">t</span><span class="p">))</span>
</span><span class='line'>        <span class="p">(</span><span class="nf">lambda?</span> <span class="nv">t</span><span class="p">)</span>
</span><span class='line'>        <span class="p">(</span><span class="nf">make-closure</span> <span class="nv">e</span> <span class="p">(</span><span class="nf">lambda-var</span> <span class="nv">t</span><span class="p">)</span> <span class="p">(</span><span class="nf">lambda-body</span> <span class="nv">t</span><span class="p">))</span>
</span><span class='line'>        <span class="p">(</span><span class="nf">apply?</span> <span class="nv">t</span><span class="p">)</span>
</span><span class='line'>        <span class="p">(</span><span class="nb">apply </span><span class="p">(</span><span class="nb">eval </span><span class="nv">e</span> <span class="p">(</span><span class="nf">apply-operator</span> <span class="nv">t</span><span class="p">))</span>
</span><span class='line'>               <span class="p">(</span><span class="nb">eval </span><span class="nv">e</span> <span class="p">(</span><span class="nf">apply-operand</span> <span class="nv">t</span><span class="p">)))))</span>
</span><span class='line'>
</span><span class='line'><span class="o">...</span>
</span></code></pre></td></tr></table></div></figure>


<p>The paper leaves out an important advantage of Lisps though:</p>

<p>Because we can write code for our made up language directly in its (almost)abstract syntax tree form, Lisps are the ideal choice when writing <a href="http://martinfowler.com/bliki/InternalDslStyle.html">Internal Domain Specific Languages</a>.</p>

<h3>Lazy Evaluation</h3>

<h4>Lists</h4>

<p>Haskell and Miranda are lazy languages and that yields a lot of power. This claim is more specific to the use of lazy lists - or sequences, streams - and starts off with a snippet that calculates the sum of squares of all odd numbers from 1 up to 100:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='haskell'><span class='line'><span class="nf">sum</span> <span class="p">[</span> <span class="n">i</span><span class="o">*</span><span class="n">i</span> <span class="o">|</span> <span class="n">i</span> <span class="ow">&lt;-</span> <span class="p">[</span><span class="mi">1</span><span class="o">..</span><span class="mi">100</span><span class="p">],</span> <span class="n">odd</span> <span class="n">i</span> <span class="p">]</span>
</span></code></pre></td></tr></table></div></figure>


<p>What follows in the paper is a not-so-clear snippet of equivalent functionality using Scheme streams.</p>

<p>Clojure features lazy sequences and list comprehensions, making the above Haskell example trivial to write:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="nf">sum</span> <span class="p">(</span><span class="k">for </span><span class="p">[</span><span class="nv">i</span> <span class="p">(</span><span class="nb">range </span><span class="mi">100</span><span class="p">)</span> <span class="nv">:when</span> <span class="p">(</span><span class="nf">odd?</span> <span class="nv">i</span><span class="p">)]</span> <span class="p">(</span><span class="nb">* </span><span class="nv">i</span> <span class="nv">i</span><span class="p">)))</span>
</span></code></pre></td></tr></table></div></figure>


<p>If you&#8217;re following at home with the original paper you&#8217;ll see this is more readable and elegant than the equivalent Scheme example.</p>

<p>Another - also idiomatic - way to write the same expression is by using a combination of map/filter:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="nf">sum</span> <span class="p">(</span><span class="nb">map </span><span class="o">#</span><span class="p">(</span><span class="nv">*</span> <span class="nv">%</span> <span class="nv">%</span><span class="p">)</span> <span class="p">(</span><span class="nb">filter </span><span class="nv">odd?</span> <span class="p">(</span><span class="nb">range </span><span class="mi">100</span><span class="p">))))</span>
</span></code></pre></td></tr></table></div></figure>


<p>Deciding which one is clearer is left as an exercise to the reader.</p>

<h4>Special forms and lazy evaluation</h4>

<p>In this section, Wadler brings another example from SICP where the reader wishes to implement his/her own <code>if</code> form.</p>

<p>As we know, in order to implement our own version of <code>if</code>, we need to use macros. That is because in Lisps arguments to functions are eagerly evaluated.</p>

<p>One might implement it like this:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="k">defmacro </span><span class="nv">my-if</span> <span class="p">[</span><span class="nv">pred</span> <span class="nv">then</span> <span class="nv">else</span><span class="p">]</span>
</span><span class='line'>  <span class="o">`</span><span class="p">(</span><span class="k">cond </span><span class="nv">~pred</span> <span class="nv">~then</span>
</span><span class='line'>          <span class="nv">:else</span> <span class="nv">~else</span><span class="p">))</span>
</span></code></pre></td></tr></table></div></figure>


<p>In Lazy languages, such as Haskell and Miranda, this problem doesn&#8217;t occur allowing such functions to be defined without the need for special and/or quoted forms:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='haskell'><span class='line'><span class="nf">myIf</span> <span class="kt">True</span>  <span class="n">t</span> <span class="n">e</span> <span class="ow">=</span> <span class="n">t</span>
</span><span class='line'><span class="nf">myIf</span> <span class="kt">False</span> <span class="n">t</span> <span class="n">e</span> <span class="ow">=</span> <span class="n">e</span>
</span></code></pre></td></tr></table></div></figure>


<p>However this completely dismisses the power of macros which allow you to extend the language in ways no other language allows - as is extensively demonstrated in books such as <a href="http://amzn.to/14mrrbk">On Lisp</a> and <a href="http://amzn.to/WKpMZA">Let Over Lambda</a>.</p>

<p>As <a href="http://en.wikipedia.org/wiki/Guy_L._Steele,_Jr.">Guy Steele</a> once put it:  <em>&#8221;[…] If you give someone Lisp, he has any language he pleases&#8221;</em></p>

<h3>Conclusion</h3>

<p>Hopefully this post doesn&#8217;t come off as trying to invalidate Wadler&#8217;s paper - that is not my intention.</p>

<p>While I do think a few of the points discussed are only applicable to the domain in which his paper was written - teaching - they are still valid and worth understanding.</p>

<p>I do however expect to have given you a different perspective on it, showing the strengths of modern Lisps such as Clojure and how it approaches these issues - such as by using its rich set of data structures, literals and techniques such as destructuring.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Purely functional data structures in Clojure: Leftist Heaps]]></title>
    <link href="http://www.leonardoborges.com/writings/2013/02/03/purely-functional-data-structures-in-clojure-leftist-heaps/"/>
    <updated>2013-02-03T12:41:00+11:00</updated>
    <id>http://www.leonardoborges.com/writings/2013/02/03/purely-functional-data-structures-in-clojure-leftist-heaps</id>
    <content type="html"><![CDATA[<blockquote><p>This post is part of a series about Chris Okasaki&#8217;s <a href="http://amzn.to/UcIidh">Purely Functional Data Structures</a>. You can see all posts in the series by visiting the <a href="http://www.leonardoborges.com/writings/tags/functional-data-structures/">functional-data-structures</a> category in this blog.</p></blockquote>

<hr />

<p>Last year I started reading a book called <a href="http://amzn.to/UcIidh">Purely Functional Data Structures</a>. It&#8217;s a fascinating book and if you&#8217;ve ever wondered how Clojure&#8217;s persistent data structures work, it&#8217;s mandatory reading.</p>

<p>However, all code samples in the book are written in <a href="http://bit.ly/YqYjtt">ML</a> - with <a href="http://bit.ly/YqYmp6">Haskell</a> versions in the end of the book. This means I got stuck in Chapter 3, where the ML snippets start.</p>

<p>I had no clue about Haskell&#8217;s - much less ML&#8217;s! - syntax and I was finding it very difficult to follow along. What I did notice is that their syntaxes are not so different from each other.</p>

<p>So I put the book down and read <a href="http://amzn.to/VuD3jT">Learn You a Haskell For Great Good!</a> with the hopes that learning more about haskell&#8217;s syntax - in particular, learning how to read its type signatures - would help me get going with <em>Puretly Functional Data Structures</em>.</p>

<p>Luckily, I was right - and I recommend you do the same if you&#8217;re not familiar with either of those languages. <a href="http://amzn.to/VuD3jT">Learn You a Haskell For Great Good!</a> is a great book and I got a lot out of it. <a href="http://www.leonardoborges.com/writings/2012/11/30/monads-in-small-bites-part-i-functors/">My series on Monads</a> is a product of reading it.</p>

<p>Enough background though.</p>

<p>The purpose of this post is two-fold: One is to share the <a href="https://github.com/leonardoborges/purely-functional-data-structures">github repository</a> I created and that will contain the Clojure versions of the data structures in the book as well as most solutions to the exercises - or at least as many as my time-poor life allows me to implement.</p>

<p>The other is to walk you through some of the code and get a discussion going. Hopefully we will all learn something - as I certainly have when implementing these. Today, we&#8217;ll start with Leftist Heaps.</p>

<h3>Leftist Heaps</h3>

<p><a href="http://en.wikipedia.org/wiki/Leftist_tree">Leftist Heaps</a> - or trees - are a variant of <a href="http://en.wikipedia.org/wiki/Binary_heap">binary heaps</a> that can be used as priority queues. On top of the standard invariants of binary heaps, it obeys the leftist property:</p>

<ul>
<li>Every node has a <em>rank</em>, which is the distance from its right spine to the nearest leaf</li>
<li>A node&#8217;s left child has a rank at least as large as its right child</li>
</ul>


<p>In a nutshell, these are the operations we need to be able to perform on a leftist heap:</p>

<ul>
<li>insert a value into an existing heap</li>
<li>merge two heaps</li>
<li>find the minimum value in a heap</li>
<li>delete the minimum value, returning a new heap</li>
</ul>


<p>Since the book uses ML/Haskell, it starts with a data type definition for Heaps that exposes these and a couple of other auxiliary functions. I decided to take a stab at writing the solution using Clojure&#8217;s protocols and records:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="nf">defprotocol</span> <span class="nv">Heap</span>
</span><span class='line'>  <span class="p">(</span><span class="nf">is-empty?</span>   <span class="p">[</span><span class="nv">this</span><span class="p">])</span>
</span><span class='line'>  <span class="p">(</span><span class="nf">insert</span>      <span class="p">[</span><span class="nv">this</span> <span class="nv">v</span><span class="p">])</span>
</span><span class='line'>  <span class="p">(</span><span class="nb">merge </span>      <span class="p">[</span><span class="nv">this</span> <span class="nv">other</span><span class="p">])</span>
</span><span class='line'>  <span class="p">(</span><span class="nf">rank</span>        <span class="p">[</span><span class="nv">this</span><span class="p">])</span>
</span><span class='line'>  <span class="p">(</span><span class="nf">find-min</span>    <span class="p">[</span><span class="nv">this</span><span class="p">])</span>
</span><span class='line'>  <span class="p">(</span><span class="nf">delete-min</span>  <span class="p">[</span><span class="nv">this</span><span class="p">]))</span>
</span><span class='line'>
</span><span class='line'><span class="p">(</span><span class="nf">defrecord</span> <span class="nv">LeftistHeap</span> <span class="p">[</span><span class="nv">rank</span> <span class="nv">value</span> <span class="nv">left</span> <span class="nv">right</span><span class="p">])</span>
</span></code></pre></td></tr></table></div></figure>


<p></p>

<p>When implementing the algorithms the base case for the recursive solutions will involve dealing with <em>nil</em> values which at first seems like it wouldn&#8217;t be a problem. However, protocol functions dispatch on the type of its first argument so what happens if I call the function <em>is-empty?</em> on <em>nil</em>?</p>

<p>Luckily, Clojure allows us to extend a protocol to core types:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
<span class='line-number'>31</span>
<span class='line-number'>32</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="nf">extend-protocol</span> <span class="nv">Heap</span>
</span><span class='line'>  <span class="nv">nil</span>
</span><span class='line'>  <span class="p">(</span><span class="nf">rank</span> <span class="p">[</span><span class="nv">_</span><span class="p">]</span> <span class="mi">0</span><span class="p">)</span>
</span><span class='line'>  <span class="p">(</span><span class="nb">merge </span><span class="p">[</span><span class="nv">_</span> <span class="nv">other</span><span class="p">]</span> <span class="nv">other</span><span class="p">)</span>
</span><span class='line'>  <span class="p">(</span><span class="nf">is-empty?</span> <span class="p">[</span><span class="nv">_</span><span class="p">]</span> <span class="nv">true</span><span class="p">)</span>
</span><span class='line'>
</span><span class='line'>  <span class="nv">LeftistHeap</span>
</span><span class='line'>  <span class="p">(</span><span class="nf">is-empty?</span> <span class="p">[</span><span class="nv">this</span><span class="p">]</span>
</span><span class='line'>    <span class="p">(</span><span class="nb">nil? </span><span class="nv">this</span><span class="p">))</span>
</span><span class='line'>
</span><span class='line'>  <span class="p">(</span><span class="nf">rank</span> <span class="p">[</span><span class="nv">this</span><span class="p">]</span>
</span><span class='line'>    <span class="p">(</span><span class="nf">:rank</span> <span class="nv">this</span><span class="p">))</span>
</span><span class='line'>
</span><span class='line'>  <span class="p">(</span><span class="nb">merge </span><span class="p">[{</span><span class="nv">val-this</span> <span class="nv">:value</span> <span class="nv">left-this</span> <span class="nv">:left</span> <span class="nv">right-this</span> <span class="nv">:right</span> <span class="nv">:as</span> <span class="nv">this</span><span class="p">}</span>
</span><span class='line'>          <span class="p">{</span><span class="nv">val-other</span> <span class="nv">:value</span> <span class="nv">left-other</span> <span class="nv">:left</span> <span class="nv">right-other</span> <span class="nv">:right</span> <span class="nv">:as</span> <span class="nv">other</span><span class="p">}]</span>
</span><span class='line'>    <span class="p">(</span><span class="nf">cond</span>
</span><span class='line'>     <span class="p">(</span><span class="nf">is-empty?</span> <span class="nv">other</span><span class="p">)</span> <span class="nv">this</span>
</span><span class='line'>     <span class="p">(</span><span class="nb">&lt;= </span><span class="nv">val-this</span> <span class="nv">val-other</span><span class="p">)</span> <span class="p">(</span><span class="nf">ensure-leftist</span> <span class="nv">left-this</span>
</span><span class='line'>                                             <span class="p">(</span><span class="nb">merge </span><span class="nv">right-this</span> <span class="nv">other</span><span class="p">)</span>
</span><span class='line'>                                             <span class="nv">val-this</span><span class="p">)</span>
</span><span class='line'>     <span class="nv">:else</span> <span class="p">(</span><span class="nf">ensure-leftist</span> <span class="nv">left-other</span>
</span><span class='line'>                           <span class="p">(</span><span class="nb">merge </span><span class="nv">this</span> <span class="nv">right-other</span><span class="p">)</span>
</span><span class='line'>                           <span class="nv">val-other</span><span class="p">)))</span>
</span><span class='line'>
</span><span class='line'>  <span class="p">(</span><span class="nf">insert</span> <span class="p">[</span><span class="nv">this</span> <span class="nv">v</span><span class="p">]</span>
</span><span class='line'>    <span class="p">(</span><span class="nb">merge </span><span class="p">(</span><span class="nf">-&gt;LeftistHeap</span> <span class="mi">1</span> <span class="nv">v</span> <span class="nv">nil</span> <span class="nv">nil</span><span class="p">)</span>
</span><span class='line'>           <span class="nv">this</span><span class="p">))</span>
</span><span class='line'>
</span><span class='line'>  <span class="p">(</span><span class="nf">find-min</span> <span class="p">[{</span><span class="nv">v</span> <span class="nv">:value</span><span class="p">}]</span> <span class="nv">v</span><span class="p">)</span>
</span><span class='line'>
</span><span class='line'>  <span class="p">(</span><span class="nf">delete-min</span> <span class="p">[{</span><span class="nv">left</span> <span class="nv">:left</span> <span class="nv">right</span> <span class="nv">:right</span><span class="p">}]</span>
</span><span class='line'>    <span class="p">(</span><span class="nb">merge </span><span class="nv">right</span> <span class="nv">left</span><span class="p">)))</span>
</span></code></pre></td></tr></table></div></figure>


<p>Note how I extended a few of the protocol functions to the nil data type, allowing me to continue with this implementation with no nasty hacks.</p>

<p>There&#8217;s one last bit missing: a function that will ensure each heap retains the leftist property:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="k">defn </span><span class="nv">ensure-leftist</span>
</span><span class='line'> <span class="p">[</span><span class="nv">this</span> <span class="nv">other</span> <span class="nv">v</span><span class="p">]</span>
</span><span class='line'> <span class="p">(</span><span class="k">let </span><span class="p">[</span><span class="nv">rank-this</span> <span class="p">(</span><span class="nf">rank</span> <span class="nv">this</span><span class="p">)</span>
</span><span class='line'>       <span class="nv">rank-other</span> <span class="p">(</span><span class="nf">rank</span> <span class="nv">other</span><span class="p">)]</span>
</span><span class='line'>   <span class="p">(</span><span class="k">if </span><span class="p">(</span><span class="nb">&gt;= </span><span class="nv">rank-this</span> <span class="nv">rank-other</span><span class="p">)</span>
</span><span class='line'>     <span class="p">(</span><span class="nf">-&gt;LeftistHeap</span> <span class="p">(</span><span class="nb">inc </span><span class="nv">rank-other</span><span class="p">)</span> <span class="nv">v</span> <span class="nv">this</span> <span class="nv">other</span><span class="p">)</span>
</span><span class='line'>     <span class="p">(</span><span class="nf">-&gt;LeftistHeap</span> <span class="p">(</span><span class="nb">inc </span><span class="nv">rank-this</span><span class="p">)</span> <span class="nv">v</span> <span class="nv">other</span> <span class="nv">this</span><span class="p">))))</span>
</span></code></pre></td></tr></table></div></figure>


<p>The reason this function is isolated is that the Heap protocol defined above is fairly generic and could be used for defining other types of heaps - and I didn&#8217;t feel it warranted its own interface.</p>

<p>We can now play with it and create a new leftist heap:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="nb">-&gt; </span><span class="p">(</span><span class="nf">-&gt;LeftistHeap</span> <span class="mi">1</span> <span class="mi">3</span> <span class="nv">nil</span> <span class="nv">nil</span><span class="p">)</span>
</span><span class='line'>                   <span class="p">(</span><span class="nf">insert</span> <span class="mi">2</span><span class="p">)</span>
</span><span class='line'>                   <span class="p">(</span><span class="nf">insert</span> <span class="mi">7</span><span class="p">)</span>
</span><span class='line'>                   <span class="p">(</span><span class="nf">insert</span> <span class="mi">4</span><span class="p">)</span>
</span><span class='line'>                   <span class="p">(</span><span class="nf">insert</span> <span class="mi">10</span><span class="p">)</span>
</span><span class='line'>                   <span class="p">(</span><span class="nf">insert</span> <span class="mi">1</span><span class="p">)</span>
</span><span class='line'>                   <span class="p">(</span><span class="nf">insert</span> <span class="mi">20</span><span class="p">))</span>
</span></code></pre></td></tr></table></div></figure>


<p>While I quite like this approach, I thought I&#8217;d also implement this solution using Clojure&#8217;s core data types - maps in this case - and no protocols. The code is shown below:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
<span class='line-number'>31</span>
<span class='line-number'>32</span>
<span class='line-number'>33</span>
<span class='line-number'>34</span>
<span class='line-number'>35</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="k">defn </span><span class="nv">mk-heap</span> <span class="p">[</span><span class="nv">rank</span> <span class="nv">value</span> <span class="nv">left</span> <span class="nv">right</span><span class="p">]</span>
</span><span class='line'>  <span class="p">{</span><span class="nv">:rank</span> <span class="nv">rank</span> <span class="nv">:value</span> <span class="nv">value</span> <span class="nv">:left</span> <span class="nv">left</span> <span class="nv">:right</span> <span class="nv">right</span><span class="p">})</span>
</span><span class='line'>
</span><span class='line'><span class="p">(</span><span class="k">defn </span><span class="nv">heap-rank</span> <span class="p">[</span><span class="nv">heap</span><span class="p">]</span>
</span><span class='line'>  <span class="p">(</span><span class="k">if </span><span class="p">(</span><span class="nb">nil? </span><span class="nv">heap</span><span class="p">)</span>
</span><span class='line'>    <span class="mi">0</span>
</span><span class='line'>    <span class="p">(</span><span class="nf">:rank</span> <span class="nv">heap</span><span class="p">)))</span>
</span><span class='line'>
</span><span class='line'><span class="p">(</span><span class="k">defn </span><span class="nv">ensure-leftist-heap</span> <span class="p">[</span><span class="nv">value</span> <span class="nv">heap-a</span> <span class="nv">heap-b</span><span class="p">]</span>
</span><span class='line'>  <span class="p">(</span><span class="k">let </span><span class="p">[</span><span class="nv">rank-a</span> <span class="p">(</span><span class="nf">heap-rank</span> <span class="nv">heap-a</span><span class="p">)</span>
</span><span class='line'>        <span class="nv">rank-b</span> <span class="p">(</span><span class="nf">heap-rank</span> <span class="nv">heap-b</span><span class="p">)]</span>
</span><span class='line'>    <span class="p">(</span><span class="k">if </span><span class="p">(</span><span class="nb">&gt;= </span><span class="nv">rank-a</span> <span class="nv">rank-b</span><span class="p">)</span>
</span><span class='line'>      <span class="p">(</span><span class="nf">mk-heap</span> <span class="p">(</span><span class="nb">inc </span><span class="nv">rank-b</span><span class="p">)</span> <span class="nv">value</span> <span class="nv">heap-a</span> <span class="nv">heap-b</span><span class="p">)</span>
</span><span class='line'>      <span class="p">(</span><span class="nf">mk-heap</span> <span class="p">(</span><span class="nb">inc </span><span class="nv">rank-a</span><span class="p">)</span> <span class="nv">value</span> <span class="nv">heap-b</span> <span class="nv">heap-a</span><span class="p">))))</span>
</span><span class='line'>
</span><span class='line'><span class="p">(</span><span class="k">defn </span><span class="nv">merge-heaps</span> <span class="p">[{</span><span class="nv">val-a</span> <span class="nv">:value</span> <span class="nv">left-a</span> <span class="nv">:left</span> <span class="nv">right-a</span> <span class="nv">:right</span> <span class="nv">:as</span> <span class="nv">heap-a</span><span class="p">}</span>
</span><span class='line'>                   <span class="p">{</span><span class="nv">val-b</span> <span class="nv">:value</span> <span class="nv">left-b</span> <span class="nv">:left</span> <span class="nv">right-b</span> <span class="nv">:right</span> <span class="nv">:as</span> <span class="nv">heap-b</span><span class="p">}]</span>
</span><span class='line'>  <span class="p">(</span><span class="nf">cond</span>
</span><span class='line'>   <span class="p">(</span><span class="nb">nil? </span><span class="nv">heap-a</span><span class="p">)</span> <span class="nv">heap-b</span>
</span><span class='line'>   <span class="p">(</span><span class="nb">nil? </span><span class="nv">heap-b</span><span class="p">)</span> <span class="nv">heap-a</span>
</span><span class='line'>   <span class="p">(</span><span class="nb">&lt;= </span><span class="nv">val-a</span> <span class="nv">val-b</span><span class="p">)</span> <span class="p">(</span><span class="nf">ensure-leftist-heap</span> <span class="nv">val-a</span>
</span><span class='line'>                                         <span class="nv">left-a</span>
</span><span class='line'>                                         <span class="p">(</span><span class="nf">merge-heaps</span> <span class="nv">right-a</span> <span class="nv">heap-b</span><span class="p">))</span>
</span><span class='line'>   <span class="nv">:else</span> <span class="p">(</span><span class="nf">ensure-leftist-heap</span> <span class="nv">val-b</span>
</span><span class='line'>                              <span class="nv">left-b</span>
</span><span class='line'>                              <span class="p">(</span><span class="nf">merge-heaps</span> <span class="nv">heap-a</span> <span class="nv">right-b</span><span class="p">))))</span>
</span><span class='line'>
</span><span class='line'><span class="p">(</span><span class="k">defn </span><span class="nv">heap-insert</span> <span class="p">[</span><span class="nv">value</span> <span class="nv">heap</span><span class="p">]</span>
</span><span class='line'>  <span class="p">(</span><span class="nf">merge-heaps</span> <span class="p">(</span><span class="nf">mk-heap</span> <span class="mi">1</span> <span class="nv">value</span> <span class="nv">nil</span> <span class="nv">nil</span><span class="p">)</span>
</span><span class='line'>               <span class="nv">heap</span><span class="p">))</span>
</span><span class='line'>
</span><span class='line'><span class="p">(</span><span class="k">defn </span><span class="nv">heap-find-min</span> <span class="p">[{</span><span class="nv">v</span> <span class="nv">:value</span><span class="p">}]</span> <span class="nv">v</span><span class="p">)</span>
</span><span class='line'>
</span><span class='line'><span class="p">(</span><span class="k">defn </span><span class="nv">heap-delete-min</span> <span class="p">[{</span><span class="nv">left</span> <span class="nv">:left</span> <span class="nv">right</span> <span class="nv">:right</span><span class="p">}]</span>
</span><span class='line'>  <span class="p">(</span><span class="nf">merge-heaps</span> <span class="nv">right</span> <span class="nv">left</span><span class="p">))</span>
</span></code></pre></td></tr></table></div></figure>


<p>Using it is equally simple:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="nf">-&gt;&gt;</span> <span class="p">(</span><span class="nf">mk-heap</span> <span class="mi">1</span> <span class="mi">3</span> <span class="nv">nil</span> <span class="nv">nil</span><span class="p">)</span>
</span><span class='line'>                    <span class="p">(</span><span class="nf">heap-insert</span> <span class="mi">2</span><span class="p">)</span>
</span><span class='line'>                    <span class="p">(</span><span class="nf">heap-insert</span> <span class="mi">7</span><span class="p">)</span>
</span><span class='line'>                    <span class="p">(</span><span class="nf">heap-insert</span> <span class="mi">4</span><span class="p">)</span>
</span><span class='line'>                    <span class="p">(</span><span class="nf">heap-insert</span> <span class="mi">10</span><span class="p">)</span>
</span><span class='line'>                    <span class="p">(</span><span class="nf">heap-insert</span> <span class="mi">1</span><span class="p">)</span>
</span><span class='line'>                    <span class="p">(</span><span class="nf">heap-insert</span> <span class="mi">20</span><span class="p">))</span>
</span></code></pre></td></tr></table></div></figure>


<p>That&#8217;s it for now.</p>

<p>As I implement more of the book&#8217;s code and exercises I&#8217;ll add them to the <a href="https://github.com/leonardoborges/purely-functional-data-structures">github repo</a> - it also includes tests for all implementations.</p>

<p>Enjoy :)</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Announcing bouncer, a validation library for Clojure Apps]]></title>
    <link href="http://www.leonardoborges.com/writings/2013/01/04/bouncer-validation-lib-for-clojure/"/>
    <updated>2013-01-04T17:39:00+11:00</updated>
    <id>http://www.leonardoborges.com/writings/2013/01/04/bouncer-validation-lib-for-clojure</id>
    <content type="html"><![CDATA[<p>Today I&#8217;m releasing bouncer, which was extracted from a project I&#8217;ve been working on.</p>

<p>It&#8217;s a validation library for Clojure apps and it lets you write code like this:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="k">def </span><span class="nv">person</span> <span class="p">{</span><span class="nv">:name</span> <span class="s">&quot;Leo&quot;</span><span class="p">})</span>
</span><span class='line'>
</span><span class='line'><span class="p">(</span><span class="nf">validate</span> <span class="nv">person</span>
</span><span class='line'>    <span class="nv">:name</span> <span class="nv">required</span>
</span><span class='line'>    <span class="nv">:age</span>  <span class="p">[</span><span class="nv">required</span> <span class="nv">number</span><span class="p">])</span>
</span></code></pre></td></tr></table></div></figure>


<p>If you&#8217;d like to see more examples and a detailed guide check out the <a href="http://github.com/leonardoborges/bouncer">github repository</a>. The README should get you started.</p>

<p>This post however isn&#8217;t only about announcing bouncer. It&#8217;s also about the motivation and implementation details behind it.</p>

<p>There are a couple of Clojure validation libraries already out there so why would I write a new one?
Well&#8230;</p>

<ul>
<li><p>Writing Clojure is fun! (who knew? :P)</p></li>
<li><p>Because I believe this problem can be solved more elegantly with the use of Monads</p></li>
</ul>


<p>If you&#8217;ve been following me for a while, you&#8217;ll know that I spent most of 2012 deepening my knowledge about functional programming.</p>

<p>In that journey, the unavoidable subject of monads came about - and it was both interesting and enlightening enough that made me <a href="http://www.leonardoborges.com/writings/2012/11/30/monads-in-small-bites-part-i-functors/">write a whole series of posts about it</a>.</p>

<p>After learning what they are and then thinking about the validation problem for a while, I couldn&#8217;t help but notice that the problem had a lot in common with the <a href="http://www.haskell.org/haskellwiki/State_Monad">State Monad</a>.</p>

<p>In order to explain how the two relate, I&#8217;ll have to digress for a moment. It&#8217;ll all make sense in the end - or so I hope</p>

<h2>Purity</h2>

<p>In pure functional languages, such as Haskell, functions can&#8217;t have side effects. These include performing IO, changing global variables or launching missiles.</p>

<p>Because of that, functions in Haskell are pure: if you repeatedly call a function <em>f</em> with the same argument <code>x</code> over time you will <em>always</em> get the same result back.</p>

<p>Pure functions are not a feature of Haskell though. We, too, can write pure functions if we wish:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="k">defn </span><span class="nv">double</span> <span class="p">[</span><span class="nv">x</span><span class="p">]</span>
</span><span class='line'>    <span class="p">(</span><span class="nb">+ </span><span class="nv">x</span> <span class="nv">x</span><span class="p">))</span>
</span></code></pre></td></tr></table></div></figure>


<p>The function <code>double</code> above is pure. If we call it with 10, we can be sure the result will always be 20.</p>

<p>This leaves us with a question though: If we were to write our programs with pure functions only, how would we perform computations that need to carry state - state that needs to change over time - around?</p>

<p>A good example of such computation is generating random numbers.</p>

<p>Most programming languages provide generators capable of creating random numbers on demand. Using them is usually trivial. Here&#8217;s an example in Java:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='java'><span class='line'><span class="n">Random</span> <span class="n">gen</span> <span class="o">=</span> <span class="k">new</span> <span class="n">Random</span><span class="o">();</span>
</span><span class='line'>
</span><span class='line'><span class="n">gen</span><span class="o">.</span><span class="na">nextDouble</span><span class="o">();</span> <span class="c1">// 0.0037635726242281065</span>
</span><span class='line'><span class="n">gen</span><span class="o">.</span><span class="na">nextDouble</span><span class="o">();</span> <span class="c1">// 0.15821091918430885</span>
</span></code></pre></td></tr></table></div></figure>


<p><strong>Impurity alert!</strong></p>

<p>The function <code>nextDouble</code> above is obviously <em>not</em> pure. Multiple invocations of it with the same argument - in this case, none - returns different results.</p>

<p><code>nextDouble</code>  is keeping some sort of global state between function calls.</p>

<p>This is where the State Monad comes in. It allows such functions to remain pure.</p>

<h2>The State Monad</h2>

<p>The State Monad provides a way to abstract <em>state</em> from the function that needs to operate on it.</p>

<p>Sounds confusing? Hopefully an example will clear things up.</p>

<p>Let&#8217;s have a look at the clojure function <code>rand</code>:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="nf">rand</span><span class="p">)</span>
</span><span class='line'><span class="c1">;; 0.04388682005715605</span>
</span><span class='line'><span class="p">(</span><span class="nf">rand</span><span class="p">)</span>
</span><span class='line'><span class="c1">;; 0.43057496371080517</span>
</span></code></pre></td></tr></table></div></figure>


<p><code>rand</code> suffers from the same problem as <code>nextDouble</code> we saw above. It keeps it&#8217;s own state that is shared across calls, therefore being an impure function.</p>

<p>Now let&#8217;s write a pure version of <code>rand</code>. We&#8217;ll call it <code>pure-rand</code>:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="k">def </span><span class="nv">gen</span> <span class="p">(</span><span class="nf">java</span><span class="o">.</span><span class="nv">util</span><span class="o">.</span><span class="nv">Random</span><span class="o">.</span><span class="p">))</span>
</span><span class='line'>
</span><span class='line'><span class="p">(</span><span class="k">defn </span><span class="nv">pure-rand</span> <span class="p">[</span><span class="nv">g</span><span class="p">]</span>
</span><span class='line'>    <span class="p">[(</span><span class="o">.</span><span class="nv">nextDouble</span> <span class="nv">g</span><span class="p">)</span> <span class="nv">g</span><span class="p">])</span>
</span><span class='line'>
</span><span class='line'><span class="p">(</span><span class="nf">pure-rand</span> <span class="nv">gen</span><span class="p">)</span>
</span><span class='line'><span class="c1">;; [0.5783608063218478 #&lt;Random java.util.Random@7f30ab6&gt;]</span>
</span><span class='line'>
</span><span class='line'><span class="p">(</span><span class="nf">pure-rand</span> <span class="nv">gen</span><span class="p">)</span>
</span><span class='line'><span class="c1">;; [0.9251968987499839 #&lt;Random java.util.Random@7f30ab6&gt;]</span>
</span></code></pre></td></tr></table></div></figure>


<p>This is interesting. <code>pure-rand</code> now takes a generator as an argument and returns a two-element vector containing the random number itself - the result we&#8217;re actually interested in - and the generator that was passed in.</p>

<p>Recall however that the generator returned, albeit the same object, is in a new sate.</p>

<p>By re-writing the function like this, we&#8217;ve regained purity, as demonstrated below:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="nf">pure-rand</span> <span class="p">(</span><span class="nf">java</span><span class="o">.</span><span class="nv">util</span><span class="o">.</span><span class="nv">Random</span><span class="o">.</span> <span class="mi">100</span><span class="p">))</span>
</span><span class='line'><span class="c1">;; [0.7220096548596434 #&lt;Random java.util.Random@bb14fe1&gt;]</span>
</span><span class='line'>
</span><span class='line'><span class="p">(</span><span class="nf">pure-rand</span> <span class="p">(</span><span class="nf">java</span><span class="o">.</span><span class="nv">util</span><span class="o">.</span><span class="nv">Random</span><span class="o">.</span> <span class="mi">100</span><span class="p">))</span>
</span><span class='line'><span class="c1">;; [0.7220096548596434 #&lt;Random java.util.Random@bb14fe1&gt;]</span>
</span></code></pre></td></tr></table></div></figure>


<p>As you can see, as long as we provide the same generator - the same argument - we get the same result back.</p>

<p>If you&#8217;re wondering why I&#8217;m returning a two element vector from our little function, the answer lies in the State Monad implementation as found in the <a href="https://github.com/clojure/algo.monads/">algo.monads</a> library.</p>

<p>From its docstring:</p>

<blockquote><p><strong>State Monad</strong>: Monad describing stateful computations. The monadic values have the structure:</p>

<pre><code>(fn [old-state] [result new-state]).
</code></pre></blockquote>

<p>It expects a function that receives its old state and returns the result and the new state - these are called monadic values.</p>

<p>By designing the function to follow this contract, we can leverage the <code>domonad</code> macro - think of it as syntactic sugar for working with monads:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="nf">require</span> <span class="o">&#39;</span><span class="p">[</span><span class="nv">clojure</span><span class="o">.</span><span class="nv">algo</span><span class="o">.</span><span class="nv">monads</span> <span class="nv">:as</span> <span class="nv">m</span><span class="p">])</span>
</span><span class='line'>
</span><span class='line'><span class="p">((</span><span class="nf">m/domonad</span> <span class="nv">m/state-m</span>
</span><span class='line'>    <span class="p">[</span><span class="nv">a</span> <span class="nv">pure-rand</span>
</span><span class='line'>     <span class="nv">b</span> <span class="nv">pure-rand</span>
</span><span class='line'>     <span class="nv">c</span> <span class="nv">pure-rand</span><span class="p">]</span>
</span><span class='line'>     <span class="p">[</span><span class="nv">a</span> <span class="nv">b</span> <span class="nv">c</span><span class="p">])</span>
</span><span class='line'>     <span class="p">(</span><span class="nf">java</span><span class="o">.</span><span class="nv">util</span><span class="o">.</span><span class="nv">Random</span><span class="o">.</span> <span class="mi">100</span><span class="p">))</span>
</span><span class='line'>
</span><span class='line'><span class="c1">;; [[0.7220096548596434 0.19497605734770518 0.6671595726539502] </span>
</span><span class='line'><span class="c1">;; #&lt;Random java.util.Random@358ddfd6&gt;]</span>
</span></code></pre></td></tr></table></div></figure>


<p>In the example above, we&#8217;re using our <code>pure-rand</code> function in the context of the State Monad to generate 3 random numbers - based on some initial state - and returning them as a vector.</p>

<p>As we&#8217;ve seen before the result is itself in a vector alongside the new state.</p>

<p>This is where the State Monad and the validation problem meet:</p>

<p>In <a href="http://github.com/leonardoborges/bouncer">bouncer</a>, each validation function is designed to be compatible with the State Monad, just like <code>pure-rand</code> above:</p>

<p>It receives an initial state - at first, the map to be validated - and returns a vector with the map of errors and the new state: the original map augmented with any errors from previous validators.</p>

<p>The end result, should one or more validations fail, is a map with all errors that might have happened, plus our new state.</p>

<p>Now if you head to the <a href="http://github.com/leonardoborges/bouncer">github repository</a> and read the examples by keeping the State Monad and the above explanation in mind, the similarities should be obvious.</p>

<h2>Wrapping up</h2>

<p>As I mentioned in the beginning of the article, there are <a href="https://github.com/r0man/validation-clj">other</a> validation <a href="https://github.com/michaelklishin/validateur">libraries</a> for Clojure and at the time of this writing they have more features than <a href="http://github.com/leonardoborges/bouncer">bouncer</a> - by all means have a look at them.</p>

<p>However I will keep maintaining <a href="http://github.com/leonardoborges/bouncer">bouncer</a> for a couple of reasons:</p>

<ul>
<li>That&#8217;s what I&#8217;m using in my current side project</li>
<li>It takes a fundamentally different implementation approach that is in itself worthy of exploration</li>
<li>If nothing else, this is yet another example of where Monads can be useful.</li>
</ul>


<h4>Acknowledgments</h4>

<p>Thanks to <a href="https://twitter.com/stevebuik">Steve</a> and <a href="https://twitter.com/juliansgamble">Julian</a> for reviewing early drafts of this post as well as <a href="https://twitter.com/nick_s_drew">Nick</a> for being such a PITA :) - our discussions led to a considerably nicer design.</p>

<p>As usual, let me know your thoughts.</p>
]]></content>
  </entry>
  
</feed>
