Last active
August 29, 2015 14:18
-
-
Save vmarquez/1cdf20531609f24eb0fb to your computer and use it in GitHub Desktop.
Coverage.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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