Skip to content

Instantly share code, notes, and snippets.

@vmarquez
Last active August 29, 2015 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vmarquez/1cdf20531609f24eb0fb to your computer and use it in GitHub Desktop.
Save vmarquez/1cdf20531609f24eb0fb to your computer and use it in GitHub Desktop.
Coverage.scala
case class Shift(start: DateTime, end: DateTime)
case class Coverage(time: DateTime, amount: Int)
def findCoverages(increment: Int, shifts: List[Shift]): List[Coverage] = {
shifts.map(s => generateDates(increment, s.start, s.end))
.flatten //Going from List[List[DateTime]] to List[DateTime]
.groupBy(dt => dt) //just grouping by date, groupBy always returns a Map[Key, List[Value]]
.map(kv => Coverage(kv._1, kv._2.size)) //when mapping over a Map/Dictionary, the parameter is a tuple, with _1 being the key and _2 being the value
.toList
}
def generateDates(i: Int, startDate: DateTime, endDate: DateTime): List[DateTime] =
if (startDate.plusMinutes(i).isBefore( endDate) )
startDate :: generateDates(i, startDate.plusMinutes(i), endDate) //generateDates(startDate.plusMinutes(i), endDate).prepend(startDate)
else
List(startDate)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment