Skip to content

Instantly share code, notes, and snippets.

@xuwei-k
Created May 8, 2011 03:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xuwei-k/961081 to your computer and use it in GitHub Desktop.
Save xuwei-k/961081 to your computer and use it in GitHub Desktop.
前書いた関数を Y combinator 使って書きなおしてみたお(`・ω・´)
// 以前書いたもの ↓ を書きなおしてみた
// http://d.hatena.ne.jp/xuwei/20100612/1276318733
import java.io.File
type Files = List[File]
type FilesPair = (Files,Files)
def Y[A,B](f:((A => B),A) => B,x:A):B = f((y:A) =>Y(f,y),x)
/** あるディレクトリ以下のファイルを再帰的に取得する関数
* @param dir 基準となるディレクトリ
* @param filterFunc filterとなる関数
*/
def fileSearch(dir:File , filterFunc:(File => Boolean) = {_ => true }):Files =
Y( (p: FilesPair => Files , n:FilesPair ) => {
val (fs,result) = n
if( fs.forall(_.isFile )){
result:::fs
}else{
p(
fs.filter( _.isDirectory ).flatMap( _.listFiles ) ,
fs.filter{ f => f.isFile & filterFunc( f ) }:::result
)
}
}, ( List(dir) , Nil ) )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment