Sha256: abdc08692ffe5cc6c593b43d9a7e93307b246108ec99138f3f4447bfc30e2f5e

Contents?: true

Size: 1.42 KB

Versions: 396

Compression:

Stored size: 1.42 KB

Contents

(defmodule meetup
  (doc "Calculating the date of meetups.")
  (export (schedule 4)))

(defun WEEKDAY-MAP ()
  "Days of the week enumerator."
  (map 'monday    1
       'tuesday   2
       'wednesday 3
       'thursday  4
       'friday    5
       'saturday  6
       'sunday    7))

(defun SCHEDULE-MAP ()
  "Enumerator for specifying an occurrence of a weekday in a month."
  (map 'first  0
       'second 1
       'third  2
       'fourth 3
       'teenth 4
       'last   5))

(defun weekday-num (weekday)
  (maps:get weekday (WEEKDAY-MAP)))

(defun schedule-num (schedule)
  (maps:get schedule (SCHEDULE-MAP)))

(defun schedule
  "Given a year, month, and weekday and schedule atoms, return the specified date."
  ([year month weekday 'last]
   (let ((last-day (calendar:last_day_of_the_month year month)))
     (date-in-week weekday `#(,year ,month ,(- last-day 6)))))
  ([year month weekday 'teenth]
   (date-in-week weekday `#(,year ,month 13)))
  ([year month weekday nth]
   (let ((start-day (+ 1 (* 7 (schedule-num nth)))))
     (date-in-week weekday `#(,year ,month ,start-day)))))

(defun date-in-week
  ([weekday (= `#(,_ ,_ ,day) start-date)]
   (date-in-range (weekday-num weekday) start-date (+ day 6))))

(defun date-in-range
  ([day-num (= `#(,year ,month ,day) date) end-day] (when (=< day end-day))
   (if (=:= day-num (calendar:day_of_the_week date))
     date
     (date-in-range day-num `#(,year ,month ,(+ day 1)) end-day))))

Version data entries

396 entries across 396 versions & 1 rubygems

Version Path
trackler-2.0.0.5 tracks/lfe/exercises/meetup/src/example.lfe
trackler-2.0.0.4 tracks/lfe/exercises/meetup/src/example.lfe
trackler-2.0.0.3 tracks/lfe/exercises/meetup/src/example.lfe
trackler-2.0.0.2 tracks/lfe/exercises/meetup/src/example.lfe
trackler-2.0.0.1 tracks/lfe/exercises/meetup/src/example.lfe
trackler-2.0.0.0 tracks/lfe/exercises/meetup/src/example.lfe
trackler-1.0.4.1 tracks/lfe/exercises/meetup/src/example.lfe
trackler-1.0.4.0 tracks/lfe/exercises/meetup/src/example.lfe
trackler-1.0.3.0 tracks/lfe/exercises/meetup/src/example.lfe
trackler-1.0.2.1 tracks/lfe/exercises/meetup/src/example.lfe
trackler-1.0.2.0 tracks/lfe/exercises/meetup/src/example.lfe
trackler-1.0.1.2 tracks/lfe/exercises/meetup/src/example.lfe
trackler-1.0.1.1 tracks/lfe/exercises/meetup/src/example.lfe
trackler-1.0.1.0 tracks/lfe/exercises/meetup/src/example.lfe
trackler-1.0.0.1 tracks/lfe/exercises/meetup/src/example.lfe
trackler-1.0.0 tracks/lfe/exercises/meetup/src/example.lfe