Giter Club home page Giter Club logo

Comments (6)

acthp avatar acthp commented on July 3, 2024 1

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.

acthp avatar acthp commented on July 3, 2024

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.

DanHodges avatar DanHodges commented on July 3, 2024

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.

DanHodges avatar DanHodges commented on July 3, 2024

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.

krzkaczor avatar krzkaczor commented on July 3, 2024

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.

krzkaczor avatar krzkaczor commented on July 3, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.