Skip to content

Instantly share code, notes, and snippets.

@j5ik2o
Created February 20, 2015 10:23
Show Gist options
  • Select an option

  • Save j5ik2o/3138b1903ec7d3e14187 to your computer and use it in GitHub Desktop.

Select an option

Save j5ik2o/3138b1903ec7d3e14187 to your computer and use it in GitHub Desktop.
package org.example
import scala.io.Source
object Hello extends App {
def find_(keyword: String, root: java.io.File): Seq[java.io.File] = {
import sys.process._
(s"grep -l -r -s -I '$keyword' $root" lines_!) map {
new java.io.File(_)
}
}
def find(keyword: String, root: java.io.File): Seq[java.io.File] = {
def from(files: Seq[java.io.File]): Stream[java.io.File] = {
val newFiles:Seq[java.io.File] = files.head match {
case file if file.isFile => files.tail
case dir if dir.isDirectory => dir.listFiles ++ files.tail
}
// FIXME: if ~ else かっこ悪い
Stream.cons(files.head, if (newFiles.isEmpty) Stream.empty[java.io.File]
else from(newFiles))
}
from(root.listFiles) filter { file =>
file.isFile && Source.fromFile(file).getLines().exists(_.contains(keyword))
}
}
val file = new java.io.File("/")
println(find("a", file))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment