- Notifications
You must be signed in to change notification settings - Fork2
Oink is a Python to Javascript translator.
License
alecthomas/oink
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Oink attempts to translate Python source to idiomatic Javascript.
It consists of the translator itself and a JS runtime.
Here are a couple of examples fromProject Euler:
Python source for solution 1:
defeuler1():"""Add all the natural numbers below 1000 that are multiples of 3 or 5"""multiple=lambdax,y:x%y==0returnsum(xforxinxrange(1,1000)ifmultiple(x,3)ormultiple(x,5))printeuler1()
Runningoink euler1.py
on this example produces the following #"auto" data-snippet-clipboard-copy-content="/** Add all the natural numbers below 1000 that are multiples of 3 or 5 */function euler1() { var multiple = function (x, y) { return x % y == 0; }; return Oink.sum(Oink.listComprehension(Oink.range(1, 1000), function (x) { return x; }, function (x) { return multiple(x, 3) || multiple(x, 5); }));};console.log(euler1());">
/** Add all the natural numbers below 1000 that are multiples of 3 or 5 */functioneuler1(){varmultiple=function(x,y){returnx%y==0;};returnOink.sum(Oink.listComprehension(Oink.range(1,1000),function(x){returnx;},function(x){returnmultiple(x,3)||multiple(x,5);}));};console.log(euler1());
Python source for solution 6:
defeuler6(op,end):returnop(sum(xrange(end+1)))-sum(op(x)forxinxrange(end+1))printeuler6(lambdax:x**2,100)
Runningoink euler6.py
on this example produces the following #"auto" data-snippet-clipboard-copy-content="function euler6(op, end) { return op(Oink.sum(Oink.range(end + 1))) - Oink.sum(Oink.listComprehension(Oink.range(end + 1), function (x) { return op(x); }));};console.log(euler6(function (x) { return Math.pow(x, 2); }, 100));">
functioneuler6(op,end){returnop(Oink.sum(Oink.range(end+1)))-Oink.sum(Oink.listComprehension(Oink.range(end+1),function(x){returnop(x);}));};console.log(euler6(function(x){returnMath.pow(x,2);},100));