Opposite of Array.reduce() in javascript -


array.reduce() takes array , combines elements array accumulator until elements consumed.

is there function (often called "unfold" in other languages) starts value , keeps generating elements until complete array produced (the accumulator depleted)?

i trying part of converting between arbitrary bases. code have follows, eliminate raw loop.

var dstalphabet = "0123456789abcdefgh"; var dstbase = dstalphabet.length; var wet = biginteger(100308923948716816384684613592839); var digits_reversed = []; while (wet.ispositive()) {   // var digitval = wet % dstbase   var divrem = wet.divrem(dstbase); // [result of division, remainder]   wet = divrem[0];   digits_reversed.push(dstalphabet.charat(divrem[1].tojsvalue())); }  return digits_reversed.reverse().join(""); 

tewathia's comment seems idiomatic way it, guess if want hard way, write own recursive primitive, like:

function unreduce(accumulator, operation, stoppredicate, ret) {     return helper([accumulator, ret])[1]      function helper(vals) {         if (stoppredicate(vals[0])) return vals[1];          return helper(operation(vals[0], vals[1]));     } } 

which might want modify bit preserve this callbacks.

i'm not sure how great is. it's kind of awkward operation callback needing update both accumulator , return values. outer function can't save operation having return length-2 array.


Comments

Popular posts from this blog

user interface - How to replace the Python logo in a Tkinter-based Python GUI app? -

objective c - Greedy NSProgressIndicator Allocation -

how to set an OCR language in Google Drive -