Convert seconds to compound duration: Difference between revisions

→‎{{header|Haskell}}: Parameterised both local names and assumptions about hours per day, days per week.
(→‎{{header|Haskell}}: Parameterised both local names and assumptions about hours per day, days per week.)
Line 1,780:
 
 
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)
 
----------------- COMPOUND DURATION STRINGS ----------------
translation :: Int -> String -> String
translation n local =
intercalate " -> " $ [show, timePhrase (words local)] <*> [n]
 
timePhrasedurationString :: [String] -> Int -> Int -> Int -> String
durationString local daysPerWeek hoursPerDay n =
timePhrase xs n = intercalate ", " (foldr timeTags [] (zip (weekParts n) xs))
intercalate " -> " $
intercalate " -> " $ [show, timePhrase daysPerWeek hoursPerDay (words local)] <*> [n]
 
translationtimePhrase :: Int -> Int -> [String] -> Int -> String
timePhrase daysPerWeek hoursPerDay xs n =
intercalate
", "
timePhrase xs n = intercalate ", " (foldr timeTags [] (zip (weekParts daysPerWeek hoursPerDay n) xs))
 
timeTags :: (Int, String) -> [String] -> [String]
Line 1,796 ⟶ 1,802:
| otherwise = xs
 
weekParts :: Int -> Int -> Int -> [Int]
weekParts daysPerWeek hoursPerDay =
weekParts = snd . flip (mapAccumR byUnits) [0, 7daysPerWeek, 24hoursPerDay, 60, 60]
 
byUnits :: Int -> Int -> (Int, Int)
Line 1,806 ⟶ 1,813:
| otherwise = (1, rest)
 
---------------------------- TEST-- --------------------------
main :: IO ()
main = do
mapM_let (putStrLnnames . flip translation= "wk d hr min sec") [7259, 86400, 6000000]</lang>
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}}
<pre>Assuming 24/7:
<pre>7259 -> 2 hr, 59 sec
86400 -> 1 d
6000000 -> 9 wk, 6 d, 10 hr, 40 min</pre>
 
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}}==
9,659

edits