Tail Call Optimization
Description:
Background
As a JS user, you know that you are limited in so many ways, so let's break the limit! In this kata, we are focusing on Tail Call.
Requirement
You are given fd
, an array of function descriptions. Your task is to return an array of functions that are able to handle tail calls properly.
Each of elements in fd
is a 3-length-array having the format [functionName, parameter, functionBody]
where
- functionName:
string
. The name of this function - parameter:
string[]
. The parameter names of this function, each elements is a identifier - functionBody:
string
. The body of this function
It's guaranteed that
- These functions would only invoke each others or built-in functions.
- These functions would only be invoked in the tail positions.
- All function bodys are valid, which means no error will occur if it is passed to the
new Function
constructor.
Example
var result = tco([['f',['x'],'return x ? f(--x) : "foo"']])
The result should contains a function functionally equals to function f(x){return x ? f(--x) : "foo"}
. Then we can test it with a large number
result[0](1e6) === 'foo'
So, why not just return function f(x){return x ? f(--x) : "foo"}
? Because
(function f(x){return x ? f(--x) : "foo"})(1e6)
throws RangeError: Maximum call stack size exceeded
.
If any functions throws an error, just let it go.
tco([['f',[],'throw "Whatever"']])[0]()
should throws "Whatever"
Checkout more in example tests.
Note
- The purpose of this kata has nothing to do with perfomance, but to test if it is able to handle tail calls, functions may be invoked for tens of thousands of times, you might need to be careful.
- I'm not 100 percent sure that my tests are correct. Please feel free to raise an issue.
- If the description above is not clear enough. Please feel free to question me.
Have Fun. O_o
Similar Kata:
Stats:
Created | Oct 14, 2017 |
Published | Oct 14, 2017 |
Warriors Trained | 1261 |
Total Skips | 431 |
Total Code Submissions | 1257 |
Total Times Completed | 150 |
JavaScript Completions | 147 |
CoffeeScript Completions | 6 |
Total Stars | 76 |
% of votes with a positive feedback rating | 99% of 42 |
Total "Very Satisfied" Votes | 41 |
Total "Somewhat Satisfied" Votes | 1 |
Total "Not Satisfied" Votes | 0 |
Total Rank Assessments | 3 |
Average Assessed Rank | 2 kyu |
Highest Assessed Rank | 2 kyu |
Lowest Assessed Rank | 2 kyu |