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
-- test :: Int -> String
script test
on lambda(intSeconds)
(intSeconds as string) & " -> " & angloDuration(intSeconds)
end lambda
end script
intercalate(linefeed, ¬
intercalate(linefeed, ¬
map(test, ¬
map(test, [7259, 86400, 6000000]))
[7259, 86400, 6000000]))
end run
end run

-- test :: Int -> String
on test(intSeconds)
(intSeconds as string) & " -> " & angloDuration(intSeconds)
end test




-- DURATION STRINGS
-- DURATION STRINGS
Line 199: Line 200:
-- angloDuration :: Int -> String
-- angloDuration :: Int -> String
on angloDuration(intSeconds)
on angloDuration(intSeconds)
-- weekParts Int -> [Int]
script mf
script mf
-- weekParts Int -> [Int]
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]
-- [String] -> (Int, String) -> [String]
on formatted(a, lstPair)
script formatted
on lambda(a, lstPair)
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 formatted
end lambda
end script
end script
intercalate(", ", ¬
intercalate(", ", ¬
foldr(formatted of mf, [], ¬
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 mf
-- partList Record -> Int -> Record
script partList
on partList(a, x)
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 partList
end lambda
end script
end script
parts of foldr(partList of mf, ¬
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 mf
script pair
on pair(x, i)
on lambda(x, i)
[x, item i of my closure's ys]
[x, item i of ys]
end pair
end lambda
end script
end script
if length of xs = length of ys then
if length of xs = length of ys then
map(mClosure(pair of mf, {ys:ys}), xs)
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 return f
if class of f is script then
script
f
else
property lambda : f
end script
script
property lambda : f
end script
end if
end mReturn
end mReturn

-- mClosure :: Handler -> Record -> Script
on mClosure(f, recBindings)
script
property closure : recBindings
property lambda : f
end script
end mClosure
</lang>
</lang>