有没有更优雅的方法来根据可选参数值过滤列表?
def f(dates: List[Date], start: Option[Long], end: Option[Long]): List[Date] = {
(start, end) match {
case (Some(s), Some(e)) => dates filter (_.getTime > s) filter (_.getTime < e)
case (Some(s), None) => dates filter (_.getTime > s)
case (None, Some(e)) => dates filter (_.getTime < e)
case (None, None) => dates
}
}
使用三个可选参数值,这将有 9 种情况等。