/articles/2010-11-05/index.html
https://github.com/gglynne/gglynne.github.com · HTML · 118 lines · 39 code · 22 blank · 57 comment · 0 complexity · a285fdec8a16fdf7c39bad0d597bc621 MD5 · raw file
- <!DOCTYPE HTML>
- <html lang="en">
- <head>
- <meta charset="utf-8">
- <title>list comprehensions: python vs ruby</title>
- <link rel="stylesheet" type="text/css" href="/style.css" media="screen">
- <style TYPE="text/css">
- <!--
- -->
- </style>
- <meta name="generator" content="nanoc 3.1.5">
- </head>
- <body>
- <!--<div id="header">-->
- <!--<ul>-->
- <!--<li>-->
- <!---->
- <!--<span class="breadcrumbs">-->
- <!--<a href="/">top</a> -->
- <!--» -->
- <!---->
- <!--<a href="/articles/">all articles</a> -->
- <!--» -->
- <!---->
- <!--<a href="/articles/2010-11-05/">list comprehensions: python vs ruby</a> -->
- <!-- -->
- <!---->
- <!--</span>-->
- <!---->
- <!--</li>-->
- <!--</ul>-->
- <!--</div>-->
- <div id="main">
- <p>python:</p>
- <pre><code>In [1]: a=range(9)
- In [2]: a
- Out[2]: [0, 1, 2, 3, 4, 5, 6, 7, 8]
- In [4]: [x for x in a if x<5]
- Out[4]: [0, 1, 2, 3, 4]
- In [5]: [x*x for x in a if x<5]
- Out[5]: [0, 1, 4, 9, 16]
- </code></pre>
- <p>ruby:</p>
- <pre><code>a=(0..9).to_a
- [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
- a.select{|x| x<6}
- [0, 1, 2, 3, 4, 5]
- a.select{|x| x<6}.collect{|x| x*x}
- [0, 1, 4, 9, 16, 25]
- </code></pre>
- <!--<div id="disqus_thread"></div>-->
- <!--<script type="text/javascript">-->
- <!--/**-->
- <!--* var disqus_identifier; [Optional but recommended: Define a unique identifier (e.g. post id or slug) for this thread] -->
- <!--*/-->
- <!--var disqus_developer=false;-->
- <!--var disqus_identifier="http://gglynne.github.com/articles/2010-11-05/";-->
- <!--var disqus_url="http://gglynne.github.com/articles/2010-11-05/";-->
- <!--(function() {-->
- <!--var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;-->
- <!--dsq.src = 'http://gglynne.disqus.com/embed.js';-->
- <!--(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);-->
- <!--})();-->
- <!--</script>-->
- <!--<noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript=gglynne">comments powered by Disqus.</a></noscript>-->
- <!--<a href="http://disqus.com" class="dsq-brlink">blog comments powered by <span class="logo-disqus">Disqus</span></a>-->
- <!--<a href="http://gglynne.github.com/#disqus_thread">Comments</a>-->
- </div>
- <div id="sidebar">
- <ul>
- <li><a href="/">top</a></li>
- <li><a href="/about/">about</a></li>
- <li><a href="/articles/">all</a></li>
- </ul>
- <!--<h2>Documentation</h2>-->
- <!--<ul>-->
- <!--<li><a href="http://nanoc.stoneship.org/docs/">Documentation</a></li>-->
- <!--<li><a href="http://nanoc.stoneship.org/docs/3-getting-started/">Getting Started</a></li>-->
- <!--</ul>-->
- <!--<h2>Community</h2>-->
- <!--<ul>-->
- <!--<li><a href="http://groups.google.com/group/nanoc/">Discussion Group</a></li>-->
- <!--<li><a href="irc://chat.freenode.net/#nanoc">IRC Channel</a></li>-->
- <!--<li><a href="http://projects.stoneship.org/trac/nanoc/">Wiki</a></li>-->
- <!--</ul>-->
- </div>
- <!--<script type="text/javascript">-->
- <!--var disqus_shortname = 'gglynne';-->
- <!--(function () {-->
- <!--var s = document.createElement('script'); s.async = true;-->
- <!--s.src = 'http://disqus.com/forums/gglynne/count.js';-->
- <!--(document.getElementsByTagName('HEAD')[0] || document.getElementsByTagName('BODY')[0]).appendChild(s);-->
- <!--}());-->
- <!--</script>-->
- </body>
- </html>