Show HN: Tacopy – Tail Call Optimization for Python

github.com

32 points by raaid-rt 5 days ago


dkersten - 33 minutes ago

Once upon a time I tried to write such a decorator too in python 2.x and the byteplay bytecode disassembler library. I was trying to do the conversion at the bytecode level instead of transforming the AST. I believe I got as far as detecting simple self recursive functions, but never actually managed to implement the actual transformation.

srean - an hour ago

> Tacopy is a Python library that provides a decorator to optimize tail-recursive functions by transforming them into iterative loops.

Can this handle mutually recursive calls ? Because those are mostly the only place I use tail calls, rest I translate to iterative loops, list comprehension, maps and reduces.

javierbg95 - an hour ago

Really cool project, fairly succinct and to the point :)

I would love to see support for arbitrarily nested functions, as it is common to wrap these into a public API function without the iteration parameters.

phplovesong - an hour ago

TCO can be implemented easily in non TC optimized langauges with a trampoline wrapper.

Why do i need a fully fledged library for something that is basically a few lines of code?

anilakar - 3 hours ago

> This eliminates the risk of stack overflow errors

When you get stack overflows anywhere from a thousand down to fifty(!) frames in the stack it's not a risk, it's an inevitability in anything more complex than a programming tutorial.

Yeah, I've been bitten by this in production. Writing the functionality in a clean iterative style was just too much of a hassle.