Revisions
-
codelahoma revised this gist
Jun 16, 2013 . 1 changed file with 27 additions and 30 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,30 +1,27 @@ # moved internal properties to arguments.callee so # we don't have to worry about name collisions with our # context. tco = (fn) -> (args...) -> my = arguments.callee my.recurred = false my.args = [] my.recur = (args...) => my.recurred = true my.args = args returnVal = fn.apply this, args while true if my.recurred tempArgs = my.args my.args = undefined my.recurred = false returnVal = fn.apply this, tempArgs else return returnVal @@ -34,20 +31,20 @@ factorial = tco (n, acc = 1) -> if n is 0 acc else factorial.recur n - 1, acc * n console.log factorial 100000 #============================================== console.log factorial 60 factorialWithCtx = tco (n, acc = 1) -> if n is 0 acc else @iterations++ factorialWithCtx.recur n - 1, acc * n counter = iterations: 0 -
adrusi revised this gist
Feb 25, 2012 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -36,7 +36,7 @@ factorial = tco (n, acc = 1) -> else @recur n - 1, acc * n console.log factorial 100000 #============================================== @@ -51,5 +51,5 @@ factorialWithCtx = tco (n, acc = 1) -> counter = iterations: 0 console.log factorialWithCtx.call counter, 100000 console.log counter.iterations -
adrusi revised this gist
Feb 25, 2012 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -29,15 +29,15 @@ tco = (fn) -> (args...) -> #============================================== factorial = tco (n, acc = 1) -> if n is 0 acc else @recur n - 1, acc * n console.log factorial 100_000 #============================================== -
adrusi created this gist
Feb 25, 2012 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,55 @@ tco = (fn) -> (args...) -> if @recured? or @args? or @recur? unless arguments.callee.noWarn # here we are taking precautionary measures to make sure we don't # accidentally overwrite some important properties in the context named # "recured", "args" or "recur". The warnings can be disabled by setting # the `noWarn` property of the function to true throw new Error """ calling a tail-recursive function in this context will overwrite the properties "recured", "args" and "recur". """ @recured = false @args = [] @recur = (args...) => @recured = true @args = args returnVal = fn.apply this, args while true if @recured tempArgs = @args @args = undefined @recured = false returnVal = fn.apply this, tempArgs else return returnVal ############################################### factorial = tco (n, acc = 1) -> if n is 0 acc else @recur n - 1, acc * n console.log factorial 100_000 ############################################### factorialWithCtx = tco (n, acc = 1) -> if n is 0 acc else @iterations++ @recur n - 1, acc * n counter = iterations: 0 console.log factorialWithCtx.call counter, 100_000 console.log counter.iterations