Convert seconds to compound duration: Difference between revisions
Content added Content deleted
(Added OCaml) |
(→{{header|AppleScript}}: Simpler closures, updated primitives) |
||
Line 183: | Line 183: | ||
<lang AppleScript>on run |
<lang AppleScript>on run |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
intercalate(linefeed, ¬ |
intercalate(linefeed, ¬ |
||
map(test, |
map(test, [7259, 86400, 6000000])) |
||
[7259, 86400, 6000000])) |
|||
end run |
end run |
||
⚫ | |||
⚫ | |||
⚫ | |||
end test |
|||
-- DURATION STRINGS |
-- DURATION STRINGS |
||
Line 199: | Line 200: | ||
-- angloDuration :: Int -> String |
-- angloDuration :: Int -> String |
||
on angloDuration(intSeconds) |
on angloDuration(intSeconds) |
||
⚫ | |||
script mf |
script mf |
||
⚫ | |||
on weekParts(intSeconds) |
on weekParts(intSeconds) |
||
unitParts(intSeconds, [missing value, 7, 24, 60, 60]) |
unitParts(intSeconds, [missing value, 7, 24, 60, 60]) |
||
end weekParts |
end weekParts |
||
end script |
|||
-- [String] -> (Int, String) -> [String] |
|||
⚫ | |||
⚫ | |||
⚫ | |||
set q to item 1 of lstPair |
set q to item 1 of lstPair |
||
if q > 0 then |
if q > 0 then |
||
Line 213: | Line 216: | ||
a |
a |
||
end if |
end if |
||
end |
end lambda |
||
end script |
end script |
||
intercalate(", ", ¬ |
intercalate(", ", ¬ |
||
foldr(formatted |
foldr(formatted, [], ¬ |
||
zip(weekParts(intSeconds) of mf, ¬ |
zip(weekParts(intSeconds) of mf, ¬ |
||
["wk", "d", "hr", "min", "sec"]))) |
["wk", "d", "hr", "min", "sec"]))) |
||
Line 227: | Line 230: | ||
-- unitParts :: Int -> [maybe Int] -> [Int] |
-- unitParts :: Int -> [maybe Int] -> [Int] |
||
on unitParts(intTotal, unitList) |
on unitParts(intTotal, unitList) |
||
-- partList :: Record -> Int -> Record |
|||
⚫ | |||
script partList |
|||
on |
on lambda(a, x) |
||
set intRest to remaining of a |
set intRest to remaining of a |
||
Line 241: | Line 244: | ||
{remaining:(intRest - intMod) div d, parts:{intMod} & parts of a} |
{remaining:(intRest - intMod) div d, parts:{intMod} & parts of a} |
||
end |
end lambda |
||
end script |
end script |
||
parts of foldr(partList |
parts of foldr(partList, ¬ |
||
{remaining:intTotal, parts:[]}, unitList) |
{remaining:intTotal, parts:[]}, unitList) |
||
end unitParts |
end unitParts |
||
Line 277: | Line 280: | ||
-- zip :: [a] -> [b] -> [(a, b)] |
-- zip :: [a] -> [b] -> [(a, b)] |
||
on zip(xs, ys) |
on zip(xs, ys) |
||
script |
script pair |
||
on |
on lambda(x, i) |
||
[x, item i of |
[x, item i of ys] |
||
end |
end lambda |
||
end script |
end script |
||
if length of xs = length of ys then |
if length of xs = length of ys then |
||
map |
map(pair, xs) |
||
else |
else |
||
missing value |
missing value |
||
Line 297: | Line 300: | ||
return strJoined |
return strJoined |
||
end intercalate |
end intercalate |
||
-- Lift 2nd class handler function into 1st class script wrapper |
-- Lift 2nd class handler function into 1st class script wrapper |
||
-- mReturn :: Handler -> Script |
-- mReturn :: Handler -> Script |
||
on mReturn(f) |
on mReturn(f) |
||
if class of f is script then |
if class of f is script then |
||
f |
|||
else |
|||
⚫ | |||
script |
|||
⚫ | |||
end script |
|||
end if |
|||
end mReturn |
end mReturn |
||
-- mClosure :: Handler -> Record -> Script |
|||
on mClosure(f, recBindings) |
|||
⚫ | |||
property closure : recBindings |
|||
⚫ | |||
⚫ | |||
end mClosure |
|||
</lang> |
</lang> |
||