<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="/stylesheets/rss.css" type="text/css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Aaron Feng: List comprehension kicks ass</title>
    <link>http://www.aaronfeng.com/articles/2007/09/28/list-comprehension-kicks-ass</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>Adventures in software development</description>
    <item>
      <title>List comprehension kicks ass</title>
      <description>&lt;p&gt;Recently I'm on an &lt;a href="http://www.erlang.org/"&gt;Erlang&lt;/a&gt; high, so I have tried to play around with it as much as I can.  It's very common for any application to create a new list based on an existing list.  For example in C# you would do something like the following:&lt;/p&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;public List&amp;lt;string&amp;gt; QualifiedUserNames(List&amp;lt;User&amp;gt; users) {
  List&amp;lt;string&amp;gt; names = new List&amp;lt;string&amp;gt;();
  foreach(User user in users) {
    if(user.Age &amp;gt;= 30) {
      names.Add(user.Name);
    }
  }
  return names;
}&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Equivalent code in Erlang:&lt;/p&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;QualifiedUserNames(Users) -&amp;gt; [Name || {user,{name,Name},{age, Age}} &amp;lt;- Users, Age &amp;gt;= 30]&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The Erlang function uses list comprehension to do all the dirty work.  It
loops through every item in the Users list, and extracts only user "type" which matches the pattern {user,{name,Name},{age,Age}}.  This is done because Erlang is a dynamic language, and the list doesn't have to contain heterogeneous items.  Age &gt;= 30 is a predicate that checks if the user should be added to the newly created list and if so, the Name is added.&lt;/p&gt;

&lt;p&gt;Pretty cool, right?  I think so.  This capability is one of the many reasons why Erlang program is usually shorter than programs written in other languages.  Well back to programming in Erlang some more.&lt;/p&gt;</description>
      <pubDate>Fri, 28 Sep 2007 22:50:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:a9fc9fcc-de25-430f-bf0d-b8e800042036</guid>
      <author>Aaron Feng</author>
      <link>http://www.aaronfeng.com/articles/2007/09/28/list-comprehension-kicks-ass</link>
      <category>programming</category>
    </item>
    <item>
      <title>"List comprehension kicks ass" by Aaron Feng</title>
      <description>&lt;p&gt;Ashton,&lt;/p&gt;

&lt;p&gt;I'm glad you enjoy my blog.  Python's syntax for list comprehension is really nice.&lt;/p&gt;</description>
      <pubDate>Wed, 09 Jan 2008 22:05:39 -0600</pubDate>
      <guid isPermaLink="false">urn:uuid:901ffeac-214c-4274-9d75-54b4e38a46a6</guid>
      <link>http://www.aaronfeng.com/articles/2007/09/28/list-comprehension-kicks-ass#comment-4331</link>
    </item>
    <item>
      <title>"List comprehension kicks ass" by ashton</title>
      <description>&lt;p&gt;Your blog is very interesting.  I use python at work, and noticed that list comprehension is much nicer than Erlang :P
QualifiendNames = [user.name for user in Users if user.age &gt;= 30]&lt;/p&gt;</description>
      <pubDate>Wed, 09 Jan 2008 16:27:39 -0600</pubDate>
      <guid isPermaLink="false">urn:uuid:2fd0450a-0312-4298-b4ce-a04e406f6690</guid>
      <link>http://www.aaronfeng.com/articles/2007/09/28/list-comprehension-kicks-ass#comment-4330</link>
    </item>
    <item>
      <title>"List comprehension kicks ass" by Aaron Feng</title>
      <description>&lt;p&gt;I don't think cryptic is the right word.  Cryptic means one chose to implement code in an unintuitive manner in favorite of clever code.  List comprehension is not "clever" code, it's a way of life in &lt;a href="http://www.erlang.org/"&gt;Erlang&lt;/a&gt;.  It looks nothing like C# or Java because it's not supposed to.  Once there is an understanding of Erlang syntax, one will find it very concise.&lt;/p&gt;

&lt;p&gt;I'm not trying to write an application for my phone.  Erlang was invented for the telecomunication industry and it is not a language for cell phones.  It can solve general everyday problems.  Erlang was created with concurrency in mind and it is designed to be extremely fault tolerant.  It is also great for high speed distributed applications.&lt;/p&gt;</description>
      <pubDate>Mon, 01 Oct 2007 20:17:51 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:2542fe26-9481-435c-8229-026ed1649017</guid>
      <link>http://www.aaronfeng.com/articles/2007/09/28/list-comprehension-kicks-ass#comment-4112</link>
    </item>
    <item>
      <title>"List comprehension kicks ass" by Todd</title>
      <description>&lt;p&gt;So shorter and more cryptic = better?  I am not a fan of these mathematical type of syntax.  Write any J lately?&lt;/p&gt;

&lt;p&gt;BTW - are you trying to write some apps for your phone?  Why Erlang?&lt;/p&gt;</description>
      <pubDate>Mon, 01 Oct 2007 13:06:52 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:bca0335f-3494-4028-8b06-f12ef30592eb</guid>
      <link>http://www.aaronfeng.com/articles/2007/09/28/list-comprehension-kicks-ass#comment-4111</link>
    </item>
  </channel>
</rss>
