Find the last Sunday of each month: Difference between revisions
→AppleScript
Line 49:
<lang AppleScript>on run argv
-- Default range in absence of arguments: from two years ago, to two years ahead▼
-- Up to two optional command line arguments: [yearFrom], [yearTo]▼
-- ~ $ osascript ~/Desktop/lastSundays.scpt ▼
-- ▼
-- ~ $ osascript ~/Desktop/lastSundays.scpt 2013▼
-- ▼
-- ~ $ osascript ~/Desktop/lastSundays.scpt 2013 2016▼
set intThisYear to year of (current date)▼
if class of argv is list then▼
set lngArgs to length of argv▼
if lngArgs > 0 then▼
else▼
set intYear to item 1 of argv▼
end if▼
set {intFrom, intTo} to {intThisYear - 2, intThisYear + 2}▼
intercalate(linefeed, ¬
map(isoRow, ¬
transpose(map(lastSundaysOfYear,
apply(cond(class of argv is list and argv ≠ {}, ¬
singleYearOrRange, fiveCurrentYears), argIntegers(argv))))))
end run
-- ARGUMENT HANDLING
-- singleYearOrRange :: [Int] -> [Int]
on singleYearOrRange(argv)
apply(cond(length of argv > 0, my range, my fiveCurrentYears), argv)
end singleYearOrRange
-- fiveCurrentYears :: () -> [Int]
on fiveCurrentYears(_)
▲ set intThisYear to year of (current date)
end fiveCurrentYears
-- argIntegers :: maybe [String] -> [Int]
on argIntegers(argv)
▲ if class of argv is list and argv ≠ {} then
{map(my parseInt, argv)}
▲ else
end argIntegers
-- DERIVATION OF LAST SUNDAYS
Line 94 ⟶ 98:
-- lastWeekDaysOfYear :: Int -> Int -> [Date]
on lastWeekDaysOfYear(intYear, iWeekday)
map(
{intYear:intYear, iWeekday:iWeekday}), my lastDaysOfMonths(intYear))
end lastWeekDaysOfYear
Line 111 ⟶ 115:
-- lastDaysOfMonths :: Int -> [Int]
on lastDaysOfMonths(y)
end lastDaysOfMonths
-- isLeapYear :: Int -> Bool
on isLeapYear(y)
(0 = y mod 4) and (0 ≠ y mod 100) or (0 = y mod 400)
end isLeapYear
-- GENERIC FUNCTIONS
Line 142 ⟶ 144:
("0" & day of dte)
end isoDateString
-- parseInt :: String -> Int
on parseInt(s)
s as integer
end parseInt
-- Testing and tabulation
Line 149 ⟶ 156:
script mf
on lambdaCol(_, iCol)
map(
{iCol:iCol}), my closure's xss)
end lambdaCol
Line 157 ⟶ 164:
end lambdaRow
end script
map(
end transpose
Line 174 ⟶ 181:
end isoRow
-- range :: (Int
on range(
if tuple = {} then return {}
if length of tuple > 1 then
▲ else
set lng to (n - m) + 1
set base to m - 1
Line 184 ⟶ 197:
return lst
end range
-- Higher-order functions
Line 209 ⟶ 221:
-- Handler -> Record -> Script
on
script
property closure : recBindings
property lambda : f
end script
end
-- cond :: Bool -> (a -> b) -> (a -> b) -> (a -> b)
on cond(bool, f, g)
▲ f
▲ else
▲ g
end cond
-- apply (a -> b) -> a -> b
on apply(f, a)
mReturn(f)'s lambda(a)
end apply
</lang>
{{Out}}
|