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.78 tracks/scala/exercises/meetup/example.scala
trackler-2.2.1.77 tracks/scala/exercises/meetup/example.scala
trackler-2.2.1.76 tracks/scala/exercises/meetup/example.scala
trackler-2.2.1.75 tracks/scala/exercises/meetup/example.scala
trackler-2.2.1.74 tracks/scala/exercises/meetup/example.scala
trackler-2.2.1.73 tracks/scala/exercises/meetup/example.scala
trackler-2.2.1.72 tracks/scala/exercises/meetup/example.scala
trackler-2.2.1.71 tracks/scala/exercises/meetup/example.scala
trackler-2.2.1.70 tracks/scala/exercises/meetup/example.scala
trackler-2.2.1.69 tracks/scala/exercises/meetup/example.scala
trackler-2.2.1.68 tracks/scala/exercises/meetup/example.scala
trackler-2.2.1.67 tracks/scala/exercises/meetup/example.scala
trackler-2.2.1.66 tracks/scala/exercises/meetup/example.scala
trackler-2.2.1.65 tracks/scala/exercises/meetup/example.scala
trackler-2.2.1.64 tracks/scala/exercises/meetup/example.scala
trackler-2.2.1.63 tracks/scala/exercises/meetup/example.scala
trackler-2.2.1.62 tracks/scala/exercises/meetup/example.scala
trackler-2.2.1.61 tracks/scala/exercises/meetup/example.scala
trackler-2.2.1.60 tracks/scala/exercises/meetup/example.scala
trackler-2.2.1.59 tracks/scala/exercises/meetup/example.scala