Skip to main content

Functional Recursion

Do tail recursion without overflowing with generators and functions.

Why?

I am coming from a background of functional programming languages where loops are not standard. Instead, recursion is used to achieve the same result more succinctly.

Overview

It provides:

  • Infinite recursion with return
  • Infinite recursion with generators (yield)

Using this has the following advantages:

  • Allows for more understandable code given the right situation
  • Avoid bugs because of deeply nested loops

Instructions

Run in the console

python3 -m pip install functional_recursion

(Use python or python3 depending on your environment)

And to use it

from functional_recursion import recur, recur_yield, tail_recursive, tail_recursive_yield

@tail_recursive
def fib_decorator_recursive(n, last_two=None):
    if last_two is None:
        last_two = (0, 1)
    if n == 0:
        return last_two[1]
    last_two = (last_two[1], sum(last_two))
    return recur(n - 1, last_two)


@tail_recursive_yield
def fib_decorator_recursive_generator(last_two=None):
    if last_two is None:
        last_two = (0, 1)
    last_two = (last_two[1], sum(last_two))
    return recur_yield(last_two, yield_val=last_two[0])

* See the complete examples and performance times here.

Authors

Release files for functional-recursion 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for functional-recursion 1.0.0
File Size Uploaded
functional_recursion-1.0.0.tar.gz 3.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for functional-recursion 1.0.0
File Interpreter ABI Platform
functional_recursion-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 6.7 kB

Release files / functional_recursion-1.0.0.tar.gz

Download URL functional_recursion-1.0.0.tar.gz
Size 3.1 kB
Tags Source
SHA-256 checksum
How to use checksums
886610fdaa7bed2adafba45d498eca0b0d428e756c8fc2795eaba76296f09dac
BLAKE2b-256 checksum
How to use checksums
a4d54575f560be544f10cacd4ae9ae3d499fb32f3060dca0a494a705d5c27696
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.5.0 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.12

Release files / functional_recursion-1.0.0-py3-none-any.whl

Download URL functional_recursion-1.0.0-py3-none-any.whl
Size 3.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
cc087cd2258a7d896b725322d0c77f556d4cfefe722a0c8bbdae10f8082e256d
BLAKE2b-256 checksum
How to use checksums
1851f908fe0d6ac145e5a6762bcdb93d711d3155670793b4891cde7293696233
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.5.0 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.12

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page