https://github.com/vitia-fritelle/tail-code-optimization
Tail Code Optimization in JavaScript
https://github.com/vitia-fritelle/tail-code-optimization
javascript recursion tail-recursion
Last synced: 7 months ago
JSON representation
Tail Code Optimization in JavaScript
- Host: GitHub
- URL: https://github.com/vitia-fritelle/tail-code-optimization
- Owner: vitia-fritelle
- License: mit
- Created: 2022-11-14T12:24:10.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2022-11-14T12:58:30.000Z (over 3 years ago)
- Last Synced: 2025-04-09T21:42:37.019Z (11 months ago)
- Topics: javascript, recursion, tail-recursion
- Language: JavaScript
- Homepage:
- Size: 3.91 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Tail Code Optimization
It is often difficult to connect knowledge learned in recursion with practice.
Compilers associated with popular programming languages do not automatically transform recursive calls into iterative ones, and this ends up in a difficulty to efficiently implement recursive techniques.
Given the advantages of approaching the code recursively, such as brevity and reducing the number of states to be handled, it is worth finding a way to adapt the code in order to abstract the programming logic.
In this code, I present a way to implement the conversion for a specific case, tail recursion in JavaScript.
For using this code all you have to do is:
``` javascript
function recursiveFuction(recursiveFunctionParameters) {
if (someBaseCase) {
return new BaseCase(someValue);
} else if (anotherBaseCase) {
return new BaseCase(anotherValue);
} else {
return new TailCall(newArgs);
}
}
tco(recursiveFunction, recursiveFunctionArgs)
```