{"id":2622,"date":"2015-09-17T12:38:48","date_gmt":"2015-09-17T12:38:48","guid":{"rendered":"http:\/\/www.garysieling.com\/blog\/?p=2622"},"modified":"2015-09-17T12:38:48","modified_gmt":"2015-09-17T12:38:48","slug":"scala-span-example","status":"publish","type":"post","link":"https:\/\/www.garysieling.com\/blog\/scala-span-example\/","title":{"rendered":"Scala span example"},"content":{"rendered":"<p>The span method lets you split a stream into two parts, by providing a function that detects the dividing line where you want the split to occur. What this doesn\u2019t let you do is to sift through the stream and move each record into the a or b side &#8211; you can\u2019t use this to separate even and odd numbers, for instance.<\/p>\n<p>Example:<\/p>\n<pre lang=\"scala\">\nscala> var (a, b) = Stream.from(1).span(_ < 10)\na: scala.collection.immutable.Stream[Int] = Stream(1, ?)\nb: scala.collection.immutable.Stream[Int] = Stream(10, ?)\n<\/pre>\n<p>From this, it appears that it has actually run through 10 elements immediately, not waiting until you actually need one.<\/p>\n<p>We can see it returns what we\u2019d expect: <\/p>\n<pre lang=\"scala\">\nscala> a.take(10).toList\nres26: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9)\n<\/pre>\n<p>As does b:<\/p>\n<pre lang=\"scala\">\nscala> b.take(10).toList\nres27: List[Int] = List(10, 11, 12, 13, 14, 15, 16, 17, 18, 19)\n<\/pre>\n<p>To show that the even\/odd filtering doesn\u2019t work, see the example below. Since the first element is \u201c1\u201d (odd) it automatically goes to the \u201cb\u201d side, and you get an empty list for \u201ca\u201d.<\/p>\n<pre lang=\"scala\">\nscala> var (a, b) = Stream.from(1).span(_ % 2 == 0)\na: scala.collection.immutable.Stream[Int] = Stream()\nb: scala.collection.immutable.Stream[Int] = Stream(1, ?)\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The span method lets you split a stream into two parts, by providing a function that detects the dividing line where you want the split to occur. What this doesn\u2019t let you do is to sift through the stream and move each record into the a or b side &#8211; you can\u2019t use this to &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.garysieling.com\/blog\/scala-span-example\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Scala span 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\/2622"}],"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=2622"}],"version-history":[{"count":0,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/2622\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/media?parent=2622"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/categories?post=2622"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/tags?post=2622"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}