Find the last Sunday of each month: Difference between revisions

Content added Content deleted
(Added F# version)
(→‎{{Header|AppleScript}}: Simpler closures, updated primitives)
Line 98: Line 98:
-- lastWeekDaysOfYear :: Int -> Int -> [Date]
-- lastWeekDaysOfYear :: Int -> Int -> [Date]
on lastWeekDaysOfYear(intYear, iWeekday)
on lastWeekDaysOfYear(intYear, iWeekday)
map(mclosure(my lastWeekDay, ¬
-- lastWeekDay :: Int -> Int -> Date
script lastWeekDay
{intYear:intYear, iWeekday:iWeekday}), my lastDaysOfMonths(intYear))
on lambda(iLastDay, iMonth)
calendarDate(intYear, iMonth, iLastDay - ¬
(((weekday of calendarDate(intYear, iMonth, iLastDay)) as integer) + ¬
(7 - iWeekday)) mod 7)
end lambda
end script
map(lastWeekDay, my lastDaysOfMonths(intYear))
end lastWeekDaysOfYear
end lastWeekDaysOfYear


-- lastWeekDay :: Int -> Int -> Date
on lastWeekDay(iLastDay, iMonth)
set iYear to intYear of my closure
calendarDate(iYear, iMonth, iLastDay - ¬
(((weekday of calendarDate(iYear, iMonth, iLastDay)) as integer) + ¬
(7 - (iWeekday of my closure))) mod 7)
end lastWeekDay




Line 156: Line 155:


-- transpose :: [[a]] -> [[a]]
-- transpose :: [[a]] -> [[a]]
on transpose(xss)
on transpose(rows)
script mf
script column
on lambdaCol(_, iCol)
map(mclosure(my closure's mf's lambdaRow, ¬
-- Just the index of each top row item
{iCol:iCol}), my closure's xss)
on lambda(_, n)
end lambdaCol
script nthCell
on lambdaRow(row)
on lambda(row)
item (my closure's iCol) of row
item n of row
end lambdaRow
end lambda
end script
-- Column n consists of
-- cell n of each row
map(nthCell, rows)
end lambda
end script
end script
map(mclosure(mf's lambdaCol, {xss:xss, mf:mf}), item 1 of xss)
-- A column from each item of the top row
map(column, item 1 of rows)
end transpose
end transpose


Line 222: Line 229:
end script
end script
end mReturn
end mReturn

-- Handler -> Record -> Script
on mclosure(f, recBindings)
script
property closure : recBindings
property lambda : f
end script
end mclosure


-- cond :: Bool -> (a -> b) -> (a -> b) -> (a -> b)
-- cond :: Bool -> (a -> b) -> (a -> b) -> (a -> b)
Line 243: Line 242:
on apply(f, a)
on apply(f, a)
mReturn(f)'s lambda(a)
mReturn(f)'s lambda(a)
end apply
end apply</lang>
</lang>


{{Out}}
{{Out}}