4 ms·
> Finally we’re finished! A little experimentation will convince you that this zoop thing we just wrote is in fact just foldr zoop _ b [] = b z
by rovolo 7y ago
> Finally we’re finished! A little experimentation will convince you that this zoop thing we just wrote is in fact just foldr
zoop _ b [] = b
zoop abb b (a : as) = abb a $ zoop abb b as
It's difficult to understand the function because the author deliberately named everything just based on their type to show off this method of autocomplete. This is what the function would look like with the same names as ramda's ReduceRight:
reduceRight _ acc [] = acc
reduceRight fn acc (head : tail) =
fn head $ reduceRight fn acc tail
https://ramdajs.com/docs/#reduceRight https://ramdajs.com/docs/#reduceRight
- z3t4 7y agoJavaScript already have the reduceRight method on the Array.prototype. But here it is anyway as promised: function reduceRight(arr, value, reduceFunc) { if(typeof value == "function" && reduceFunc == undefined) { reduceFunc = value; value = 0; } for(var i=arr.length; i--; i<-1) { value = reduceFunc(value, arr[i]); } return value; } assert( reduceRight([1,2,3], (a,b)=>a+b) , 6); assert( reduceRight([1,2,3], 1, (a,b)=>a+b) , 7);
- z3t4 7y agoNext iteration looks like this. Note that I added another test when I made the update to the function. function reduceRight(arr, value, reduceFunc) { if(typeof value == "function" && reduceFunc == undefined) { reduceFunc = value; value = arr.pop(); } for(var i=arr.length; i--; i<-1) { value = reduceFunc(value, arr[i]); } return value; } assert( reduceRight([1,2,3], (a,b)=>a+b) , 6); assert( reduceRight([1,2,3], 1, (a,b)=>a+b) , 7); assert( reduceRight([1,2,3], (a,b)=>a-b) , 0);