Skip to content

Instantly share code, notes, and snippets.

@FireNeslo
Last active December 19, 2015 21:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FireNeslo/6023804 to your computer and use it in GitHub Desktop.
Save FireNeslo/6023804 to your computer and use it in GitHub Desktop.
Weird string to function thing using to-Function
var fn = require('to-function');
function toObject(keys,values) {
  var rv = {};
  for (var i = 0; i < keys.length; ++i)
    if (keys[i] !== undefined) rv[keys[i]] = values[i];
  return rv;
}
function h(f) {
var args = f.split("=>")[0].split(",");
var func = f.split("=>")[1].split('-').map(function(fs){
args.forEach(function(v){
var exp = new RegExp(v,'g');
fs = fs.replace(exp, "_."+v);
console.log(v, exp);
})
fs = "_res = "+fs;
console.log(fs);
return fn(fs) ;
});
return function() {
var argobj = toObject(args, arguments);
var res;
func.forEach(function(f) {
res = f(argobj);
})
return res;
}
}
var point = h('i,j=>{x:i,y:j}');
var pmul = h(
'p,m=>p.x=p.x*m-'+
'p.y=p.y*m-'+
'p'
);
var three = h(
'x,y=>point(x,y)-'+
'pmul(_._res,3)'
)
module.exports = {h:h};
// and so:
three(1,1) // -> {x:3,y:3}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment