Last active
December 19, 2015 21:58
-
-
Save FireNeslo/6023804 to your computer and use it in GitHub Desktop.
Weird string to function thing using to-Function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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