Convert seconds to compound duration: Difference between revisions

{{header|AppleScript}}
({{header|AppleScript}})
Line 179:
9 wk, 6 d, 10 hr, 40 min
</pre>
 
 
=={{header|AppleScript}}==
 
 
{{Trans|JavaScript}}
 
<lang AppleScript>
on run
intercalate(linefeed, ¬
map(test, ¬
[7259, 86400, 6000000]))
end run
 
-- test :: Int -> String
on test(intSeconds)
(intSeconds as string) & " -> " & angloDuration(intSeconds)
end test
 
 
 
-- angloDuration :: Int -> String
on angloDuration(intSeconds)
script mf
on formatted(a, lstPair)
set q to item 1 of lstPair
if q > 0 then
{(q as string) & space & item 2 of lstPair} & a
else
a
end if
end formatted
end script
intercalate(", ", ¬
foldr(mf's formatted, [], ¬
zip(weekParts(intSeconds), ¬
["wk", "d", "hr", "min", "sec"])))
end angloDuration
 
 
-- weekParts :: Int -> [Int]
on weekParts(intSeconds)
script mf
-- partList Record -> Int -> Int -> Record
on partList(a, x, i)
set intRest to remaining of a
if x is not missing value then
set intMod to intRest mod x
set d to x
else
set intMod to intRest
set d to 1
end if
{remaining:(intRest - intMod) div d, parts:{intMod} & parts of a}
end partList
end script
parts of foldr(mf's partList, ¬
{remaining:intSeconds, parts:[]}, [missing value, 7, 24, 60, 60])
end weekParts
 
 
 
-- GENERIC FUNCTIONS
 
-- foldr :: (a -> b -> a) -> a -> [b] -> a
on foldr(f, startValue, xs)
set mf to mReturn(f)
set v to startValue
set lng to length of xs
repeat with i from lng to 1 by -1
set v to mf's lambda(v, item i of xs, i, xs)
end repeat
return v
end foldr
 
-- map :: (a -> b) -> [a] -> [b]
on map(f, xs)
set mf to mReturn(f)
set lng to length of xs
set lst to {}
repeat with i from 1 to lng
set end of lst to mf's lambda(item i of xs, i, xs)
end repeat
return lst
end map
 
-- zip :: [a] -> [b] -> [(a, b)]
on zip(xs, ys)
script mf
on pair(x, i)
[x, item i of my closure's ys]
end pair
end script
if length of xs = length of ys then
map(mClosure(pair of mf, {ys:ys}), xs)
else
missing value
end if
end zip
 
-- intercalate :: Text -> [Text] -> Text
on intercalate(strText, lstText)
set {dlm, my text item delimiters} to {my text item delimiters, strText}
set strJoined to lstText as text
set my text item delimiters to dlm
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
property lambda : f
end script
end mReturn
 
-- mClosure :: Handler -> Record -> Script
on mClosure(f, recBindings)
script
property closure : recBindings
property lambda : f
end script
end mClosure</lang>
 
 
<pre>7259 -> 2 hr, 59 sec
86400 -> 1 d
6000000 -> 9 wk, 6 d, 10 hr, 40 min</pre>
 
=={{header|AutoHotkey}}==
9,659

edits