-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add
fib.py
example with `@bend~ decorator
- Loading branch information
1 parent
f9bf829
commit 94450ed
Showing
1 changed file
with
49 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
from typing import TypeVar | ||
T = TypeVar("T") | ||
|
||
|
||
def bend(fn: T) -> T: | ||
""" | ||
Decorator to hind compiler that the recursive function should be translated | ||
to a inline `bend` block. | ||
This will be internal to the library. | ||
""" | ||
return fn | ||
|
||
|
||
def fib_recursive(n: int) -> int: | ||
""" | ||
Calculates fibonacci numbers recursively. | ||
""" | ||
match n: | ||
case 0: | ||
return 0 | ||
case 1: | ||
return 1 | ||
case _: | ||
return fib_recursive(n - 2) + fib_recursive(n - 1) | ||
|
||
|
||
def fib_iterative(n: int) -> int: | ||
""" | ||
Calculates fibonacci numbers iteratively (tail-recursively). | ||
""" | ||
|
||
@bend | ||
def go(a: int, b: int, n: int) -> int: | ||
if n == 0: | ||
return a | ||
else: | ||
return go(b, a + b, n - 1) | ||
|
||
return go(0, 1, n) | ||
|
||
|
||
def main(): | ||
print(fib_iterative(10)) | ||
print(fib_recursive(10)) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |