Convert seconds to compound duration: Difference between revisions
Convert seconds to compound duration (view source)
Revision as of 23:08, 30 September 2016
, 7 years ago→{{header|AppleScript}}: Simpler closures, updated primitives
(Added OCaml) |
(→{{header|AppleScript}}: Simpler closures, updated primitives) |
||
Line 183:
<lang AppleScript>on run
-- test :: Int -> String▼
(intSeconds as string) & " -> " & angloDuration(intSeconds)▼
end script▼
intercalate(linefeed, ¬
map(test,
end run
▲-- test :: Int -> String
▲on test(intSeconds)
▲ (intSeconds as string) & " -> " & angloDuration(intSeconds)
-- DURATION STRINGS
Line 199 ⟶ 200:
-- angloDuration :: Int -> String
on angloDuration(intSeconds)
script mf
▲ -- weekParts Int -> [Int]
on weekParts(intSeconds)
unitParts(intSeconds, [missing value, 7, 24, 60, 60])
end weekParts
end
on formatted(a, lstPair)▼
script formatted▼
set q to item 1 of lstPair
if q > 0 then
Line 213 ⟶ 216:
a
end if
end
end script
intercalate(", ", ¬
foldr(formatted
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
on
set intRest to remaining of a
Line 241 ⟶ 244:
{remaining:(intRest - intMod) div d, parts:{intMod} & parts of a}
end
end script
parts of foldr(partList
{remaining:intTotal, parts:[]}, unitList)
end unitParts
Line 277 ⟶ 280:
-- zip :: [a] -> [b] -> [(a, b)]
on zip(xs, ys)
script
on
[x, item i of
end
end script
if length of xs = length of ys then
map
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
else
▲ property lambda : f
property lambda : f▼
end script
end if
end mReturn
▲ script
▲ property lambda : f
▲ end script
</lang>
|