logoalt Hacker News

Show HN: Tacopy – Tail Call Optimization for Python

78 pointsby raaid-rtlast Sunday at 6:18 AM37 commentsview on HN

Comments

lukasnxyztoday at 1:54 PM

what is this, why is 95% of the file useless comments: https://github.com/raaidrt/tacopy/blob/main/src/tacopy/unpar...

you're calling a built in function, why make obfuscate this in an "llm way"

show 2 replies
denys_potapovtoday at 3:20 PM

Cool. Recursion in python is common bottleneck in competitive programming. Will give it a try. I created a similar tool for recursion [1]. But ended with rewriting AST and emulating stack. Pros - no need for accumulator, cons - almost unusable in real world.

[1] https://dev.to/denyspotapov/callonce-python-macro-for-unlimi...

show 1 reply
jagged-chiseltoday at 10:32 AM

Taco Py. No Ta Copy. Took my brain a minute or so …

dkerstentoday at 9:27 AM

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.

show 1 reply
sreantoday at 9:08 AM

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

show 1 reply
anilakartoday at 6:51 AM

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

javierbg95today at 9:09 AM

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.

show 1 reply
phplovesongtoday at 8:40 AM

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?

show 1 reply
busfahrertoday at 12:31 PM

Fun fact: In Scheme, TCO is required by the spec

show 1 reply
upghosttoday at 12:05 PM

Really impressive! For anyone who's not a pythonista, trying to implement TCO is something akin to solving the Collatz conjecture but for Python. It's often just an exercise in madness. So seeing an elegant solution to this is really cool, I myself was a victim of this madness and was unable to do it so very cool to see someone nail it! This will be a goto tool for sure.

artemonstertoday at 12:32 PM

Can someone give some real world examples for TCO? Every time I see this I only see fibonacci and gcd and I really want to encounter this one in the wild on something real and applicable

show 7 replies