Created
February 14, 2019 14:54
-
-
Save ankitml/6551cb8d4317fb51f7f9ebea4a45b822 to your computer and use it in GitHub Desktop.
infinite recursion with generators experiments in python
This file contains hidden or 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
from random import random | |
def generate_infinite_random_nos(): | |
yield random() | |
yield from generate_infinite_random_nos() | |
#https://eddmann.com/posts/merge-sort-in-scala-using-tail-recursion-and-streams/ | |
# https://medium.com/@dawran6/lazy-codes-infinite-sequences-in-python-and-clojure-80bba720b3a3 | |
def tri_num_infinite_series(n=1, tri_num=0): | |
yield tri_num | |
tri_num += n | |
n += 1 | |
yield from tri_num_infinite_series(n, tri_num) | |
def infinite_factorial_generator(n=1, fac=1): | |
fac = fac*n | |
yield fac | |
yield from infinite_factorial_generator(n+1,fac) | |
def generate_piby4_infinite_sequence(n=1, sign=1, val=1): | |
yield sign*val | |
yield from generate_piby4_infinite_sequence(n+2, -1*sign, 1/(n+2)) | |
def calculate_pi(n): | |
gen = generate_piby4_infinite_sequence() | |
return 4 * sum(next(gen) for i in range(n)) | |
#nested_list = [[1,2,3], [[11]], 5, [1,19]] | |
#nested_list = [[1,2,3], [[[[11]]]], 5, [1,19]] | |
# caution string elements can be iterated upon, infinitly | |
#nested_list = [[1,2,3], [[[[11]]]], 5, [1,19], 'ankit'] | |
def flatten(nested_list): | |
try: | |
for sublist in nested_list: | |
yield from flatten(sublist) | |
except TypeError: | |
yield nested_list | |
#http://code.activestate.com/recipes/221457-self-recursive-generators/ | |
# mapping /filtering over a tree like structures | |
randomPasswordGenerator = (lambda characters: # level 2 | |
lambda length: ''.join(random.choice(characters) for i in range(length)) # level 3 | |
)(string.digits + string.ascii_letters) # level 2 args | |
# http://simeonvisser.com/posts/python-3-using-yield-from-in-generators-part-2.html | |
# trampoline functions - https://www.python.org/dev/peps/pep-0342/ | |
# this has been called a poor mans tail recursion | |
def zrange(n): | |
print(n) | |
if n>1: | |
return lambda: zrange(n-1) | |
#http://lambda-the-ultimate.org/node/2374 | |
# By trampolining you can unwind the part of the C stack before going further | |
""" | |
In [30]: def tramp(func, *args): | |
...: t = func(*args) | |
...: while callable(t): | |
...: t = t() | |
The main advantage of recursive definitions is that you can easily prove properties about them using mathematical induction -- even using automated systems like ACL2. | |
""" | |
# some background on tail call optimization | |
#https://stackoverflow.com/questions/25297446/is-it-advisable-to-write-recursive-functions-in-python | |
# https://dev.to/pichardoj/do-you-even-recurse-and-if-you-do-do-you-do-it-safely-4mef | |
#there are many caveats to the use of such a decorator, since it has to assume that any recursive call (in the decorated function) is tail-recursive and can be eliminated.jjjj | |
#https://en.wikipedia.org/wiki/Continuation-passing_style | |
#https://dev.to/pichardoj/do-you-even-recurse-and-if-you-do-do-you-do-it-safely-4mef | |
# https://eli.thegreenplace.net/2017/on-recursion-continuations-and-trampolines/ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment