JSFiddle - React, Tailwind, and code Playground

by Martin MalĂ˝

JavaScript

foldl = function(xs, fn, init) {
    var result = init;
    if (xs === null) {return result;}
    for (var i=0; i<xs.length; i++) {
      result = fn(result, xs[i]);
    };
    return result;
  };    

foldlize = function(fn, init) {
    return function(xs) {
        return foldl(xs,fn,init);
    };
};


sum = function(xs) {
    return foldl(xs, 
                 function(a,b) {
                     return a+b;
                 },
                 0);
};
  
prod = function(xs) {
    return foldl(xs, 
                 function(a,b) {
                     return a*b;
                 },
                 1);
};

map = function (xs, fn) {
    return foldl(xs,
                 function(rs,x) {
                     rs.push(fn(x));
                     return rs;
                 },
                 []);
};

each = function (xs, fn) {
    return foldl(xs,
                 function(_,x) {
                     fn(x);
                     return _;
                 },
                 null);
};

var a = [1, 2, 3];

y = map(a, function(a){return a*2;});

each(y, function(x){alert(x);});