convert some function similar to tail recursive function

convert some functions similar to tail recursive function.
test_optimize.coffee have a test passed.