{"id":1163,"date":"2022-09-23T01:32:30","date_gmt":"2022-09-23T05:32:30","guid":{"rendered":"https:\/\/codefork.com\/blog\/?p=1163"},"modified":"2023-01-25T02:06:33","modified_gmt":"2023-01-25T07:06:33","slug":"generator-expressions","status":"publish","type":"post","link":"https:\/\/codefork.com\/blog\/index.php\/2022\/09\/23\/generator-expressions\/","title":{"rendered":"Generator Expressions"},"content":{"rendered":"\n<p>I discovered Python <a href=\"https:\/\/peps.python.org\/pep-0289\/\">generator expressions<\/a> this week. They&#8217;ve been around since v2.4, way back in 2004, and yet, somehow, they managed to escape my notice until now. I don&#8217;t think I&#8217;ve ever seen them used in any codebase I&#8217;ve worked on.<\/p>\n\n\n\n<p>Generator expressions are almost the same as list comprehension syntax, except instead of square brackets, you use parentheses. And instead of returning a list, the comprehension returns a generator object. <\/p>\n\n\n\n<p>This is quite a nice bit of syntactic sugar that helps to keep things more succinct than writing iterators and generator functions. But it&#8217;s not as succinct as, say, the chained method syntax you find in Scala or Ruby, which I think is vastly superior in terms of clarity. Several years ago, I wrote up some notes about Ruby streams in a <a href=\"https:\/\/github.com\/codeforkjeff\/ruby-data-streams\">github repo<\/a> for my co-workers because it was a valuable technique in some data processing scripts we were working on. (Since then, I haven&#8217;t done much Ruby.)<\/p>\n\n\n\n<p>Here&#8217;s a bit of Python code that duplicates the simple Ruby example in the linked repo above, with the identical output. Again, it&#8217;s not nearly as nice to look at as chained method calls, but still better than having to write separate generator functions or classes.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>nums = list(range(1,6))\n\nadd10 = lambda i: print(f\"adding 10 to {i}\") or i + 10\nfilter_even = lambda i: print(f\"filtering {i} on evenness\") or i % 2 == 0\noutput = lambda i: print(f\"in each: {i}\")\n\n# list comprehensions\n\nr1 = &#91; add10(x) for x in nums ]\nr2 = &#91; x for x in r1 if filter_even(x) ]\nr3 = &#91; output(x) for x in r2 ]\n\n# generator expressions\n\nr1 = ( add10(x) for x in nums )\nr2 = ( x for x in r1 if filter_even(x) )\nr3 = ( output(x) for x in r2 )\nlist(r3)<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I discovered Python generator expressions this week. They&#8217;ve been around since v2.4, way back in 2004, and yet, somehow, they managed to escape my notice until now. I don&#8217;t think I&#8217;ve ever seen them used in any codebase I&#8217;ve worked on. Generator expressions are almost the same as list comprehension syntax, except instead of square &hellip; <a href=\"https:\/\/codefork.com\/blog\/index.php\/2022\/09\/23\/generator-expressions\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Generator Expressions&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,7,23],"tags":[],"class_list":["post-1163","post","type-post","status-publish","format-standard","hentry","category-misc","category-python","category-ruby"],"_links":{"self":[{"href":"https:\/\/codefork.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/1163","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codefork.com\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codefork.com\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codefork.com\/blog\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/codefork.com\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=1163"}],"version-history":[{"count":6,"href":"https:\/\/codefork.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/1163\/revisions"}],"predecessor-version":[{"id":1179,"href":"https:\/\/codefork.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/1163\/revisions\/1179"}],"wp:attachment":[{"href":"https:\/\/codefork.com\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=1163"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codefork.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=1163"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codefork.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=1163"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}