Meetup
Find the date of a monthly meetup described as “the first Monday”, “the last Thursday” or “the teenth Saturday” of a month.
Adapted from Exercism’s problem specification (MIT). Listed under Dates & Time.
// FILE: meetup.nomi
//# The date of a meetup given as a weekday of a month, such as the second
//# Tuesday or the teenth Friday.
import std::calendar.{Date, Weekday}
/// Which of a month's days on a given weekday a meetup falls on.
pub enum Week
First
Second
Third
Fourth
Last
Teenth
end
/// The date of the meetup in `month` of `year` that falls on `weekday` in
/// `week`: the first to fourth such day, the last, or the one from the 13th
/// to the 19th.
pub fn meetup(year: Int, month: Int, week: Week, weekday: Weekday) -> Date
days = days_on(year, month, weekday)
[first, second, third, fourth, ..] = days else
unreachable "a weekday falls at least four times in every month"
end
last = Iter.last(days)!
case week
.First -> first
.Second -> second
.Third -> third
.Fourth -> fourth
.Last -> last
.Teenth -> teenth(days)
end
end
/// Every day of the month that falls on `weekday`, in order: four or five
/// of them.
fn days_on(year: Int, month: Int, weekday: Weekday) -> Vector<Date>
1..=Date.days_in_month(year, month)!
|> Iter.map(&Date.new(year, month, &1)!)
|> Iter.filter(&(Date.weekday(&1) == weekday))
|> Iter.to_vector()
end
/// The one of `days` that falls from the 13th to the 19th.
fn teenth(days: Vector<Date>) -> Date
Some(day) = Iter.find(days, &(Date.day(&1) in 13..=19)) else
unreachable "the 13th to the 19th are seven days, so one falls on every weekday"
end
day
end
// FILE: meetup_test.nomi
import
std::calendar.{Date, Weekday}
meetup.{self, Week}
end
test "meetup"
[
(
"when teenth Monday is the 13th, the first day of the teenth week",
2013,
5,
Week.Teenth,
Weekday.Monday,
Date"2013-05-13",
),
(
"when teenth Monday is the 19th, the last day of the teenth week",
2013,
8,
.Teenth,
.Monday,
Date"2013-08-19",
),
(
"when teenth Monday is some day in the middle of the teenth week",
2013,
9,
.Teenth,
.Monday,
Date"2013-09-16",
),
(
"when teenth Tuesday is the 19th, the last day of the teenth week",
2013,
3,
.Teenth,
.Tuesday,
Date"2013-03-19",
),
(
"when teenth Tuesday is some day in the middle of the teenth week",
2013,
4,
.Teenth,
.Tuesday,
Date"2013-04-16",
),
(
"when teenth Tuesday is the 13th, the first day of the teenth week",
2013,
8,
.Teenth,
.Tuesday,
Date"2013-08-13",
),
(
"when teenth Wednesday is some day in the middle of the teenth week",
2013,
1,
.Teenth,
.Wednesday,
Date"2013-01-16",
),
(
"when teenth Wednesday is the 13th, the first day of the teenth week",
2013,
2,
.Teenth,
.Wednesday,
Date"2013-02-13",
),
(
"when teenth Wednesday is the 19th, the last day of the teenth week",
2013,
6,
.Teenth,
.Wednesday,
Date"2013-06-19",
),
(
"when teenth Thursday is some day in the middle of the teenth week",
2013,
5,
.Teenth,
.Thursday,
Date"2013-05-16",
),
(
"when teenth Thursday is the 13th, the first day of the teenth week",
2013,
6,
.Teenth,
.Thursday,
Date"2013-06-13",
),
(
"when teenth Thursday is the 19th, the last day of the teenth week",
2013,
9,
.Teenth,
.Thursday,
Date"2013-09-19",
),
(
"when teenth Friday is the 19th, the last day of the teenth week",
2013,
4,
.Teenth,
.Friday,
Date"2013-04-19",
),
(
"when teenth Friday is some day in the middle of the teenth week",
2013,
8,
.Teenth,
.Friday,
Date"2013-08-16",
),
(
"when teenth Friday is the 13th, the first day of the teenth week",
2013,
9,
.Teenth,
.Friday,
Date"2013-09-13",
),
(
"when teenth Saturday is some day in the middle of the teenth week",
2013,
2,
.Teenth,
.Saturday,
Date"2013-02-16",
),
(
"when teenth Saturday is the 13th, the first day of the teenth week",
2013,
4,
.Teenth,
.Saturday,
Date"2013-04-13",
),
(
"when teenth Saturday is the 19th, the last day of the teenth week",
2013,
10,
.Teenth,
.Saturday,
Date"2013-10-19",
),
(
"when teenth Sunday is the 19th, the last day of the teenth week",
2013,
5,
.Teenth,
.Sunday,
Date"2013-05-19",
),
(
"when teenth Sunday is some day in the middle of the teenth week",
2013,
6,
.Teenth,
.Sunday,
Date"2013-06-16",
),
(
"when teenth Sunday is the 13th, the first day of the teenth week",
2013,
10,
.Teenth,
.Sunday,
Date"2013-10-13",
),
(
"when first Monday is some day in the middle of the first week",
2013,
3,
.First,
.Monday,
Date"2013-03-04",
),
(
"when first Monday is the 1st, the first day of the first week",
2013,
4,
.First,
.Monday,
Date"2013-04-01",
),
(
"when first Tuesday is the 7th, the last day of the first week",
2013,
5,
.First,
.Tuesday,
Date"2013-05-07",
),
(
"when first Tuesday is some day in the middle of the first week",
2013,
6,
.First,
.Tuesday,
Date"2013-06-04",
),
(
"when first Wednesday is some day in the middle of the first week",
2013,
7,
.First,
.Wednesday,
Date"2013-07-03",
),
(
"when first Wednesday is the 7th, the last day of the first week",
2013,
8,
.First,
.Wednesday,
Date"2013-08-07",
),
(
"when first Thursday is some day in the middle of the first week",
2013,
9,
.First,
.Thursday,
Date"2013-09-05",
),
(
"when first Thursday is another day in the middle of the first week",
2013,
10,
.First,
.Thursday,
Date"2013-10-03",
),
(
"when first Friday is the 1st, the first day of the first week",
2013,
11,
.First,
.Friday,
Date"2013-11-01",
),
(
"when first Friday is some day in the middle of the first week",
2013,
12,
.First,
.Friday,
Date"2013-12-06",
),
(
"when first Saturday is some day in the middle of the first week",
2013,
1,
.First,
.Saturday,
Date"2013-01-05",
),
(
"when first Saturday is another day in the middle of the first week",
2013,
2,
.First,
.Saturday,
Date"2013-02-02",
),
(
"when first Sunday is some day in the middle of the first week",
2013,
3,
.First,
.Sunday,
Date"2013-03-03",
),
(
"when first Sunday is the 7th, the last day of the first week",
2013,
4,
.First,
.Sunday,
Date"2013-04-07",
),
(
"when second Monday is some day in the middle of the second week",
2013,
3,
.Second,
.Monday,
Date"2013-03-11",
),
(
"when second Monday is the 8th, the first day of the second week",
2013,
4,
.Second,
.Monday,
Date"2013-04-08",
),
(
"when second Tuesday is the 14th, the last day of the second week",
2013,
5,
.Second,
.Tuesday,
Date"2013-05-14",
),
(
"when second Tuesday is some day in the middle of the second week",
2013,
6,
.Second,
.Tuesday,
Date"2013-06-11",
),
(
"when second Wednesday is some day in the middle of the second week",
2013,
7,
.Second,
.Wednesday,
Date"2013-07-10",
),
(
"when second Wednesday is the 14th, the last day of the second week",
2013,
8,
.Second,
.Wednesday,
Date"2013-08-14",
),
(
"when second Thursday is some day in the middle of the second week",
2013,
9,
.Second,
.Thursday,
Date"2013-09-12",
),
(
"when second Thursday is another day in the middle of the second week",
2013,
10,
.Second,
.Thursday,
Date"2013-10-10",
),
(
"when second Friday is the 8th, the first day of the second week",
2013,
11,
.Second,
.Friday,
Date"2013-11-08",
),
(
"when second Friday is some day in the middle of the second week",
2013,
12,
.Second,
.Friday,
Date"2013-12-13",
),
(
"when second Saturday is some day in the middle of the second week",
2013,
1,
.Second,
.Saturday,
Date"2013-01-12",
),
(
"when second Saturday is another day in the middle of the second week",
2013,
2,
.Second,
.Saturday,
Date"2013-02-09",
),
(
"when second Sunday is some day in the middle of the second week",
2013,
3,
.Second,
.Sunday,
Date"2013-03-10",
),
(
"when second Sunday is the 14th, the last day of the second week",
2013,
4,
.Second,
.Sunday,
Date"2013-04-14",
),
(
"when third Monday is some day in the middle of the third week",
2013,
3,
.Third,
.Monday,
Date"2013-03-18",
),
(
"when third Monday is the 15th, the first day of the third week",
2013,
4,
.Third,
.Monday,
Date"2013-04-15",
),
(
"when third Tuesday is the 21st, the last day of the third week",
2013,
5,
.Third,
.Tuesday,
Date"2013-05-21",
),
(
"when third Tuesday is some day in the middle of the third week",
2013,
6,
.Third,
.Tuesday,
Date"2013-06-18",
),
(
"when third Wednesday is some day in the middle of the third week",
2013,
7,
.Third,
.Wednesday,
Date"2013-07-17",
),
(
"when third Wednesday is the 21st, the last day of the third week",
2013,
8,
.Third,
.Wednesday,
Date"2013-08-21",
),
(
"when third Thursday is some day in the middle of the third week",
2013,
9,
.Third,
.Thursday,
Date"2013-09-19",
),
(
"when third Thursday is another day in the middle of the third week",
2013,
10,
.Third,
.Thursday,
Date"2013-10-17",
),
(
"when third Friday is the 15th, the first day of the third week",
2013,
11,
.Third,
.Friday,
Date"2013-11-15",
),
(
"when third Friday is some day in the middle of the third week",
2013,
12,
.Third,
.Friday,
Date"2013-12-20",
),
(
"when third Saturday is some day in the middle of the third week",
2013,
1,
.Third,
.Saturday,
Date"2013-01-19",
),
(
"when third Saturday is another day in the middle of the third week",
2013,
2,
.Third,
.Saturday,
Date"2013-02-16",
),
(
"when third Sunday is some day in the middle of the third week",
2013,
3,
.Third,
.Sunday,
Date"2013-03-17",
),
(
"when third Sunday is the 21st, the last day of the third week",
2013,
4,
.Third,
.Sunday,
Date"2013-04-21",
),
(
"when fourth Monday is some day in the middle of the fourth week",
2013,
3,
.Fourth,
.Monday,
Date"2013-03-25",
),
(
"when fourth Monday is the 22nd, the first day of the fourth week",
2013,
4,
.Fourth,
.Monday,
Date"2013-04-22",
),
(
"when fourth Tuesday is the 28th, the last day of the fourth week",
2013,
5,
.Fourth,
.Tuesday,
Date"2013-05-28",
),
(
"when fourth Tuesday is some day in the middle of the fourth week",
2013,
6,
.Fourth,
.Tuesday,
Date"2013-06-25",
),
(
"when fourth Wednesday is some day in the middle of the fourth week",
2013,
7,
.Fourth,
.Wednesday,
Date"2013-07-24",
),
(
"when fourth Wednesday is the 28th, the last day of the fourth week",
2013,
8,
.Fourth,
.Wednesday,
Date"2013-08-28",
),
(
"when fourth Thursday is some day in the middle of the fourth week",
2013,
9,
.Fourth,
.Thursday,
Date"2013-09-26",
),
(
"when fourth Thursday is another day in the middle of the fourth week",
2013,
10,
.Fourth,
.Thursday,
Date"2013-10-24",
),
(
"when fourth Friday is the 22nd, the first day of the fourth week",
2013,
11,
.Fourth,
.Friday,
Date"2013-11-22",
),
(
"when fourth Friday is some day in the middle of the fourth week",
2013,
12,
.Fourth,
.Friday,
Date"2013-12-27",
),
(
"when fourth Saturday is some day in the middle of the fourth week",
2013,
1,
.Fourth,
.Saturday,
Date"2013-01-26",
),
(
"when fourth Saturday is another day in the middle of the fourth week",
2013,
2,
.Fourth,
.Saturday,
Date"2013-02-23",
),
(
"when fourth Sunday is some day in the middle of the fourth week",
2013,
3,
.Fourth,
.Sunday,
Date"2013-03-24",
),
(
"when fourth Sunday is the 28th, the last day of the fourth week",
2013,
4,
.Fourth,
.Sunday,
Date"2013-04-28",
),
("last Monday in a month with four Mondays", 2013, 3, .Last, .Monday, Date"2013-03-25"),
("last Monday in a month with five Mondays", 2013, 4, .Last, .Monday, Date"2013-04-29"),
("last Tuesday in a month with four Tuesdays", 2013, 5, .Last, .Tuesday, Date"2013-05-28"),
(
"last Tuesday in another month with four Tuesdays",
2013,
6,
.Last,
.Tuesday,
Date"2013-06-25",
),
(
"last Wednesday in a month with five Wednesdays",
2013,
7,
.Last,
.Wednesday,
Date"2013-07-31",
),
(
"last Wednesday in a month with four Wednesdays",
2013,
8,
.Last,
.Wednesday,
Date"2013-08-28",
),
(
"last Thursday in a month with four Thursdays",
2013,
9,
.Last,
.Thursday,
Date"2013-09-26",
),
(
"last Thursday in a month with five Thursdays",
2013,
10,
.Last,
.Thursday,
Date"2013-10-31",
),
("last Friday in a month with five Fridays", 2013, 11, .Last, .Friday, Date"2013-11-29"),
("last Friday in a month with four Fridays", 2013, 12, .Last, .Friday, Date"2013-12-27"),
(
"last Saturday in a month with four Saturdays",
2013,
1,
.Last,
.Saturday,
Date"2013-01-26",
),
(
"last Saturday in another month with four Saturdays",
2013,
2,
.Last,
.Saturday,
Date"2013-02-23",
),
("last Sunday in a month with five Sundays", 2013, 3, .Last, .Sunday, Date"2013-03-31"),
("last Sunday in a month with four Sundays", 2013, 4, .Last, .Sunday, Date"2013-04-28"),
(
"when last Wednesday in February in a leap year is the 29th",
2012,
2,
.Last,
.Wednesday,
Date"2012-02-29",
),
(
"last Wednesday in December that is also the last day of the year",
2014,
12,
.Last,
.Wednesday,
Date"2014-12-31",
),
(
"when last Sunday in February in a non-leap year is not the 29th",
2015,
2,
.Last,
.Sunday,
Date"2015-02-22",
),
(
"when first Friday is the 7th, the last day of the first week",
2012,
12,
.First,
.Friday,
Date"2012-12-07",
),
]
|> subtests (description, year, month, week, weekday, expected), description
assert meetup.meetup(year, month, week, weekday) == expected
end
endok meetup (95 cases)
test result: ok. 95 passed, 0 failed
How it’s written
Section titled “How it’s written”- The week is an enum,
Week, with the canonical data’s six descriptors, and the day is std/calendar’sWeekday. The canonical strings ("teenth","Monday") are the test table’s data, written as variants: the first row qualifies them (Week.Teenth,Weekday.Monday) and the rows after it write.First,.Tuesday. days_onlists the month’s days on the weekday:Date.days_in_month(year, month)!days, eachDate.new(year, month, day)!, filtered by weekday. The calendar decides the month’s length, with no table of month lengths or leap-year rule here. Both!s trap only for a month that is not 1 to 12, which the caller chose, and the trap names the call (style §29).- The first four weeks are taken apart with a vector pattern,
Vector[first, second, third, fourth, ..]. A weekday falls four or five times in every month, so the pattern cannot fail, and itselseis anunreachablethat says why (style §29). The last day isIter.last(days)!: the pattern above matched, sodaysis not empty. A vector pattern needs at least as many elements as it names, and the last day is the fourth in a month with four, so it cannot be a fifth name in the same pattern. - The teenth day is
Iter.findover the days for one from the 13th to the 19th. Those are seven days in a row, so one of them is on every weekday, and theNonethatIter.findcan answer is anunreachablethat says so. - The expected dates are
Dateliterals,Date"2013-05-13", so the table compares dates, not strings. - The 95 canonical cases are one
subteststable named by the description column.