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.180 tracks/scala/exercises/meetup/example.scala
trackler-2.2.1.179 tracks/scala/exercises/meetup/example.scala
trackler-2.2.1.178 tracks/scala/exercises/meetup/example.scala
trackler-2.2.1.177 tracks/scala/exercises/meetup/example.scala
trackler-2.2.1.176 tracks/scala/exercises/meetup/example.scala
trackler-2.2.1.175 tracks/scala/exercises/meetup/example.scala
trackler-2.2.1.174 tracks/scala/exercises/meetup/example.scala
trackler-2.2.1.173 tracks/scala/exercises/meetup/example.scala
trackler-2.2.1.172 tracks/scala/exercises/meetup/example.scala
trackler-2.2.1.171 tracks/scala/exercises/meetup/example.scala
trackler-2.2.1.170 tracks/scala/exercises/meetup/example.scala
trackler-2.2.1.169 tracks/scala/exercises/meetup/example.scala
trackler-2.2.1.167 tracks/scala/exercises/meetup/example.scala
trackler-2.2.1.166 tracks/scala/exercises/meetup/example.scala
trackler-2.2.1.165 tracks/scala/exercises/meetup/example.scala
trackler-2.2.1.164 tracks/scala/exercises/meetup/example.scala
trackler-2.2.1.163 tracks/scala/exercises/meetup/example.scala
trackler-2.2.1.162 tracks/scala/exercises/meetup/example.scala
trackler-2.2.1.161 tracks/scala/exercises/meetup/example.scala
trackler-2.2.1.160 tracks/scala/exercises/meetup/example.scala