{"id":30,"date":"2015-03-24T13:17:45","date_gmt":"2015-03-24T12:17:45","guid":{"rendered":"http:\/\/67bricks.com\/blog\/2015\/03\/24\/scalacons-for-creating-streams-from-functions\/"},"modified":"2021-12-16T10:51:39","modified_gmt":"2021-12-16T10:51:39","slug":"scalacons-for-creating-streams-from-functions","status":"publish","type":"post","link":"https:\/\/blog.67bricks.com\/?p=30","title":{"rendered":"Scala Stream.cons for creating streams from functions"},"content":{"rendered":"<p>A feature of Scala that I hadn&#8217;t used before was Stream.cons. This allows you to create a stream (essentially, a lazily evaluated list) from a function. So, for doing some work on files based on their position in the directory hierarchy, we can create a list of their parents:<\/p>\n<blockquote>\n<blockquote><\/blockquote>\n<pre style=\"background-color: #ffffff; color: #000000; font-family: 'Courier New'; font-size: 12pt\"><span style=\"color: #000080; font-weight: bold\">def <\/span>fileParents(file: File) : <span style=\"color: #20999d\">Stream<\/span>[File] = {\n  <span style=\"color: #000080; font-weight: bold\">val <\/span>parent = file.getParentFile\n  <span style=\"color: #000080; font-weight: bold\">if <\/span>(parent == <span style=\"color: #000080; font-weight: bold\">null<\/span>) <span style=\"color: #660e7a; font-style: italic\">Stream<\/span>.<span style=\"font-style: italic\">empty <\/span><span style=\"color: #000080; font-weight: bold\">else <\/span><span style=\"color: #660e7a; font-style: italic\"><\/span><span style=\"color: #660e7a; font-style: italic\">\n    Stream<\/span>.<span style=\"font-style: italic\">cons<\/span>(parent, <span style=\"font-style: italic\">fileParents<\/span>(parent))\n}<\/pre>\n<blockquote><\/blockquote>\n<\/blockquote>\n<p>and then use standard Scala functionality for filtering and finding things in lists, rather than having to write code that iterates through the parent files manually.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A feature of Scala that I hadn&#8217;t used before was Stream.cons. This allows you to create a stream (essentially, a lazily evaluated list) from a function. So, for doing some work on files based on their position in the directory hierarchy, we can create a list of their parents: def fileParents(file: File) : Stream[File] = &hellip; <a href=\"https:\/\/blog.67bricks.com\/?p=30\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Scala Stream.cons for creating streams from functions&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-30","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/blog.67bricks.com\/index.php?rest_route=\/wp\/v2\/posts\/30","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.67bricks.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.67bricks.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.67bricks.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.67bricks.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=30"}],"version-history":[{"count":1,"href":"https:\/\/blog.67bricks.com\/index.php?rest_route=\/wp\/v2\/posts\/30\/revisions"}],"predecessor-version":[{"id":291,"href":"https:\/\/blog.67bricks.com\/index.php?rest_route=\/wp\/v2\/posts\/30\/revisions\/291"}],"wp:attachment":[{"href":"https:\/\/blog.67bricks.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=30"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.67bricks.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=30"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.67bricks.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=30"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}