Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Tail call optimization in Python

Name: Anonymous 2007-09-27 17:42 ID:4a99BlE3

Please critique this tail call optimizing decorator I've just written. I've tried to fix the pitfalls of other proposed decorators, and the result is this one that supports mutual recursion, does not use exceptions, stack inspection or any implementation-dependent hack, and is pretty short and fast - the fastest out of the ones I could find and try. (It's, incidentally, the simplest @tailcall implementation to me as well.) In fact, in tail-recursive environments I tested, the impact of using the decorator is difficult to even measure, as the extra time the decorator takes to run is (I suppose) saved by the better use of cache memory. The only caveat it has is that if used in a function that's not called in a tail-recursive fashion, bad things will happen. How to use? Simple: just decorate the shit out of your tail-recursive functions.

def tailcall(f):
    '''Decorator that implements tail call optimization.
    Supports cooperative tail call - even when only one of the functions
    is decorated, and all exceptions pass through it. You can tell the
    functions that have been tail optimized because they have "tco" before
    them in the stack frame.
    The only caveat is that if you attempt to decorate a function that
    isn't called in a tail recursive fashion from another decorated
    function, you'll get wrong results.'''
   
    tdata = [False] #[Optimizing?]
    DO_CALL = object() #Call marker
    def tco(*a, **aa):
        if tdata[0]:
            return DO_CALL, a, aa
        else:
            tdata[0] = True
            ret = f(*a, **aa)
            while type(ret) is tuple and ret and ret[0] is DO_CALL:
                ret = f(*ret[1], **ret[2])
            tdata[0] = False
            return ret
    tco.__doc__ = f.__doc__
    return tco

Name: Anonymous 2007-09-27 18:27 ID:xYszicY/

This seems pretty nice and clean.

Name: Anonymous 2007-09-27 18:52 ID:e9jscdN9

Eight words: the neat, completely optional choice of code indentation.

Name: Anonymous 2007-09-27 18:53 ID:vq+iq5Qm

>>3
i lold hard

Name: Anonymous 2007-09-27 19:45 ID:Pgfmp4RW

>>3
that;s 10 words

Name: Anonymous 2007-09-27 20:20 ID:Fe0A94Qo

>>5
YHL HAND

Name: Anonymous 2007-09-27 20:20 ID:L87peaO6

>>5
It doesn't include the words before the break that is denoted by the colon

Name: Anonymous 2007-09-27 20:31 ID:saMTq9hr

OH GOD JUST FUCKING USE GOTO ALREADY WTF

Name: Anonymous 2007-09-27 22:40 ID:tXF2tfA8

>>8
NIH

Name: Anonymous 2007-09-27 23:12 ID:e9jscdN9

goto >>10

Name: Anonymous 2007-09-28 1:10 ID:QFaTN8AI

O SHI- NEVER ENDING RECURSION!!! QUICK! SOMEONE KILL THE PROCESS!

Name: Anonymous 2007-09-28 1:17 ID:r7l4v5Hv

Name: Anonymous 2009-01-14 12:19

LISP

Name: Anonymous 2009-01-14 12:20

shit.

Name: Anonymous 2012-06-25 23:38

䊑唰頉䈷㌤䚃杢ጷ㎇酁Ֆኆŕ䌣᝹̔㉵銔鐠ᕁ礖桱鈨㚇犑聤饙荈ᑤ└璀㊐ငݱ㘢呆ޙᕹԡ֘☩䙒处朹⌃蜣ᤇ0䑅ᝃܗ椀执ष❴蜦椶ₘ梐桸⠱墅蜵榒း䘓芒偣㑶ᔥ䈘愁煵兢ᝀ逷斒⠘␢㠇㖑吆ख़奷᝕剃妈䝰䑧匒癈椠䡁需∃㥅␑陲፩咁陗ᒄᙔ⤶悘➀锣噷䍠䅉倩犄ᤧ逩㤔夨攴慕霤⢑م㐴ቕ⍳㝤核ℨ䀷荂酡̱㊈瀧塂㎘⦀փ熆⤃碂鈁╰ᕖ虶吩ݡ阡袁蘄ॐť↑荖㤔萗虀陆夃䥲唳᝸䌉萱䔀㤄䙱聣䥶莈阗㈇䉕憀榃鍳愥敘❤▁颗┈ʈᜤ㐥昐兩搢礔㎒⤠葑奧癠ᤐ癐蔉䙓鐤┣᠔鑱䚔坈ᦃ垔酔㐦⁶ለᅩ挄շ偄噁ㄨյ䌲榘㝳蝒聣⡆楇䠢䕸脨敨䐥᝖Ͷ塓玁⡘࠷⤐ͥ扁☤♨煡㌔舠ٖᥴ㜉⡑搣瑈儡ጰ㜰䒖聤ॐ瀥ᐈ㑣ሴ煘卅析䔉瀱荗ळڙ䤃㝩Ĉ虐阖刲ޙ㠢⍘œ蒔剑桠靖㌸ᄕ╣啥吥㒁ᥗ锓ᖆ✈划倗㑙倧㚆䐘鍷⡀㝉

Don't change these.
Name: Email:
Entire Thread Thread List