summaryrefslogtreecommitdiff
path: root/4-kyu/human-readable-duration-format.hs
blob: d506d2a33d99919f56c46749ad68ec96e55e29ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
-- https://www.codewars.com/kata/52742f58faf5485cae000b9a
module FormatDuration where

import Data.List

units = ["second", "minute", "hour", "day", "year"]

unitsLen :: (Integral i) => [i]
unitsLen = [60, 60, 24, 365]

seris [] cur ret = reverse (cur:ret)
seris (x:xs) cur ret = seris xs (cur `div` x) ((cur `mod` x):ret)

zipUnit nums = filter (/="") $ zipWith fn nums units
  where
    fn 0 unit = ""
    fn 1 unit = "1 " ++ unit
    fn n unit = (show n) ++ " " ++ unit ++ "s"
                
joinUnits :: [String] -> String
joinUnits [x] = x
joinUnits (x:xs) = (intercalate ", " $ reverse xs) ++ " and " ++ x

formatDuration :: (Integral i, Show i) => i -> String
formatDuration n = if toInteger n == 0 then "now"
                   else joinUnits $ zipUnit $ seris unitsLen n []