{"id":3137,"date":"2016-02-10T01:50:55","date_gmt":"2016-02-10T01:50:55","guid":{"rendered":"http:\/\/www.garysieling.com\/blog\/?p=3137"},"modified":"2016-02-10T01:50:55","modified_gmt":"2016-02-10T01:50:55","slug":"scala-list-comprehension","status":"publish","type":"post","link":"https:\/\/www.garysieling.com\/blog\/scala-list-comprehension\/","title":{"rendered":"Scala list comprehension"},"content":{"rendered":"<p>Scala offers list comprehensions, which are a simple way to create lists. These are often used to convert one list into another, while applying some change to each entry. List comprehensions can also filter a list.<\/p>\n<p>For example, we can determine the length of words in a sentence:<\/p>\n<pre lang=\"scala\">\nval x = \"The quick brown fox jumps over the lazy dog\".split(\" \")\ny: Array[Int] = Array(3, 5, 5, 3, 5, 4, 3, 4, 3)\n<\/pre>\n<p>Then, we can filter it to only long words:<\/p>\n<pre lang=\"scala\">\nval z = for (i <- x if i.length > 4) yield i \nz: Array[String] = Array(quick, brown, jumps)\n<\/pre>\n<p>Let&#8217;s say we had a second sentence, and we wanted to find the words that are in both.<\/p>\n<p>We can do this with a for comprehension:<\/p>\n<p>For instance:<\/p>\n<pre lang=\"scala\">\nval words1 = \"This is an example sentence about a lazy dog\".split(\" \")\nval words2 = \"The quick brown fox jumps over the lazy dog\".split(\" \")\n\nfor (\n  i <- words1;\n  j <- words2 if i.equals(j)\n) yield i\n\nres22: Array[String] = Array(lazy, dog)\n<\/pre>\n<p>It's worth noting that you can also use a numeric range:<br \/>\nYou can also use a numeric range:<\/p>\n<pre lang=\"scala\">\nfor (i <- 0 to 5) yield i\n\nres11: scala.collection.immutable.IndexedSeq[Int] = \n  Vector(0, 1, 2, 3, 4, 5)\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Scala offers list comprehensions, which are a simple way to create lists. These are often used to convert one list into another, while applying some change to each entry. List comprehensions can also filter a list. For example, we can determine the length of words in a sentence: val x = &#8220;The quick brown fox &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.garysieling.com\/blog\/scala-list-comprehension\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Scala list comprehension&#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,486],"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/3137"}],"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=3137"}],"version-history":[{"count":0,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/3137\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/media?parent=3137"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/categories?post=3137"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/tags?post=3137"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}