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")
)
You could do something like this:
def notEndsWith(suffix: String*): File => Boolean = { file =>
!suffix.exists(file.getName.endsWith)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With