Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala method that returns multiple concatenated filter functions

In my app, I'm filtering a file array by various types, like the following:

val files:Array[File] = recursiveListFiles(file)
  .filter(!_.toString.endsWith("png"))
  .filter(!_.toString.endsWith("gif"))
  .filter(!_.toString.endsWith("jpg"))
  .filter(!_.toString.endsWith("jpeg"))
  .filter(!_.toString.endsWith("bmp"))
  .filter(!_.toString.endsWith("db"))

But it would be more neatly to define a method which takes a String array and returns all those filters as a concatenated function. Is that possible? So that I can write

val files:Array[File] = recursiveListFiles(file).filter(
  notEndsWith("png", "gif", "jpg", "jpeg", "bmp", "db") 
)
like image 253
Wolkenarchitekt Avatar asked Aug 04 '10 22:08

Wolkenarchitekt


1 Answers

You could do something like this:

def notEndsWith(suffix: String*): File => Boolean = { file =>
  !suffix.exists(file.getName.endsWith)
}
like image 79
Moritz Avatar answered Oct 19 '22 16:10

Moritz