Skip to main content

Basic tail call removal decorator

Project description

# tcopy

_Do not use this._

A direct tail call optimizing decorator for Python.

## Examples:

```python
from tcopy import tco

@tco
def fib(n, x=0, y=1):
if n == 0:
return x
return fib(n - 1, y, x + y)
```

The `tco` decorator will rewrite `fib` into the following at
definition time:

```python
def fib(n, x=0, y=1):
while 1:
if n == 0:
return x
n, x, y = n - 1, y, x + y
```

## Quirks

`tco` uses `inspect.getsource` to grab a function's source code from
disk. Because of this, the decorator does not work in the Python REPL.

Due to the way `tco` handles code generation and the fact that a
function's `__code__` and `__closure__` (thankfully) cannot be assigned
to, names that a function closes over are injected into a synthetic
dictionary that is used as the generated function's `globals` dict.
Late-bound names are turned into thunks so `tco`'d functions that close
over late-bound names _must_ call those names in order to get to their
values independent of their type. Thunks that wrap functions pass
their arguments to those wrapped functions so there is no need for any
indirection in those cases. For example:

```python
from tcopy import tco


def outer1():
@tco
def fact(n, acc=1):
if n == 0:
return acc
return fact(n - one(), acc * n)

one = 1
return fact


def outer2():
@tco
def fact(n, acc=1):
if n == 0:
return acc
return fact(sub_one(n), acc * n)

sub_one = lambda x: x - 1
return fact
```

Turn into (conceptually):

```python
from tcopy import tco


def outer1():
def fact(n, acc=1):
while 1:
if n == 0:
return acc
n, acc = n - (lambda: one)(), acc * n

one = 1
return fact


def outer2():
def fact(n, acc=1):
while 1:
if n == 0:
return acc
n, acc = (lambda x: sub_one(x))(n), acc * n

sub_one = lambda x: x - 1
return fact
```

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tcopy-0.1.1.tar.gz (3.2 kB view details)

Uploaded Source

Built Distribution

tcopy-0.1.1.macosx-10.10-intel.exe (67.6 kB view details)

Uploaded Source

File details

Details for the file tcopy-0.1.1.tar.gz.

File metadata

  • Download URL: tcopy-0.1.1.tar.gz
  • Upload date:
  • Size: 3.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for tcopy-0.1.1.tar.gz
Algorithm Hash digest
SHA256 8eda5959c614c799a3500e817eaa866c44055ad0945ae26d24ef8b2cef0207fe
MD5 f1564f0fbacb9bc8ebcd172fdbd4c60c
BLAKE2b-256 d91228a83e8cc622862074a87af3cd15f00d1e315a8d065814ac57e3a4230695

See more details on using hashes here.

Provenance

File details

Details for the file tcopy-0.1.1.macosx-10.10-intel.exe.

File metadata

File hashes

Hashes for tcopy-0.1.1.macosx-10.10-intel.exe
Algorithm Hash digest
SHA256 76a3f634e601e61a721a23cd5f88a60d1fe7e9e39effa624454ef9627104ea44
MD5 5df5b7bc563d6dbb61f9dc4d0e90c62a
BLAKE2b-256 59a8d93187840e040954c169ed469557ea98a916eba9c5a7e300b700b29c865c

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page