Show HN: Tacopy – Tail Call Optimization for Python
32 points
5 days ago
| 5 comments
| github.com
| HN
dkersten
31 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.
reply
srean
49 minutes 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.

reply
pansa2
36 minutes ago
[-]
> Limitations […] No mutual recursion: Only direct self-recursion is optimized
reply
javierbg95
49 minutes 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.

reply
phplovesong
1 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?

reply
srean
53 minutes ago
[-]
There's quite a bit of overhead.

I believe Clojure does it with trampoline as JVM does not (as far as I know) does not support tail call optimization. Ironic, given Guy Steele.

reply
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.

reply