Created
July 11, 2014 13:03
-
-
Save fthomas/45c4df6a8272352b854d to your computer and use it in GitHub Desktop.
List contents of a directory with scalaz-stream
This file contains 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
package scalaz.stream | |
import java.nio.file.{DirectoryStream, Path} | |
import java.nio.file.Files._ | |
import scalaz.concurrent.Task | |
import Process._ | |
object os { | |
def listDirectory(dir: Path): Process[Task, Path] = | |
evalIterable(newDirectoryStream(dir)) | |
def listDirectoryDeep(dir: Path, followSymlinks: Boolean = false): Process[Task, Path] = | |
listDirectory(dir).flatMap { path => | |
emit(path) ++ { | |
val recur = isDirectory(path) && (!isSymbolicLink(path) || followSymlinks) | |
if (recur) listDirectoryDeep(path, followSymlinks) else halt | |
} | |
} | |
private type CloseableIterable[A] = | |
java.lang.Iterable[A] with java.io.Closeable | |
private def evalIterable[A](iterable: => CloseableIterable[A]): Process[Task, A] = | |
io.resource(Task.delay(iterable))(r => Task.delay(r.close())) { r => | |
lazy val it = r.iterator() | |
Task.delay { if (it.hasNext) it.next() else throw End } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment