Convert seconds to compound duration: Difference between revisions

Content added Content deleted
m (Added Quackery.)
Line 294: Line 294:
=={{header|AppleScript}}==
=={{header|AppleScript}}==


<lang AppleScript>
<lang AppleScript>-- LOCAL NAMES FOR COMPOUND DURATIONS ----------------------------------------
-------------------- COMPOUND DURATIONS ------------------

script angloNames
on |λ|(n)
(n as string) & " -> " & ¬
localCompoundDuration(["wk", "d", "hr", "min", "sec"], n)
end |λ|
end script

unlines(map(angloNames, [7259, 86400, 6000000]))


-- DURATION STRINGS ----------------------------------------------------------


-- weekParts Int -> [Int]
-- weekParts Int -> [Int]
Line 312: Line 301:
unitParts(intSeconds, [missing value, 7, 24, 60, 60])
unitParts(intSeconds, [missing value, 7, 24, 60, 60])
end weekParts
end weekParts



-- localCompoundDuration :: Int -> String
-- localCompoundDuration :: Int -> String
Line 333: Line 323:
end localCompoundDuration
end localCompoundDuration


-- INTEGER DECOMPOSITION -----------------------------------------------------
------------------ INTEGER DECOMPOSITION -----------------


-- unitParts :: Int -> [maybe Int] -> [Int]
-- unitParts :: Int -> [maybe Int] -> [Int]
Line 357: Line 347:
{remaining:intTotal, parts:[]}, unitList)
{remaining:intTotal, parts:[]}, unitList)
end unitParts
end unitParts

--------------------------- TEST -------------------------
on run
script angloNames
on |λ|(n)
(n as string) & " -> " & ¬
localCompoundDuration(["wk", "d", "hr", "min", "sec"], n)
end |λ|
end script
unlines(map(angloNames, [7259, 86400, 6000000]))
end run




-- GENERIC FUNCTIONS ---------------------------------------------------------
-------------------- GENERIC FUNCTIONS -------------------


-- foldr :: (b -> a -> a) -> a -> [b] -> a
-- foldr :: (a -> b -> b) -> b -> [a] -> b
on foldr(f, startValue, xs)
on foldr(f, startValue, xs)
tell mReturn(f)
tell mReturn(f)
Line 373: Line 375:
end foldr
end foldr



-- intercalate :: Text -> [Text] -> Text
on intercalate(strText, lstText)
-- intercalate :: String -> [String] -> String
on intercalate(delim, xs)
set {dlm, my text item delimiters} to {my text item delimiters, strText}
set strJoined to lstText as text
set {dlm, my text item delimiters} to ¬
{my text item delimiters, delim}
set s to xs as text
set my text item delimiters to dlm
set my text item delimiters to dlm
s
return strJoined
end intercalate
end intercalate



-- map :: (a -> b) -> [a] -> [b]
-- map :: (a -> b) -> [a] -> [b]
on map(f, xs)
on map(f, xs)
-- The list obtained by applying f
-- to each element of xs.
tell mReturn(f)
tell mReturn(f)
set lng to length of xs
set lng to length of xs
Line 392: Line 399:
end tell
end tell
end map
end map



-- min :: Ord a => a -> a -> a
-- min :: Ord a => a -> a -> a
Line 402: Line 410:
end min
end min



-- Lift 2nd class handler function into 1st class script wrapper
-- mReturn :: Handler -> Script
-- mReturn :: First-class m => (a -> b) -> m (a -> b)
on mReturn(f)
on mReturn(f)
if class of f is script then
-- 2nd class handler function lifted into 1st class script wrapper.
if script is class of f then
f
f
else
else
Line 413: Line 422:
end if
end if
end mReturn
end mReturn



-- unlines :: [String] -> String
-- unlines :: [String] -> String
on unlines(xs)
on unlines(xs)
-- A single string formed by the intercalation
intercalate(linefeed, xs)
-- of a list of strings with the newline character.
set {dlm, my text item delimiters} to ¬
{my text item delimiters, linefeed}
set s to xs as text
set my text item delimiters to dlm
s
end unlines
end unlines



-- zip :: [a] -> [b] -> [(a, b)]
-- zip :: [a] -> [b] -> [(a, b)]
on zip(xs, ys)
on zip(xs, ys)
-- A list of step-wise pairs drawn from xs and ys
-- up to the length of the shorter of those lists.
set lng to min(length of xs, length of ys)
set lng to min(length of xs, length of ys)
set lst to {}
set zs to {}
repeat with i from 1 to lng
repeat with i from 1 to lng
set end of lst to {item i of xs, item i of ys}
set end of zs to {item i of xs, item i of ys}
end repeat
end repeat
return lst
return zs
end zip</lang>
end zip</lang>
{{Out}}
{{Out}}