/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

  1. <!DOCTYPE HTML>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>list comprehensions: python vs ruby</title>
  6. <link rel="stylesheet" type="text/css" href="/style.css" media="screen">
  7. <style TYPE="text/css">
  8. <!--
  9. -->
  10. </style>
  11. <meta name="generator" content="nanoc 3.1.5">
  12. </head>
  13. <body>
  14. <!--<div id="header">-->
  15. <!--<ul>-->
  16. <!--<li>-->
  17. <!---->
  18. <!--<span class="breadcrumbs">-->
  19. <!--<a href="/">top</a> -->
  20. <!--» -->
  21. <!---->
  22. <!--<a href="/articles/">all articles</a> -->
  23. <!--» -->
  24. <!---->
  25. <!--<a href="/articles/2010-11-05/">list comprehensions: python vs ruby</a> -->
  26. <!-- -->
  27. <!---->
  28. <!--</span>-->
  29. <!---->
  30. <!--</li>-->
  31. <!--</ul>-->
  32. <!--</div>-->
  33. <div id="main">
  34. <p>python:</p>
  35. <pre><code>In [1]: a=range(9)
  36. In [2]: a
  37. Out[2]: [0, 1, 2, 3, 4, 5, 6, 7, 8]
  38. In [4]: [x for x in a if x&lt;5]
  39. Out[4]: [0, 1, 2, 3, 4]
  40. In [5]: [x*x for x in a if x&lt;5]
  41. Out[5]: [0, 1, 4, 9, 16]
  42. </code></pre>
  43. <p>ruby:</p>
  44. <pre><code>a=(0..9).to_a
  45. [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
  46. a.select{|x| x&lt;6}
  47. [0, 1, 2, 3, 4, 5]
  48. a.select{|x| x&lt;6}.collect{|x| x*x}
  49. [0, 1, 4, 9, 16, 25]
  50. </code></pre>
  51. <!--<div id="disqus_thread"></div>-->
  52. <!--<script type="text/javascript">-->
  53. <!--/**-->
  54. <!--* var disqus_identifier; [Optional but recommended: Define a unique identifier (e.g. post id or slug) for this thread] -->
  55. <!--*/-->
  56. <!--var disqus_developer=false;-->
  57. <!--var disqus_identifier="http://gglynne.github.com/articles/2010-11-05/";-->
  58. <!--var disqus_url="http://gglynne.github.com/articles/2010-11-05/";-->
  59. <!--(function() {-->
  60. <!--var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;-->
  61. <!--dsq.src = 'http://gglynne.disqus.com/embed.js';-->
  62. <!--(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);-->
  63. <!--})();-->
  64. <!--</script>-->
  65. <!--<noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript=gglynne">comments powered by Disqus.</a></noscript>-->
  66. <!--<a href="http://disqus.com" class="dsq-brlink">blog comments powered by <span class="logo-disqus">Disqus</span></a>-->
  67. <!--<a href="http://gglynne.github.com/#disqus_thread">Comments</a>-->
  68. </div>
  69. <div id="sidebar">
  70. <ul>
  71. <li><a href="/">top</a></li>
  72. <li><a href="/about/">about</a></li>
  73. <li><a href="/articles/">all</a></li>
  74. </ul>
  75. <!--<h2>Documentation</h2>-->
  76. <!--<ul>-->
  77. <!--<li><a href="http://nanoc.stoneship.org/docs/">Documentation</a></li>-->
  78. <!--<li><a href="http://nanoc.stoneship.org/docs/3-getting-started/">Getting Started</a></li>-->
  79. <!--</ul>-->
  80. <!--<h2>Community</h2>-->
  81. <!--<ul>-->
  82. <!--<li><a href="http://groups.google.com/group/nanoc/">Discussion Group</a></li>-->
  83. <!--<li><a href="irc://chat.freenode.net/#nanoc">IRC Channel</a></li>-->
  84. <!--<li><a href="http://projects.stoneship.org/trac/nanoc/">Wiki</a></li>-->
  85. <!--</ul>-->
  86. </div>
  87. <!--<script type="text/javascript">-->
  88. <!--var disqus_shortname = 'gglynne';-->
  89. <!--(function () {-->
  90. <!--var s = document.createElement('script'); s.async = true;-->
  91. <!--s.src = 'http://disqus.com/forums/gglynne/count.js';-->
  92. <!--(document.getElementsByTagName('HEAD')[0] || document.getElementsByTagName('BODY')[0]).appendChild(s);-->
  93. <!--}());-->
  94. <!--</script>-->
  95. </body>
  96. </html>