Advertisement
Guest User

Untitled

a guest
Dec 11th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.34 KB | None | 0 0
  1. import org.joda.time.{DateTime, Interval, LocalTime}
  2.  
  3. case class TimePeriod(from: LocalTime, to: LocalTime)
  4. case class Person(startwork: LocalTime, endwork: LocalTime, meetings: List[TimePeriod])
  5.  
  6. val fakeTime = new LocalTime("7:00")
  7. val faketimes: List[TimePeriod] = List(
  8. TimePeriod(fakeTime, fakeTime.plusHours(2)),
  9. TimePeriod(fakeTime.plusHours(3), fakeTime.plusHours(4)),
  10. TimePeriod(fakeTime.plusHours(4), fakeTime.plusHours(5)),
  11. TimePeriod(fakeTime.plusHours(2), fakeTime.plusHours(3)),
  12. TimePeriod(fakeTime, fakeTime.plusHours(2))
  13. )
  14.  
  15. val p1 = Person(fakeTime, fakeTime.plusHours(9), faketimes)
  16. val p2 = Person(fakeTime, fakeTime.plusHours(10), faketimes)
  17. val p3 = Person(fakeTime.minusHours(1), fakeTime.plusHours(10), faketimes)
  18. val p4 = Person(fakeTime.plusHours(3), fakeTime.plusHours(12), faketimes)
  19.  
  20.  
  21. val people = List(p1,p2,p3,p4)
  22.  
  23.  
  24. val times = people.foldLeft((Option.empty[LocalTime], Option.empty[LocalTime]))((accum, curr) => {
  25. accum match {
  26. case (None, None) => (Some(curr.startwork), Some(curr.endwork))
  27. case (Some(start), Some(end)) if start.isBefore(curr.startwork) && end.isBefore(curr.endwork) => (Option(curr.startwork), Option(curr.endwork))
  28. case (Some(start), Some(end)) if start.isBefore(curr.startwork) => (Option(curr.startwork), accum._2)
  29. case (Some(start), Some(end)) if curr.endwork.isBefore(end) => (accum._1, Option(curr.endwork))
  30. case _ => accum
  31. }
  32. })
  33.  
  34. val allUnavaibleTimes = people.foldLeft(List[TimePeriod]())((accum, curr) => accum ::: curr.meetings)
  35.  
  36.  
  37. val test3 = List(1,2,3).indexWhere(_ == 4)
  38. val test2 = List(1,2,3,4,5).indexOf(6)
  39. val test = List(1,2,3,4,5).updated(2,5)
  40. val test4 = List(1,2,3,4,5,6,7).updated(3, List(10,10))
  41. val test5 = List(1,2,3,4,5).patch(2, List(10, 10), 1)
  42. val test6 = IndexedSeq(1,2,3,4,5)
  43. test6(3)
  44.  
  45. calculate(times._1.get, times._2.get, IndexedSeq(TimePeriod(times._1.get, times._2.get)), faketimes)
  46.  
  47.  
  48. def calculate = (minTime: LocalTime, maxTime: LocalTime, master: IndexedSeq[TimePeriod], unavailableSlots: List[TimePeriod]) => {
  49. unavailableSlots.foldLeft(master)((accum, curr) => {
  50. if(curr.from.isBefore(minTime)) accum
  51. if(curr.to.isAfter(maxTime)) accum
  52. // curr.11am.isAfter(10am), curr.12noon.isBefore(19:00)
  53. val index = accum.indexWhere(slot => curr.from.isAfter(slot.from) && curr.to.isBefore(slot.to))
  54. if(index >= 0) {
  55. accum.patch(index, List(TimePeriod(accum(index).from, curr.from), TimePeriod(curr.to, accum(index).to)), 1)
  56. }
  57. else accum
  58. })
  59. }
  60.  
  61. //val lst = List(1,2,3,4,5)
  62. //
  63. //if(lst.indexWhere(_ == 5) > -1) {
  64. // val index = lst.indexWhere(_ == 5)
  65. // lst.patch(index, List(4,5), index)
  66. //}
  67.  
  68. //val sensibleTimes = allUnavaibleTimes.foldLeft(List[TimePeriod]())((accum, curr) => {
  69. // accum match {
  70. // case Nil => accum
  71. // case times if accum.exists(x => x.to == curr.from) => accum.updated(accum.indexWhere(_.to == curr.from), TimePeriod())
  72. // }
  73. //})
  74.  
  75.  
  76. //val availbleSpace = people.foldLeft(List[TimePeriod]())((accum,curr) => {
  77. // val possibleMeetings = curr.meetings.filter {
  78. // case time if time.from.isBefore(times._1.get) && time.to.isAfter(times._2.get) => false
  79. // case time if time.from.isBefore(times._1.get) => false
  80. // case time if time.to.isAfter(times._2.get) => false
  81. // case _ => true
  82. // }
  83. // curr.meetings match {
  84. // case timeperiod => TimePeriod(new LocalTime(), new LocalTime()) :: accum
  85. // }
  86. //})
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement