Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

trampoline #1238

Open
char0n opened this issue Dec 24, 2019 · 0 comments
Open

trampoline #1238

char0n opened this issue Dec 24, 2019 · 0 comments

Comments

@char0n
Copy link
Owner

char0n commented Dec 24, 2019

Is your feature request related to a problem? Please describe.

All that trampoline does is repeatedly call the return value of a function until it’s no longer a function

Describe the solution you'd like

function evenOline(n) {
  if (n === 0)
    return true;
  else
    return partial1(oddOline, Math.abs(n) - 1);
}

function oddOline(n) {
  if (n === 0)
    return false;
  else
    return partial1(evenOline, Math.abs(n) - 1);
}

trampoline(oddOline, 3);
//=> true
trampoline(evenOline, 200000);
//=> true

trampoline(oddOline, 300000);
//=> false

trampoline(evenOline, 200000000);
// wait a few seconds
//=> true

Describe alternatives you've considered

Additional context

The code and exmplanation comes from book Functional Javascript by Michael Fogus.
Possible implementation for the function:

function trampoline(fun /*, args */) {
  var result = fun.apply(fun, _.rest(arguments));

  while (_.isFunction(result)) {
    result = result();
  }

  return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant