Convert seconds to compound duration: Difference between revisions

Content deleted Content added
Hout (talk | contribs)
Hout (talk | contribs)
→‎{{header|Haskell}}: Parameterised both local names and assumptions about hours per day, days per week.
Line 1,780: Line 1,780:




Or, parameterising the local names for these durations:
Or, parameterising both the local names for these durations, and also the working assumptions about hours per day, and days per week:


<lang haskell>import Data.List (intercalate, mapAccumR)
<lang haskell>import Data.List (intercalate, mapAccumR)


----------------- COMPOUND DURATION STRINGS ----------------
translation :: Int -> String -> String
translation n local =
intercalate " -> " $ [show, timePhrase (words local)] <*> [n]


timePhrase :: [String] -> Int -> String
durationString :: String -> Int -> Int -> Int -> String
durationString local daysPerWeek hoursPerDay n =
timePhrase xs n = intercalate ", " (foldr timeTags [] (zip (weekParts n) xs))
intercalate " -> " $
[show, timePhrase daysPerWeek hoursPerDay (words local)] <*> [n]

timePhrase :: Int -> Int -> [String] -> Int -> String
timePhrase daysPerWeek hoursPerDay xs n =
intercalate
", "
(foldr timeTags [] (zip (weekParts daysPerWeek hoursPerDay n) xs))


timeTags :: (Int, String) -> [String] -> [String]
timeTags :: (Int, String) -> [String] -> [String]
Line 1,796: Line 1,802:
| otherwise = xs
| otherwise = xs


weekParts :: Int -> [Int]
weekParts :: Int -> Int -> Int -> [Int]
weekParts daysPerWeek hoursPerDay =
weekParts = snd . flip (mapAccumR byUnits) [0, 7, 24, 60, 60]
snd . flip (mapAccumR byUnits) [0, daysPerWeek, hoursPerDay, 60, 60]


byUnits :: Int -> Int -> (Int, Int)
byUnits :: Int -> Int -> (Int, Int)
Line 1,806: Line 1,813:
| otherwise = (1, rest)
| otherwise = (1, rest)


---------------------------TEST----------------------------
---------------------------- TEST --------------------------
main :: IO ()
main :: IO ()
main =
main = do
mapM_ (putStrLn . flip translation "wk d hr min sec") [7259, 86400, 6000000]</lang>
let names = "wk d hr min sec"
let tests = [7259, 86400, 6000000]
putStrLn "Assuming 24/7:"
mapM_ (putStrLn . durationString names 7 24) tests
putStrLn "\nor, at 8 working hours per day, 5 days per week:"
mapM_ (putStrLn . durationString names 5 8) tests</lang>
{{Out}}
{{Out}}
<pre>Assuming 24/7:
<pre>7259 -> 2 hr, 59 sec
7259 -> 2 hr, 59 sec
86400 -> 1 d
86400 -> 1 d
6000000 -> 9 wk, 6 d, 10 hr, 40 min</pre>
6000000 -> 9 wk, 6 d, 10 hr, 40 min

or, at 8 working hours per day, 5 days per week:
7259 -> 2 hr, 59 sec
86400 -> 3 d
6000000 -> 41 wk, 3 d, 2 hr, 40 min</pre>


=={{header|J}}==
=={{header|J}}==