These are chat archives for ramda/ramda

7th
Apr 2015
Scott Sauyet
@CrossEye
Apr 07 2015 00:54

@JAForbes: I've never seen that pattern before. Very clever. Used like this, it can be thought of as a one-liner at the beginning of any function, with only the substitutions for the function name required:

function f(a,b,c){
  if (arguments.length < f.length) {return f.bind.apply(f,[ null].concat([].slice.apply(arguments)));}

  return a + b + c;
}

And even that second instance could easily be replaced with 'Function'.

James Forbes
@JAForbes
Apr 07 2015 04:11

@CrossEye Yes. That is why I liked it so much. I prefer your version too: Easy pickings for a macro/ transpiler.

I only wish there was a this that referenced the current function, so it wasn't necessary to update the function reference in your top line. Maybe there is something like that in ES6, I'm not up to speed.

James Forbes
@JAForbes
Apr 07 2015 04:35
I guess there is no point in doing this. As once you define a new func, you may as well define curry. But if we did want to take the abstraction further...
function later(f, args){
  return args.length < f.length && 
    f.bind.apply(f,[null].concat([].slice.apply(args)))
}


function f(a,b,c){
  var _later;  if( _later = later(f, arguments) ) return _later;
  return a + b + c
}
James Forbes
@JAForbes
Apr 07 2015 04:42

I like that despite the fact that f is curried. You can still inspect the body.

Were I to use R.curry(f). We get

function (a0, a1) {
    void a1;
    return fn.apply(this, arguments);
}

Sure you could override the curried function's toString and use f+"" but the other approach doesn't need any tricks.