Jump to content

Convert seconds to compound duration: Difference between revisions

→‎JS ES6: Tidied.
(→‎JS ES6: Tidied.)
Line 2,217:
====ES6====
<lang JavaScript>(() => {
const"use main = () => {strict";
 
const localNames = ['wk', 'd', 'hr', 'min', 'sec'];
// ---------------------- GENERICCOMPOUND DURATION ----------------------
return [7259, 86400, 6E6]
.map(intSeconds =>
`${intSeconds} -> ${
compoundDuration(localNames)(intSeconds)
}`).join('\n');
};
 
// compoundDuration :: [String] -> Int -> String
const compoundDuration = labels =>
intSecondsnSeconds => weekParts(intSecondsnSeconds)
.map((v, i) => [v, labels[i]])
.reduce((a, x) =>
a.concat(x[0] ? ([
`${x[0]} ${x? [1] || '?'}`
]) : `${x[0]),} ${x[1] || "?"}`
'0' ] : a,[]
);, []
)
.join('", '"),;
 
 
// weekParts :: Int -> [Int]
// weekParts =:: intSecondsInt =-> [0, 7, 24, 60, 60Int]
const weekParts = nSeconds => [0, 7, 24, 60, 60]
.reduceRight((a, x) => {
const
r = a[0],
mod = x !== 0 ? r % x : r;
return Tuple((r - mod) / (x || 1))(
[mod].concat(a[1])
);
}, Tuple(intSeconds)([]))[1]
 
'1':return b,[
return Tuple( (r - mod) / (x || 1))(,
[mod], ...concat(a[1])]
}) ];
}, Tuple(intSeconds)([nSeconds, [])])[1];
 
// ---------------------- GENERIC ----------------------
// Tuple (,) :: a -> b -> (a, b)
const Tuple = a =>
b => ({
type: 'Tuple',
'0': a,
'1': b,
length: 2
});
 
// ---------------------- TEST -----------------------
// ---
// main :: IO ()
const Tuplemain = a() => {
const localNames = ['"wk'", '"d'", '"hr'", '"min'", '"sec'"];
 
return [7259, 86400, 6E6]
.map(intSecondsnSeconds =>
`${intSecondsnSeconds} -> ${
compoundDuration(localNames)(intSecondsnSeconds)
}`).join('"\n'");
};
 
// MAIN ---
return main();
})();</lang>
9,655

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.