Skip to content

Commit

Permalink
improve fib.py example
Browse files Browse the repository at this point in the history
  • Loading branch information
steinerkelvin committed Jun 12, 2024
1 parent 94450ed commit 924c2a1
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions examples/fib.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
from typing import TypeVar
# from benda import bjit

T = TypeVar("T")


def bend(fn: T) -> T:
"""
Decorator to hind compiler that the recursive function should be translated
to a inline `bend` block.
Decorator that hints the transpiler that a call to the recursive function
should be translated to an inline `bend` block.
This will be internal to the library.
This decorator is intended to be defined internally by the benda library.
"""
return fn

# @bjit


def fib_recursive(n: int) -> int:
"""
Calculates fibonacci numbers recursively.
Calculates the nth Fibonacci number using a recursive approach.
"""
match n:
case 0:
Expand All @@ -24,12 +28,16 @@ def fib_recursive(n: int) -> int:
case _:
return fib_recursive(n - 2) + fib_recursive(n - 1)

# @bjit


def fib_iterative(n: int) -> int:
"""
Calculates fibonacci numbers iteratively (tail-recursively).
Calculates the nth Fibonacci number using an iterative (tail-recursive) approach.
"""

# This decorator does nothing, it just hints the benda transpiler to
# use a `bend` block for the recursive computation.
@bend
def go(a: int, b: int, n: int) -> int:
if n == 0:
Expand Down

0 comments on commit 924c2a1

Please sign in to comment.