Convert seconds to compound duration: Difference between revisions

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

edits