<?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>elblogg &#187; snippets</title>
	<atom:link href="http://blog.elzapp.com/tag/snippets/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.elzapp.com</link>
	<description>blogge v1 (norr bloðga, av *blod) skjære, stikke fisk slik at blodet renner ut, jf *bløgge</description>
	<lastBuildDate>Wed, 01 Sep 2010 08:41:50 +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>Should have guessed it</title>
		<link>http://blog.elzapp.com/2008/11/05/should-have-guessed-it.html</link>
		<comments>http://blog.elzapp.com/2008/11/05/should-have-guessed-it.html#comments</comments>
		<pubDate>Wed, 05 Nov 2008 09:16:58 +0000</pubDate>
		<dc:creator>elzapp</dc:creator>
				<category><![CDATA[english]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[snippets]]></category>

		<guid isPermaLink="false">http://blog.elzapp.com/?p=414</guid>
		<description><![CDATA[I have sort of a love/hate relationship with Python, the programming language. Coming from PHP and Java which both have excellent documentation, while lots can be said about the solutions. With Python 2.6 the documentation really has improved, but its still not really there, while the solutions are so elegant that the only reason you [...]]]></description>
			<content:encoded><![CDATA[<p>I have sort of a love/hate relationship with Python, the programming language. Coming from PHP and Java which both have excellent documentation, while lots can be said about the solutions. With Python 2.6 the documentation really has improved, but its still not really there, while the solutions are so elegant that the only reason you don&#8217;t guess how to do stuff is that it feels too simple.</p>
<p>Today I had one of these realizations, while reading <a href="http://pieceofpy.com/index.php/2008/11/04/voting-and-the-land-of-ice-and-snow/">Wayne&#8217;s snippet</a> of the day.</p>
<p>Python has a really nice list-generation scheme, so you can generate lists of the content you want from another list of object with only one line of code.</p>
<p>Example:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p414code5'); return false;">View Code</a> PYTHON</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p4145"><td class="code" id="p414code5"><pre class="python" style="font-family:monospace;"><span style="color: #66cc66;">&amp;</span>gt<span style="color: #66cc66;">;&amp;</span>gt<span style="color: #66cc66;">;&amp;</span>gt<span style="color: #66cc66;">;</span> <span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">math</span> <span style="color: #ff7700;font-weight:bold;">import</span> floor
<span style="color: #66cc66;">&amp;</span>gt<span style="color: #66cc66;">;&amp;</span>gt<span style="color: #66cc66;">;&amp;</span>gt<span style="color: #66cc66;">;</span> a = <span style="color: black;">&#91;</span><span style="color: #ff4500;">1.5</span>, <span style="color: #ff4500;">1.9</span>, <span style="color: #ff4500;">2.5</span>, <span style="color: #ff4500;">3.1</span>, <span style="color: #ff4500;">5.7</span><span style="color: black;">&#93;</span>
<span style="color: #66cc66;">&amp;</span>gt<span style="color: #66cc66;">;&amp;</span>gt<span style="color: #66cc66;">;&amp;</span>gt<span style="color: #66cc66;">;</span> b = <span style="color: black;">&#91;</span>floor<span style="color: black;">&#40;</span>i<span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> a<span style="color: black;">&#93;</span>
<span style="color: #66cc66;">&amp;</span>gt<span style="color: #66cc66;">;&amp;</span>gt<span style="color: #66cc66;">;&amp;</span>gt<span style="color: #66cc66;">;</span> <span style="color: #ff7700;font-weight:bold;">print</span> b
<span style="color: black;">&#91;</span><span style="color: #ff4500;">1.0</span>, <span style="color: #ff4500;">1.0</span>, <span style="color: #ff4500;">2.0</span>, <span style="color: #ff4500;">3.0</span>, <span style="color: #ff4500;">5.0</span><span style="color: black;">&#93;</span></pre></td></tr></table></div>

<p>Here I generated a list of whole numbers from a list of non-whole numbers.</p>
<p>Now We&#8217;ll take this further.<br />
Lets say we want to filter the list, so we&#8217;ll only get the numbers that satisfy <code>1<=x<2</code>.</p>
<p>We could do this using <code>filter()</code> and lambda functions. Which is totally OK. It'll make Python newbies totally confused (which it did with me until recently, when I realized what lambda functions really are (I'll get back to this)), but it'll work nicely.</p>
<p>But wouldn't it be nice if we could use the same list generation scheme for this as well?<br />
Guess what. You can!</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p414code6'); return false;">View Code</a> PYTHON</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p4146"><td class="code" id="p414code6"><pre class="python" style="font-family:monospace;"><span style="color: #66cc66;">&amp;</span>gt<span style="color: #66cc66;">;&amp;</span>gt<span style="color: #66cc66;">;&amp;</span>gt<span style="color: #66cc66;">;</span> a = <span style="color: black;">&#91;</span><span style="color: #ff4500;">1.5</span>, <span style="color: #ff4500;">1.9</span>, <span style="color: #ff4500;">2.5</span>, <span style="color: #ff4500;">3.1</span>, <span style="color: #ff4500;">5.7</span><span style="color: black;">&#93;</span>
<span style="color: #66cc66;">&amp;</span>gt<span style="color: #66cc66;">;&amp;</span>gt<span style="color: #66cc66;">;&amp;</span>gt<span style="color: #66cc66;">;</span> b = <span style="color: black;">&#91;</span>i <span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> a <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff4500;">1</span><span style="color: #66cc66;">&lt;</span>=i <span style="color: #ff7700;font-weight:bold;">and</span> i<span style="color: #66cc66;">&lt;</span><span style="color: #ff4500;">2</span><span style="color: black;">&#93;</span>
<span style="color: #66cc66;">&amp;</span>gt<span style="color: #66cc66;">;&amp;</span>gt<span style="color: #66cc66;">;&amp;</span>gt<span style="color: #66cc66;">;</span> <span style="color: #ff7700;font-weight:bold;">print</span> b
<span style="color: black;">&#91;</span><span style="color: #ff4500;">1.5</span>, <span style="color: #ff4500;">1.9</span><span style="color: black;">&#93;</span></pre></td></tr></table></div>

