Created
February 20, 2015 10:23
-
-
Save j5ik2o/3138b1903ec7d3e14187 to your computer and use it in GitHub Desktop.
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
| 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