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.98 tracks/scala/exercises/meetup/example.scala
trackler-2.2.1.97 tracks/scala/exercises/meetup/example.scala
trackler-2.2.1.96 tracks/scala/exercises/meetup/example.scala
trackler-2.2.1.95 tracks/scala/exercises/meetup/example.scala
trackler-2.2.1.94 tracks/scala/exercises/meetup/example.scala
trackler-2.2.1.93 tracks/scala/exercises/meetup/example.scala
trackler-2.2.1.92 tracks/scala/exercises/meetup/example.scala
trackler-2.2.1.91 tracks/scala/exercises/meetup/example.scala
trackler-2.2.1.90 tracks/scala/exercises/meetup/example.scala
trackler-2.2.1.89 tracks/scala/exercises/meetup/example.scala
trackler-2.2.1.88 tracks/scala/exercises/meetup/example.scala
trackler-2.2.1.87 tracks/scala/exercises/meetup/example.scala
trackler-2.2.1.86 tracks/scala/exercises/meetup/example.scala
trackler-2.2.1.85 tracks/scala/exercises/meetup/example.scala
trackler-2.2.1.84 tracks/scala/exercises/meetup/example.scala
trackler-2.2.1.83 tracks/scala/exercises/meetup/example.scala
trackler-2.2.1.82 tracks/scala/exercises/meetup/example.scala
trackler-2.2.1.81 tracks/scala/exercises/meetup/example.scala
trackler-2.2.1.80 tracks/scala/exercises/meetup/example.scala
trackler-2.2.1.79 tracks/scala/exercises/meetup/example.scala