<p>Doing the same using filter and a lambda function will look like this</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p414code7'); return false;">View Code</a> PYTHON</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p4147"><td class="code" id="p414code7"><pre class="python" style="font-family:monospace;"><span style="color: #66cc66;">&amp;</span>gt<span style="color: #66cc66;">;&amp;</span>gt<span style="color: #66cc66;">;&amp;</span>gt<span style="color: #66cc66;">;</span> a = <span style="color: black;">&#91;</span><span style="color: #ff4500;">1.5</span>, <span style="color: #ff4500;">1.9</span>, <span style="color: #ff4500;">2.5</span>, <span style="color: #ff4500;">3.1</span>, <span style="color: #ff4500;">5.7</span><span style="color: black;">&#93;</span>
<span style="color: #66cc66;">&amp;</span>gt<span style="color: #66cc66;">;&amp;</span>gt<span style="color: #66cc66;">;&amp;</span>gt<span style="color: #66cc66;">;</span> b = <span style="color: #008000;">filter</span><span style="color: black;">&#40;</span><span style="color: #ff7700;font-weight:bold;">lambda</span> x: <span style="color: #ff4500;">1</span><span style="color: #66cc66;">&lt;</span>=x <span style="color: #ff7700;font-weight:bold;">and</span> x<span style="color: #66cc66;">&lt;</span><span style="color: #ff4500;">2</span>, a<span style="color: black;">&#41;</span>
<span style="color: #66cc66;">&amp;</span>gt<span style="color: #66cc66;">;&amp;</span>gt<span style="color: #66cc66;">;&amp;</span>gt<span style="color: #66cc66;">;</span> <span style="color: #ff7700;font-weight:bold;">print</span> b
<span style="color: black;">&#91;</span><span style="color: #ff4500;">1.5</span>, <span style="color: #ff4500;">1.9</span><span style="color: black;">&#93;</span></pre></td></tr></table></div>

<p>and without using a lambda function:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p414code8'); return false;">View Code</a> PYTHON</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p4148"><td class="code" id="p414code8"><pre class="python" style="font-family:monospace;"><span style="color: #66cc66;">&amp;</span>gt<span style="color: #66cc66;">;&amp;</span>gt<span style="color: #66cc66;">;&amp;</span>gt<span style="color: #66cc66;">;</span> <span style="color: #ff7700;font-weight:bold;">def</span> myfilter<span style="color: black;">&#40;</span>x<span style="color: black;">&#41;</span>:
<span style="color: #66cc66;">&amp;</span>gt<span style="color: #66cc66;">;&amp;</span>gt<span style="color: #66cc66;">;&amp;</span>gt<span style="color: #66cc66;">;</span>     <span style="color: #ff7700;font-weight:bold;">return</span> x: <span style="color: #ff4500;">1</span><span style="color: #66cc66;">&lt;</span>=x <span style="color: #ff7700;font-weight:bold;">and</span> x<span style="color: #66cc66;">&lt;</span><span style="color: #ff4500;">2</span>
<span style="color: #66cc66;">&amp;</span>gt<span style="color: #66cc66;">;&amp;</span>gt<span style="color: #66cc66;">;&amp;</span>gt<span style="color: #66cc66;">;</span> a = <span style="color: black;">&#91;</span><span style="color: #ff4500;">1.5</span>, <span style="color: #ff4500;">1.9</span>, <span style="color: #ff4500;">2.5</span>, <span style="color: #ff4500;">3.1</span>, <span style="color: #ff4500;">5.7</span><span style="color: black;">&#93;</span>
<span style="color: #66cc66;">&amp;</span>gt<span style="color: #66cc66;">;&amp;</span>gt<span style="color: #66cc66;">;&amp;</span>gt<span style="color: #66cc66;">;</span> b = <span style="color: #008000;">filter</span><span style="color: black;">&#40;</span>myfilter, a<span style="color: black;">&#41;</span>
<span style="color: #66cc66;">&amp;</span>gt<span style="color: #66cc66;">;&amp;</span>gt<span style="color: #66cc66;">;&amp;</span>gt<span style="color: #66cc66;">;</span> <span style="color: #ff7700;font-weight:bold;">print</span> b
<span style="color: black;">&#91;</span><span style="color: #ff4500;">1.5</span>, <span style="color: #ff4500;">1.9</span><span style="color: black;">&#93;</span></pre></td></tr></table></div>

<p>All of these will work equally fine, and none of them is very hard to understand, as long as you keep in mind that lambda functions just are like anonymous functions/classes.</p>
<p>But there is something very facinating with the elegance and simplicity in the syntax of the list generator snippet.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.elzapp.com/2008/11/05/should-have-guessed-it.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
