Created
May 8, 2011 03:46
-
-
Save xuwei-k/961081 to your computer and use it in GitHub Desktop.
前書いた関数を Y combinator 使って書きなおしてみたお(`・ω・´)
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
// 以前書いたもの ↓ を書きなおしてみた | |
// 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