javascriptfunctional-programmingcurryingpointfreeramda.js

How to convert simple average function in javascript to pointfree form using Ramda?


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


Solution

  • 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);