Skip to content

Instantly share code, notes, and snippets.

@codelahoma
Forked from adrusi/gist:1905351
Created June 16, 2013 22:55

Revisions

  1. codelahoma revised this gist Jun 16, 2013. 1 changed file with 27 additions and 30 deletions.
    57 changes: 27 additions & 30 deletions gistfile1.coffee
    Original file line number Diff line number Diff line change
    @@ -1,30 +1,27 @@
    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
    # 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
    @recur n - 1, acc * n
    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++
    @recur n - 1, acc * n
    factorialWithCtx.recur n - 1, acc * n

    counter =
    iterations: 0
  2. @adrusi adrusi revised this gist Feb 25, 2012. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions gistfile1.coffee
    Original 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 100_000
    console.log factorial 100000
    #==============================================


    @@ -51,5 +51,5 @@ factorialWithCtx = tco (n, acc = 1) ->

    counter =
    iterations: 0
    console.log factorialWithCtx.call counter, 100_000
    console.log factorialWithCtx.call counter, 100000
    console.log counter.iterations
  3. @adrusi adrusi revised this gist Feb 25, 2012. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions gistfile1.coffee
    Original 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
    ###############################################
    #==============================================



  4. @adrusi adrusi created this gist Feb 25, 2012.
    55 changes: 55 additions & 0 deletions gistfile1.coffee
    Original 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