<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>spiral_code &#187; python</title>
	<atom:link href="http://blog.trydionel.com/tag/python/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.trydionel.com</link>
	<description></description>
	<lastBuildDate>Fri, 27 Aug 2010 00:35:24 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>List Comprehensions in Ruby</title>
		<link>http://blog.trydionel.com/2009/10/27/list-comprehensions-in-ruby/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=list-comprehensions-in-ruby</link>
		<comments>http://blog.trydionel.com/2009/10/27/list-comprehensions-in-ruby/#comments</comments>
		<pubDate>Tue, 27 Oct 2009 00:12:14 +0000</pubDate>
		<dc:creator>Jeff Tucker</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[lists]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://trydionel.wordpress.com/?p=11</guid>
		<description><![CDATA[I think list comprehensions are one of the neatest features of Python. They&#8217;re extremely readable, and are pretty simple to learn. They take verbose, loop-based array handlers and compact them into one elegant line. As an example, the list comprehension &#8230; <a href="http://blog.trydionel.com/2009/10/27/list-comprehensions-in-ruby/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I think <a href="http://en.wikipedia.org/wiki/List_comprehension">list comprehensions</a> are one of the neatest features of Python.  They&#8217;re extremely readable, and are pretty simple to learn.  They take verbose, loop-based array handlers and compact them into one elegant line.  As an example, the list comprehension below produces the primes between 1 and 10:</p>
<pre class="brush: python;">
# suppose isPrime(n) returns the primality of n
[x for x in range(1,10) if isPrime(x)]
# =&amp;gt; [2, 3, 5, 7]
</pre>
<p>When I migrated from Python to Ruby recently, I was disappointed to find that there was no simple replacement for list comprehensions.  Top that with everyone in the Ruby community saying &#8220;Ruby doesn&#8217;t do list comprehensions,&#8221; and I basically gave up on my hope of finding a suitable analogue.  Fortunately though, I stumbled upon <a href="http://www.ruby-forum.com/topic/89416">a thread in Ruby-Forum</a> recently showing how to quickly add the capability onto any array!</p>
<p>The meat of the trick is this:</p>
<pre class="brush: ruby;">
module Enumerable
  def comprehend( &amp;amp;amp;block )
    block ? map( &amp;amp;amp;block ).compact : self
  end
end
</pre>
<p>With this monkey patch, I&#8217;m now able to write:</p>
<pre class="brush: ruby;">
# Likewise, is_prime?(n) returns primality of n
(1..9).comprehend { |x| x if is_prime?(x) }
# =&amp;gt; [2, 3, 5, 7]
</pre>
<p>While this isn&#8217;t a direct replacement of Python&#8217;s implementation, it&#8217;s at least enough to prove that Ruby is surprisingly powerful!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.trydionel.com/2009/10/27/list-comprehensions-in-ruby/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
