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