You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am sad to say, but I don't think that your readable-operators are actually readable 馃樋. You see, operator to me means inline function call, but that is in fact the opposite of what your library does:
Judging by the name, what I actually thought that your library would do was to give me a way to define my own inline operators.
You might not be aware of this, but it is actually possible to redefine the built in operators in javascript! Here are a couple of ways to do so:
valueOf
Whenever you use one of the operators + - * / % & | ^ << >> >>> < <= > >= on an object, javascript will call the valueOf function on your object. Here I exploit this to redefine + to mean *:
functionMul(x){this.val=x;this.valueOf=function(){if(Mul.others===undefined)Mul.others=[];letsum=0,prod=1;for(letxofMul.others){sum+=x;prod*=x;}Mul.others.push(this.val);returnprod*this.val-sum;};}consta=newMul(2);constb=newMul(3);// Jokes on you javascript- addition is now multiplication!console.log(a+b);// prints 6 (2*3), not 5 (2+3)
Proxies
Proxies let you intercept has, get and set, which is used to look for members of your object when you do obj.member, so you can use it to redefine ., = and things like += and *=! Here I redefine . to mean function-composition:
letpfree=ctx=>f=>newProxy(f,{has: (t,p)=>pint||pinctx,get: (t,p)=>{letk=pint? t[p]: ctx[p];if(kinstanceofFunction)return(functionfetch(_k){returnpfree(ctx)(x=>(q=>qinstanceofFunction
? fetch(q)
: t(q))(_k(x)))})(k);returnk;}});with(pfree(global)(x=>x)){add=x=>y=>x+y;inc=add(1);addTwo=inc.incaddSix=addTwo.add(3).incconsole.log(addTwo(3));// Prints 5, same as inc(inc(3))console.log(addSix(2));// Prints 8, same as addTwo(add(3, inc(2)))}
Detecting operators
In the example where I redefined + to mean *, I also redefined all the other operators too! How do we find out which operator was used when javascript only calls valueOf (This also applies to proxies when you do += and similar.)? Why you look at the answer that javascript produces of course!
functionMul(x){this.val=x;this.valueOf=function(){if(Mul.prod===undefined)Mul.prod=1;if(Mul.sum===undefined)Mul.sum=0;Mul.prod*=this.val;Mul.sum+=this.val;return1;};}Mul.answ=function(witness){letsum=Mul.sum,prod=Mul.prod;Mul.sum=0;Mul.prod=1;if(witness==1)returnsum;// * was used, return sum!if(witness!=1)returnprod;// + was used, return product!};consta=newMul(2);constb=newMul(3);console.log(Mul.answ(a+b));// Prints 6 (a*b)console.log(Mul.answ(a*b));// Prints 5 (a+b)
The text was updated successfully, but these errors were encountered:
I am sad to say, but I don't think that your readable-operators are actually readable 馃樋. You see, operator to me means inline function call, but that is in fact the opposite of what your library does:
Inline:
Readable-operators:
Judging by the name, what I actually thought that your library would do was to give me a way to define my own inline operators.
You might not be aware of this, but it is actually possible to redefine the built in operators in javascript! Here are a couple of ways to do so:
valueOf
Whenever you use one of the operators
+ - * / % & | ^ << >> >>> < <= > >=
on an object, javascript will call the valueOf function on your object. Here I exploit this to redefine+
to mean*
:Proxies
Proxies let you intercept
has
,get
andset
, which is used to look for members of your object when you doobj.member
, so you can use it to redefine.
,=
and things like+=
and*=
! Here I redefine.
to mean function-composition:Detecting operators
In the example where I redefined
+
to mean*
, I also redefined all the other operators too! How do we find out which operator was used when javascript only callsvalueOf
(This also applies to proxies when you do+=
and similar.)? Why you look at the answer that javascript produces of course!The text was updated successfully, but these errors were encountered: