I agree with colleagues that it is better to have separate filters for each condition. Use the .filter method for .filter . However, you also need to convert Data to Long . This can be done using an implicit converter:
implicit def dateToLong(d:Date) = d.getTime
Then you just filter:
dates.view.filter(_ >= start).filter(_ <= finish)
If you still need additional filters, then the implicit βpimp-my libraryβ may help
implicit class ListWithOptionalFilters[T](datas:List[T]){ def filterOpt(f:Option[T=>Boolean]) = datas.filter(d => f.forall(d)) }
Then you do it like this:
def f(dates: List[Date], start: Option[Long], end: Option[Long]) = { val startFilter = start.map(l => (d:Date) => d>=l) val endFilter = end.map(l => (d:Date) => d<=l) dates.filterOpt(startFilter).filterOpt(endFilter) }
source share