r/node • • Apr 14 '24

JavaScript Functions - 4 Ways

Post image
208 Upvotes

79 comments sorted by

View all comments

-1

u/SponsoredByMLGMtnDew Apr 14 '24

I don't think I would've used arrow functions as the final two ways.

Definitely would've gone with partial application

let __map = [].map;

function map (list, unaryFn) {
  return __map.call(list, unaryFn);
};

function square (n) {
  return n * n;
};

map([1, 2, 3], square);

and currying

function curry (binaryFn) {
  return function (firstArg) {
    return function (secondArg) {
      return binaryFn(firstArg, secondArg);
    };
  };
};

let curriedMap = curry(map),
    double = function (n) { return n + n; };

let oneToThreeEach = curriedMap([1, 2, 3]);

oneToThreeEach(square);
  //=> [1, 4, 9]
oneToThreeEach(double);
  //=> [2, 4, 6]

source

I don't think I would've used arrow functions as the final two ways.