Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import org.joda.time.{DateTime, Interval, LocalTime}
- case class TimePeriod(from: LocalTime, to: LocalTime)
- case class Person(startwork: LocalTime, endwork: LocalTime, meetings: List[TimePeriod])
- val fakeTime = new LocalTime("7:00")
- val faketimes: List[TimePeriod] = List(
- TimePeriod(fakeTime, fakeTime.plusHours(2)),
- TimePeriod(fakeTime.plusHours(3), fakeTime.plusHours(4)),
- TimePeriod(fakeTime.plusHours(4), fakeTime.plusHours(5)),
- TimePeriod(fakeTime.plusHours(2), fakeTime.plusHours(3)),
- TimePeriod(fakeTime, fakeTime.plusHours(2))
- )
- val p1 = Person(fakeTime, fakeTime.plusHours(9), faketimes)
- val p2 = Person(fakeTime, fakeTime.plusHours(10), faketimes)
- val p3 = Person(fakeTime.minusHours(1), fakeTime.plusHours(10), faketimes)
- val p4 = Person(fakeTime.plusHours(3), fakeTime.plusHours(12), faketimes)
- val people = List(p1,p2,p3,p4)
- val times = people.foldLeft((Option.empty[LocalTime], Option.empty[LocalTime]))((accum, curr) => {
- accum match {
- case (None, None) => (Some(curr.startwork), Some(curr.endwork))
- case (Some(start), Some(end)) if start.isBefore(curr.startwork) && end.isBefore(curr.endwork) => (Option(curr.startwork), Option(curr.endwork))
- case (Some(start), Some(end)) if start.isBefore(curr.startwork) => (Option(curr.startwork), accum._2)
- case (Some(start), Some(end)) if curr.endwork.isBefore(end) => (accum._1, Option(curr.endwork))
- case _ => accum
- }
- })
- val allUnavaibleTimes = people.foldLeft(List[TimePeriod]())((accum, curr) => accum ::: curr.meetings)
- val test3 = List(1,2,3).indexWhere(_ == 4)
- val test2 = List(1,2,3,4,5).indexOf(6)
- val test = List(1,2,3,4,5).updated(2,5)
- val test4 = List(1,2,3,4,5,6,7).updated(3, List(10,10))
- val test5 = List(1,2,3,4,5).patch(2, List(10, 10), 1)
- val test6 = IndexedSeq(1,2,3,4,5)
- test6(3)
- calculate(times._1.get, times._2.get, IndexedSeq(TimePeriod(times._1.get, times._2.get)), faketimes)
- def calculate = (minTime: LocalTime, maxTime: LocalTime, master: IndexedSeq[TimePeriod], unavailableSlots: List[TimePeriod]) => {
- unavailableSlots.foldLeft(master)((accum, curr) => {
- if(curr.from.isBefore(minTime)) accum
- if(curr.to.isAfter(maxTime)) accum
- // curr.11am.isAfter(10am), curr.12noon.isBefore(19:00)
- val index = accum.indexWhere(slot => curr.from.isAfter(slot.from) && curr.to.isBefore(slot.to))
- if(index >= 0) {
- accum.patch(index, List(TimePeriod(accum(index).from, curr.from), TimePeriod(curr.to, accum(index).to)), 1)
- }
- else accum
- })
- }
- //val lst = List(1,2,3,4,5)
- //
- //if(lst.indexWhere(_ == 5) > -1) {
- // val index = lst.indexWhere(_ == 5)
- // lst.patch(index, List(4,5), index)
- //}
- //val sensibleTimes = allUnavaibleTimes.foldLeft(List[TimePeriod]())((accum, curr) => {
- // accum match {
- // case Nil => accum
- // case times if accum.exists(x => x.to == curr.from) => accum.updated(accum.indexWhere(_.to == curr.from), TimePeriod())
- // }
- //})
- //val availbleSpace = people.foldLeft(List[TimePeriod]())((accum,curr) => {
- // val possibleMeetings = curr.meetings.filter {
- // case time if time.from.isBefore(times._1.get) && time.to.isAfter(times._2.get) => false
- // case time if time.from.isBefore(times._1.get) => false
- // case time if time.to.isAfter(times._2.get) => false
- // case _ => true
- // }
- // curr.meetings match {
- // case timeperiod => TimePeriod(new LocalTime(), new LocalTime()) :: accum
- // }
- //})
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement