How do I convert the following simple average function to pointfree form (using Ramda)?
var _average = function(xs) {
return R.reduce(R.add, 0, xs) / xs.length;
};
I've been this for a while now, but the R.divide function is throwing me off since the numerator and the denominator requires evaluation first
Using R.converge:
// average :: Array Number -> Number
const average = R.converge(R.divide, [R.sum, R.length]);
Using R.lift (which a more generally applicable function than R.converge):
// average :: Array Number -> Number
const average = R.lift(R.divide)(R.sum, R.length);
Here's one way to do it:
let xs = [5, 5];
let average = R.compose(R.apply(R.divide), R.juxt([R.sum, R.length]));
console.log(average(xs));
<script src="//cdn.jsdelivr.net/ramda/latest/ramda.min.js"></script>
Basically, R.juxt maps the array values into R.sum and R.length which gives you an array with the sum of the array and the length of the array. The result is applied to R.divide.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With