Last active
August 29, 2015 14:15
-
-
Save orionll/da3c011b4476a0f91fd0 to your computer and use it in GitHub Desktop.
List files purely
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.nio.file.DirectoryStream | |
| import java.nio.file.Files | |
| import java.nio.file.Path | |
| import java.nio.file.Paths | |
| import scalaz.concurrent.Task | |
| import scalaz.std.anyVal._ | |
| import scalaz.stream.Cause | |
| import scalaz.stream.Process | |
| import scalaz.stream.io | |
| object Main extends App { | |
| val proc = files("D:/temp").scan1Map(_ => 1).chunk(10).map("Files found: " + _.last.toString).to(io.stdOutLines) | |
| def files(dir: String): Process[Task, Path] = files(Files.newDirectoryStream(Paths.get(dir))) | |
| def files(dir: => DirectoryStream[Path]): Process[Task, Path] = io.resource(Task.delay((dir, dir.iterator))) | |
| { case (dir, _) => Task.delay(dir.close) } | |
| { case (_, it) => Task.delay(if (it.hasNext) it.next else throw Cause.Terminated(Cause.End)) } | |
| proc.run.run // <- Side effects are happening here | |
| // The output is: | |
| // Files found: 10 | |
| // Files found: 20 | |
| // Files found: 30 | |
| // ... | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment