Scala: exclude some dates

I have a list of dates that I want to ignore:

 private val excludeDates = List(
              new DateTime("2015-07-17"),
               new DateTime("2015-07-20"),
               new DateTime("2015-07-23")
              )

But I always need to display four dates, with the exception of my blacklist of Dates and weekends. So far, with the following code, my counter is incremented when I click on an ignored date, and this creates a feeling. So, how can I go to the next date until I press 4 dates not on my black list and my days off? Maybe for a while, but I don't know how to add it to scala code:

1 to 4  map { endDate.minusDays(_)} diff excludeDates filter {
              _.getDayOfWeek() match {
                         case DateTimeConstants.SUNDAY |       DateTimeConstants.SATURDAY => false
                case _ => true
              }
            }
+4
source share
2 answers

You can use Stream:

val blacklist = excludeDates.toSet

Stream.from(1)
      .map(endDate.minusDays(_))
      .filter(dt => ! blacklist.contains(dt))
      .take(4)
      .toList
+7
source

Quick and rude, I would do it like this:

val upperLimit = 4 + excludeDates.length

(1 to upperLimit).map( endDate.minusDays ).filter( d => !excludeDates.contains(d) ).take(4)

, , , , , , , .take(n)

, :)

+1

All Articles