Consider the problem of decomposing milliseconds into readable units of time. Imagine you had a function that did this
> breakupMillis(100000000)
Array [ 0, 40, 46, 3, 1 ]
means that 100 million milliseconds is exactly 1 day, 3 hours, 46 minutes and 40 seconds.
The function can be generalized by adopting an array of modules, like this
> breakup(100000000, [1000, 60, 60, 24])
Array [ 0, 40, 46, 3, 1 ]
This function can be used (hypothetically) for other things:
> breakup(1000, [8, 8, 8])
Array [ 0, 5, 7, 1 ]
means 1000 in decimal is 01750 in octal.
Here is what I wrote for this:
const breakup = (n, l) => l.map(p =>
{ const q = n % p; n = (n - q) / p; return q; }).concat(n);
This feature is beautiful, it is even referentially transparent, but I have two completely aesthetic complaints.
map. This seems to work for reduce, although I do not understand how to do it.- rewriting a variable
n. I generally do not like to use var; using a secret varmakes it worse.
. , ( )? map , .