Convert seconds to compound duration: Difference between revisions
Content added Content deleted
Drkameleon (talk | contribs) |
(→JS ES6: Tidied.) |
||
Line 2,217: | Line 2,217: | ||
====ES6==== |
====ES6==== |
||
<lang JavaScript>(() => { |
<lang JavaScript>(() => { |
||
"use strict"; |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
// compoundDuration :: [String] -> Int -> String |
// compoundDuration :: [String] -> Int -> String |
||
const compoundDuration = labels => |
const compoundDuration = labels => |
||
nSeconds => weekParts(nSeconds) |
|||
.map((v, i) => [v, labels[i]]) |
.map((v, i) => [v, labels[i]]) |
||
.reduce((a, x) => |
.reduce((a, x) => |
||
a.concat( |
a.concat( |
||
x[0] ? [ |
|||
`${x[0]} ${x[1] || "?"}` |
|||
⚫ | |||
⚫ | |||
) |
) |
||
.join( |
.join(", "); |
||
// weekParts :: Int -> [Int] |
|||
// weekParts :: Int -> [Int] |
|||
const weekParts = nSeconds => [0, 7, 24, 60, 60] |
|||
.reduceRight((a, x) => { |
.reduceRight((a, x) => { |
||
const |
const |
||
r = a[0], |
r = a[0], |
||
mod = x !== 0 ? r % x : r; |
mod = x !== 0 ? r % x : r; |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
// Tuple (,) :: a -> b -> (a, b) |
|||
⚫ | |||
b => ({ |
|||
type: 'Tuple', |
|||
⚫ | |||
⚫ | |||
length: 2 |
|||
⚫ | |||
// ---------------------- TEST ----------------------- |
|||
⚫ | |||
// main :: IO () |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
return main(); |
return main(); |
||
})();</lang> |
})();</lang> |