{"id":2632,"date":"2015-09-18T01:50:55","date_gmt":"2015-09-18T01:50:55","guid":{"rendered":"http:\/\/www.garysieling.com\/blog\/?p=2632"},"modified":"2015-09-18T01:50:55","modified_gmt":"2015-09-18T01:50:55","slug":"scala-yield-example","status":"publish","type":"post","link":"https:\/\/www.garysieling.com\/blog\/scala-yield-example\/","title":{"rendered":"Scala yield example"},"content":{"rendered":"<p>Like Python, scala lets you use yield to generate sequences of values. In the simplest case, these appear to be exactly the same, but if you read on, you will find that Scala&#8217;s for comprehensions have a lot more options.<\/p>\n<p>For instance, take this contrived example: we generate an infinite stream (1 to infinity), loop over it, returning a new value.<\/p>\n<pre lang=\"scala\">\nvar x = Stream.from(1)\n\nval y = for (z <- x) yield z * 2\n\ny.take(10).toList\n\nres2: List[Int] = List(2, 4, 6, 8, 10, 12, 14, 16, 18, 20)\n<\/pre>\n<p>This does what we'd intuitively expect, similar to python or C#. It is the same as doing a \"map\", like so (although the map version is more terse):<\/p>\n<pre lang=\"scala\">\nvar x = Stream.from(1)\n\nval y = x.map(_ * 2)\n\ny.take(10).toList\n\nres2: List[Int] = List(2, 4, 6, 8, 10, 12, 14, 16, 18, 20)\n<\/pre>\n<p>The advantage of list comprehensions is that you can do a few at once, and they get run as nested loops (or rather, the nearly incomprehensible nested flatMap):<\/p>\n<pre lang=\"Scala\">\nval y = \n  for(\n    a <- List('a', 'b', 'c'); \n    b <- List(1,2,3)\n  ) \n    yield (a, b)\n\ny: List[(Char, Int)] = \n  List(\n    (a,1), (a,2), (a,3), \n    (b,1), (b,2), (b,3), \n    (c,1), (c,2), (c,3)\n  )\n\nres6: List[Int] = List(4, 8, 12, 16, 20, 24, 28, 32, 36, 40)\n<\/pre>\n<p>You can also put \"if\" in:<\/p>\n<pre lang=\"Scala\">\nval y = for (z <- x; if z % 2 == 0) yield z * 2\n\nres6: List[Int] = List(4, 8, 12, 16, 20, 24, 28, 32, 36, 40)\n<\/pre>\n<p>This is equivalent to doing:<\/p>\n<pre lang=\"scala\">\nx.filter(_ % 2 == 0).map(_ * 2)\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Like Python, scala lets you use yield to generate sequences of values. In the simplest case, these appear to be exactly the same, but if you read on, you will find that Scala&#8217;s for comprehensions have a lot more options. For instance, take this contrived example: we generate an infinite stream (1 to infinity), loop &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.garysieling.com\/blog\/scala-yield-example\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Scala yield example&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[4],"tags":[480,482,488],"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/2632"}],"collection":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/comments?post=2632"}],"version-history":[{"count":0,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/2632\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/media?parent=2632"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/categories?post=2632"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/tags?post=2632"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}