Comments (6)
This is awesome! Thanks!
I just tested with this:
function fact(n, acc = 1) {
return n > 1 ? fact(n - 1, n * acc) : acc;
}
and with some nested ternary expressions, which have all worked perfectly.
I notice that if I write it as an arrow function, it doesn't get converted:
var fact = (n, acc = 1) => n > 1 ? fact(n - 1, n * acc) : acc;
output:
var fact = function fact(n) {
var acc = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
return n > 1 ? fact(n - 1, n * acc) : acc;
};
while if I write an arrow function with if
/else
, it does get converted:
var fact = (n, acc = 1) => {
if (n > 1) {
return fact(n - 1, n * acc);
}
return acc;
};
output:
var fact = function fact(n) {
var acc = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
var _repeat = true;
var _n, _acc;
while (_repeat) {
_repeat = false;
if (n > 1) {
_n = n - 1;
_acc = n * acc;
n = _n;
acc = _acc;
_repeat = true;
continue;
}
return acc;
}
};
Maybe an ordering issue in the transform?
from babel-plugin-tailcall-optimization.
I think your second example is wrong, because it's not recursive.
But, yeah, I don't think the ternary operator is supported. I'd really like it to be supported, as many algorithms are more naturally written that way. I expect it wouldn't be too hard: I would guess you could convert the ternary to an if/else prior to testing if the function is recursive, and bail out if it's not.
from babel-plugin-tailcall-optimization.
So sorry. When I typed up that example I renamed some things at the last moment and forgot to rename countJumps to countJumpsTernary in that last line.
You are correct, that was wrong.
I know there's a plugin that transforms ternary to if/else, so I'll experiment with that as well. Thanks for the feedback 👍
const countJumps = (list, index, step) => {
const value = list.get(index);
if (value === undefined) return step;
return countJumps(list.set(index, value + 1), index + value, step + 1);
};
const countJumpsTernary = (list, index, step) => {
const value = list.get(index);
return value === undefined
? step
: countJumpsTernary(list.set(index, value + 1), index + value, step + 1);
};
Produces
const countJumps = (list, index, step) => {
var _repeat3 = true;
var _list3, _index3, _step3;
while (_repeat3) {
_repeat3 = false;
const value = list.get(index);
if (value === undefined) return step;
_list3 = list.set(index, value + 1);
_index3 = index + value;
_step3 = step + 1;
list = _list3;
index = _index3;
step = _step3;
_repeat3 = true;
continue;
}
};
const countJumpsTernary = (list, index, step) => {
const value = list.get(index);
return value === undefined ? step : countJumpsTernary(list.set(index, value + 1), index + value, step + 1);
};
from babel-plugin-tailcall-optimization.
Ah shucks, just realized that babel-plugin-transform-ternary-to-if-else
transforms a ternary to an if else nested in an IIFE so scratch that.
from babel-plugin-tailcall-optimization.
Hey @DanHodges @acthp Support for ternary landed in #11 I am gonna release new version to NPM now. Stay tight.
from babel-plugin-tailcall-optimization.
Okay 1.0.12
is out. Please reopen this issue if it didn't solve your problems.
from babel-plugin-tailcall-optimization.
Related Issues (15)
- Plugin breaks on `export default function` due to null pointer HOT 4
- Didn't ES2015 introduce tail call optimization? HOT 3
- Support babel 7 HOT 9
- does not work with concise body syntax (implied return) in arrow function
- Can a Babel 7 release be awaited? HOT 4
- Discussion of adding TCO when closure is detected HOT 8
- How the conversion is helping in TCO ? HOT 1
- -
- -
- -
- dependencies that should be dev HOT 1
- Discussion about mutual recursion
- invalid code generated due to missing semicolons HOT 1
- Doesn't handle the ternary operator HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from babel-plugin-tailcall-optimization.