JSFiddle - React, Tailwind, and code Playground

by pz_godlin

JavaScript

_ = {};

log = function (p) { console.log(p); }

Function.prototype.sequence = function ()
{
    var funcs = Array.prototype.slice.call(arguments);
    funcs.unshift(this);
    return function ()
    {
        var args = arguments;
        for (var i = 0; i < funcs.length; ++i)
            args = [funcs[i].apply(this, args)];
            
        return args[0];
    }
}

Function.prototype.compose = function ()
{
    var funcs = Array.prototype.slice.call(arguments).reverse();
    funcs.push(this);
    return function ()
    {
        var args = arguments;
        for (var i = 0; i < funcs.length; ++i)
            args = [funcs[i].apply(this, args)];
            
        return args[0];
    }
}
Function.prototype.curry = function (length)
{
    var fn = this;
    var args =  [];
    length   = length || fn.length;
    for (var i = 0; i < length; i++) args[i] = _;
    
    return  function ()
    {
        var offset = args.indexOf(_);
        for (var j = 0; offset >= 0 && j< arguments.length; j++) 
        {
            if (arguments[j] !== _)  args[offset] = arguments[j];  
            offset = args.indexOf(_, offset+1);
        }        
        return ( args.indexOf(_) < 0 ) ? fn.apply(this, args) : arguments.callee;
    };   
}

Array.prototype.sequence = function ()
{
   return Function.prototype.sequence.apply(this.shift(), this); 
}

Array.prototype.compose = function ()
{
   return Function.prototype.compose.apply(this.shift(), this); 
}

Array.prototype.mapRange = function (fn)
{
    var res = [];
    for (var i=this[0]; i<=this[1]; ++i)
        res[i] = fn(i);
    return res;
}

function inc (x)  { return x+1; }
function double (x)  { return x*2; }

var dbl_inc = [double, inc].sequence();
var r1 = [1,10].mapRange(dbl_inc);

log(r1)


/*
var obj = {
      param : 'Test'
    , fn    : (function(a, b, c) { return [c, b, a]; }).curry()
}

var f1 = (function(a, b, c) { return [a, b, c]; }).curry();

//log(obj.fn(_, _, 3)(_, 2)(1));

var a = function ( x ) {...