September 2017 Update

This commit is contained in:
Ingy döt Net 2017-09-23 10:01:46 +02:00
parent bba7bfd280
commit ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions

View file

@ -1,44 +1,47 @@
-- TEST -----------------------------------------------------------------------
on run
fiveWeekends(1900, 2100)
end run
-- FIVE WEEKENDS --------------------------------------------------------------
-- fiveWeekends :: Int -> Int -> Record
on fiveWeekends(fromYear, toYear)
set lstYears to range(fromYear, toYear)
set lstYears to enumFromTo(fromYear, toYear)
-- yearMonthString :: (Int, Int) -> String
script yearMonthString
on lambda(lstYearMonth)
on |λ|(lstYearMonth)
((item 1 of lstYearMonth) as string) & " " & ¬
item (item 2 of lstYearMonth) of ¬
{"January", "", "March", "", "May", "", ¬
"July", "August", "", "October", "", "December"}
end lambda
end |λ|
end script
-- addLongMonthsOfYear :: [(Int, Int)] -> [(Int, Int)]
script addLongMonthsOfYear
on lambda(lstYearMonth, intYear)
on |λ|(lstYearMonth, intYear)
-- yearMonth :: Int -> (Int, Int)
script yearMonth
on lambda(intMonth)
return {intYear, intMonth}
end lambda
on |λ|(intMonth)
{intYear, intMonth}
end |λ|
end script
lstYearMonth & ¬
map(yearMonth, my longMonthsStartingFriday(intYear))
end lambda
end |λ|
end script
-- leanYear :: Int -> Bool
script leanYear
on lambda(intYear)
on |λ|(intYear)
0 = length of longMonthsStartingFriday(intYear)
end lambda
end |λ|
end script
set lstFullMonths to map(yearMonthString, ¬
@ -46,32 +49,26 @@ on fiveWeekends(fromYear, toYear)
set lstLeanYears to filter(leanYear, lstYears)
return {{|number|:length of lstFullMonths}, ¬
{{|number|:length of lstFullMonths}, ¬
{firstFive:(items 1 thru 5 of lstFullMonths)}, ¬
{lastFive:(items -5 thru -1 of lstFullMonths)}, ¬
{leanYearCount:length of lstLeanYears}, ¬
{leanYears:lstLeanYears}}
end fiveWeekends
-- longMonthsStartingFriday :: Int -> [Int]
on longMonthsStartingFriday(intYear)
-- startIsFriday :: Int -> Bool
script startIsFriday
on lambda(iMonth)
on |λ|(iMonth)
weekday of calendarDate(intYear, iMonth, 1) is Friday
end lambda
end |λ|
end script
filter(startIsFriday, [1, 3, 5, 7, 8, 10, 12])
end longMonthsStartingFriday
---------------------------------------------------------------------------
-- GENERIC FUNCTIONS
-- calendarDate :: Int -> Int -> Int -> Date
on calendarDate(intYear, intMonth, intDay)
tell (current date)
@ -81,6 +78,50 @@ on calendarDate(intYear, intMonth, intDay)
end tell
end calendarDate
-- GENERIC FUNCTIONS ----------------------------------------------------------
-- enumFromTo :: Enum a => a -> a -> [a]
on enumFromTo(m, n)
set {intM, intN} to {fromEnum(m), fromEnum(n)}
if intM > intN then
set d to -1
else
set d to 1
end if
set lst to {}
if class of m is text then
repeat with i from intM to intN by d
set end of lst to chr(i)
end repeat
else
repeat with i from intM to intN by d
set end of lst to i
end repeat
end if
return lst
end enumFromTo
-- fromEnum :: Enum a => a -> Int
on fromEnum(x)
set c to class of x
if c is boolean then
if x then
1
else
0
end if
else if c is text then
if x "" then
id of x
else
missing value
end if
else
x as integer
end if
end fromEnum
-- filter :: (a -> Bool) -> [a] -> [a]
on filter(f, xs)
tell mReturn(f)
@ -88,7 +129,7 @@ on filter(f, xs)
set lng to length of xs
repeat with i from 1 to lng
set v to item i of xs
if lambda(v, i, xs) then set end of lst to v
if |λ|(v, i, xs) then set end of lst to v
end repeat
return lst
end tell
@ -100,7 +141,7 @@ on foldl(f, startValue, xs)
set v to startValue
set lng to length of xs
repeat with i from 1 to lng
set v to lambda(v, item i of xs, i, xs)
set v to |λ|(v, item i of xs, i, xs)
end repeat
return v
end tell
@ -112,25 +153,12 @@ on map(f, xs)
set lng to length of xs
set lst to {}
repeat with i from 1 to lng
set end of lst to lambda(item i of xs, i, xs)
set end of lst to |λ|(item i of xs, i, xs)
end repeat
return lst
end tell
end map
-- range :: Int -> Int -> [Int]
on range(m, n)
if n < m then
set d to -1
else
set d to 1
end if
set lst to {}
repeat with i from m to n by d
set end of lst to i
end repeat
return lst
end range
-- Lift 2nd class handler function into 1st class script wrapper
-- mReturn :: Handler -> Script
@ -139,7 +167,7 @@ on mReturn(f)
f
else
script
property lambda : f
property |λ| : f
end script
end if
end mReturn