{"id":2606,"date":"2015-09-16T01:11:27","date_gmt":"2015-09-16T01:11:27","guid":{"rendered":"http:\/\/www.garysieling.com\/blog\/?p=2606"},"modified":"2015-09-16T01:11:27","modified_gmt":"2015-09-16T01:11:27","slug":"scala-stream-example","status":"publish","type":"post","link":"https:\/\/www.garysieling.com\/blog\/scala-stream-example\/","title":{"rendered":"Scala stream example"},"content":{"rendered":"<p>You can easily generate an infinite stream:<\/p>\n<pre lang=\"scala\">\nval x = Stream.from(1)\n\nx: scala.collection.immutable.Stream[Int] = Stream(1, ?)\n<\/pre>\n<p>Using this, you can pull values, however the &#8220;take&#8221; results will still be a lazy-loaded stream:<\/p>\n<pre lang=\"scala\">\nx.take(10)\n\nres11: scala.collection.immutable.Stream[Int] = Stream(1, ?)\n<\/pre>\n<p>You have to manually force this to return what you want:<\/p>\n<pre lang=\"scala\">\nx.take(10).toList\nres12: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)\n<\/pre>\n<p>Alternately, you can use this as more complex constructions. For instance, shown here, you can find the first ten numbers divisible by five:<\/p>\n<pre lang=\"scala\">\nx.filter(_ % 5 == 0).take(10).toList\n\nres16: List[Int] = \n  List(5, 10, 15, 20, \n       25, 30, 35, 40, 45, 50)\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>You can easily generate an infinite stream: val x = Stream.from(1) x: scala.collection.immutable.Stream[Int] = Stream(1, ?) Using this, you can pull values, however the &#8220;take&#8221; results will still be a lazy-loaded stream: x.take(10) res11: scala.collection.immutable.Stream[Int] = Stream(1, ?) You have to manually force this to return what you want: x.take(10).toList res12: List[Int] = List(1, 2, &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.garysieling.com\/blog\/scala-stream-example\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Scala stream 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\/2606"}],"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=2606"}],"version-history":[{"count":0,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/2606\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/media?parent=2606"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/categories?post=2606"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/tags?post=2606"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}