Sha256: e31dea463f24460d8d17297af59d11d7f3ade7470c507e8f350902f5effe852a

Contents?: true

Size: 1.82 KB

Versions: 138

Compression:

Stored size: 1.82 KB

Contents

import java.time.{DayOfWeek, LocalDate}

import Schedule.Schedule

case class Meetup(month: Int, year: Int) {
  private val thirteenth = LocalDate.of(year, month, 13)
  private val firstDay = LocalDate.of(year, month, 1)
  private val nextMonth = firstDay.plusMonths(1)

  private val teenth: Scheduler = (dayOfWeek: Int) => thirteenth.next(dayOfWeek)
  private val first: Scheduler = (dayOfWeek: Int) => firstDay.next(dayOfWeek)
  private val second: Scheduler = (dayOfWeek: Int) => first.day(dayOfWeek).plusDays(7)
  private val third: Scheduler = (dayOfWeek: Int) => second.day(dayOfWeek).plusDays(7)
  private val fourth: Scheduler = (dayOfWeek: Int) => third.day(dayOfWeek).plusDays(7)
  private val last: Scheduler = (dayOfWeek: Int) => nextMonth.next(dayOfWeek).minusDays(7)
  private def schedulers: Map[Schedule, Scheduler] = Map(Schedule.Teenth -> teenth,
    Schedule.First -> first,
    Schedule.Second -> second,
    Schedule.Third -> third,
    Schedule.Fourth -> fourth,
    Schedule.Last -> last)

  def day(dayOfWeek: Int, schedule: Schedule): LocalDate =
    schedulers(schedule).day(dayOfWeek)

  implicit class LocalDateOps(self: LocalDate) {
    def next(dayOfWeek: Int): LocalDate = self.plusDays(daysUntil(dayOfWeek))

    def daysUntil(dayOfWeek: Int): Int = (Meetup.Sun - this.dayOfWeek + dayOfWeek) % 7

    def dayOfWeek: Int = self.getDayOfWeek.getValue
  }

  trait Scheduler {
    def day(dayOfWeek: Int): LocalDate
  }
}

object Schedule extends Enumeration {
  type Schedule = Value
  val Teenth, First, Second, Third, Fourth, Last = Value
}

object Meetup {
  val Mon = DayOfWeek.MONDAY.getValue
  val Tue = DayOfWeek.TUESDAY.getValue
  val Wed = DayOfWeek.WEDNESDAY.getValue
  val Thu = DayOfWeek.THURSDAY.getValue
  val Fri = DayOfWeek.FRIDAY.getValue
  val Sat = DayOfWeek.SATURDAY.getValue
  val Sun = DayOfWeek.SUNDAY.getValue
}

Version data entries

138 entries across 138 versions & 1 rubygems

Version Path
trackler-2.2.1.119 tracks/scala/exercises/meetup/example.scala
trackler-2.2.1.118 tracks/scala/exercises/meetup/example.scala
trackler-2.2.1.117 tracks/scala/exercises/meetup/example.scala
trackler-2.2.1.116 tracks/scala/exercises/meetup/example.scala
trackler-2.2.1.115 tracks/scala/exercises/meetup/example.scala
trackler-2.2.1.114 tracks/scala/exercises/meetup/example.scala
trackler-2.2.1.113 tracks/scala/exercises/meetup/example.scala
trackler-2.2.1.111 tracks/scala/exercises/meetup/example.scala
trackler-2.2.1.110 tracks/scala/exercises/meetup/example.scala
trackler-2.2.1.109 tracks/scala/exercises/meetup/example.scala
trackler-2.2.1.108 tracks/scala/exercises/meetup/example.scala
trackler-2.2.1.107 tracks/scala/exercises/meetup/example.scala
trackler-2.2.1.106 tracks/scala/exercises/meetup/example.scala
trackler-2.2.1.105 tracks/scala/exercises/meetup/example.scala
trackler-2.2.1.104 tracks/scala/exercises/meetup/example.scala
trackler-2.2.1.103 tracks/scala/exercises/meetup/example.scala
trackler-2.2.1.102 tracks/scala/exercises/meetup/example.scala
trackler-2.2.1.101 tracks/scala/exercises/meetup/example.scala
trackler-2.2.1.100 tracks/scala/exercises/meetup/example.scala
trackler-2.2.1.99 tracks/scala/exercises/meetup/example.scala