Convert seconds to compound duration: Difference between revisions

Content added Content deleted
(add Standard ML)
m (→‎{{header|Haskell}}: Tidied parameterised variant.)
Line 1,847: Line 1,847:
<lang haskell>import Data.List (intercalate, mapAccumR)
<lang haskell>import Data.List (intercalate, mapAccumR)


----------------- COMPOUND DURATION STRINGS ----------------
---------------- COMPOUND DURATION STRINGS ---------------


durationString :: String -> String -> Int -> Int -> [String] -> Int -> String
durationString ::
String ->
durationString componentGap numberLabelGap daysPerWeek hoursPerDay xs n =
String ->
intercalate
Int ->
componentGap
(foldr
Int ->
[String] ->
(timeTags numberLabelGap)
[]
Int ->
String
(zip (weekParts daysPerWeek hoursPerDay n) xs))
durationString
componentGap
numberLabelGap
daysPerWeek
hoursPerDay
xs
n =
intercalate
componentGap
( foldr
(timeTags numberLabelGap)
[]
(zip (weekParts daysPerWeek hoursPerDay n) xs)
)


timeTags :: String -> (Int, String) -> [String] -> [String]
timeTags :: String -> (Int, String) -> [String] -> [String]
Line 1,865: Line 1,879:
weekParts :: Int -> Int -> Int -> [Int]
weekParts :: Int -> Int -> Int -> [Int]
weekParts daysPerWeek hoursPerDay =
weekParts daysPerWeek hoursPerDay =
snd
snd . flip (mapAccumR byUnits) [0, daysPerWeek, hoursPerDay, 60, 60]
. flip
(mapAccumR byUnits)
[0, daysPerWeek, hoursPerDay, 60, 60]


byUnits :: Int -> Int -> (Int, Int)
byUnits :: Int -> Int -> (Int, Int)
Line 1,874: Line 1,891:
| otherwise = (1, rest)
| otherwise = (1, rest)


---------------------------- TEST --------------------------
--------------------------- TEST -------------------------


translation :: String -> Int -> Int -> Int -> String
translation :: String -> Int -> Int -> Int -> String
translation local daysPerWeek hoursPerDay n =
translation local daysPerWeek hoursPerDay n =
intercalate " -> " $
intercalate " -> " $
[ show,
[show, durationString ", " " " daysPerWeek hoursPerDay (words local)] <*> [n]
durationString
", "
" "
daysPerWeek
hoursPerDay
(words local)
]
<*> [n]


main :: IO ()
main :: IO ()