Last active
August 24, 2021 18:24
-
-
Save jsbueno/1cd8465b70072b0047a4996954563dcc to your computer and use it in GitHub Desktop.
Slides using jsbueno/terminedia for quickly explaining the concept of tail recursion optimizations. Used to illustrate a lightning talk on a proof of concept decorator for usng async tasks to have tail-recursion optimization: https://gist.github.com/jsbueno/7644e94bbaa4fbaa8b5e8f4d2bd71425
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
import time | |
import terminedia as TM | |
from terminedia import V2, Color, pause, shape | |
SIZE = 170, 50 | |
def hto8(pos): | |
return (V2(pos) * 0.0625).as_int | |
def hto4(pos): | |
return (V2(pos) * 0.125).as_int | |
def hto2(pos): | |
return (V2(pos) * 0.25).as_int | |
def hto1(pos): | |
return (V2(pos) * 0.5).as_int | |
colorseq = [(0.7, 0.7, 1), (1, 1, .7)] * 10 | |
def sl1(size=15): | |
sl = TM.shape(SIZE) | |
label_col = (20 + 2 * size) | |
left = 10 | |
for i, bottom in enumerate(range(98, 10, -size)): | |
sl.context.color = colorseq[i] | |
sl.high.draw.rect((left, bottom - size, size * 2 + left, bottom), fill=True) | |
sl.text[4].at(hto4((label_col, (bottom - size / 2))) , f"chamada {i}") | |
second_col = 140 | |
# sl.context.char = "1" | |
sl.context.color = "white" | |
sl.text[8].at(hto8((second_col, 0)), "recursão") | |
return sl | |
def sl2(size=15): | |
sl = TM.shape(SIZE) | |
label_col = (20 + 2 * size) | |
bottom = 98 | |
for i, left in enumerate(range(10, 160, size * 2)): | |
sl.context.color = colorseq[i] | |
sl.high.draw.rect((left, bottom - size, size * 2 + left, bottom), fill=True) | |
sl.text[2].at(hto2((left // 2, (bottom - size) - 4)) + (1,0) , f"ch{i}") | |
second_col = 140 | |
# sl.context.char = "1" | |
sl.context.color = "white" | |
sl.text[4].at(hto4((second_col, 0)), "tail recursion") | |
sl.text[4].at(hto4((second_col, 8)), "optimization") | |
return sl | |
def sl3(size=15): | |
sl = TM.shape(SIZE) | |
label_col = (20 + 2 * size) | |
bottom = 98 | |
left = 10 | |
for i, bottom in enumerate(range(98, 10, -size)): | |
sl.context.color = colorseq[i] if i != 1 else (1,1,0) | |
sl.high.draw.rect((left, bottom - size, size * 2 + left, bottom), fill=i < 2) | |
sl.text[4].at(hto4((label_col, (bottom - size / 2))) , | |
f"chamada {i}" if i != 1 else "decorator" ) | |
bottom = 98 | |
for i, left in enumerate(range(20 + 2 * size, 160, size * 2)): | |
sl.context.color = colorseq[i] | |
sl.high.draw.rect((left, bottom - size, size * 2 + left, bottom), fill= True) | |
# sl.text[2].at(hto2((left // 2, (bottom - size) - 4)) + (1,0) , f"task{i}") | |
second_col = 140 | |
# sl.context.char = "1" | |
sl.context.color = "white" | |
sl.text[4].at(hto4((second_col, 0)), "task wrapper") | |
sl.text[4].at(hto4((second_col, 8)), "decorator") | |
return sl | |
def main(): | |
screen = TM.Screen() | |
slides = [sl1, sl2, sl3] | |
with screen: | |
prev_slide = None | |
slide_i = 0 | |
while True: | |
if slide_i != prev_slide: | |
screen.clear() | |
slide = slides[slide_i]() | |
with screen.commands: | |
screen.blit((0,0), slide) | |
prev_slide = slide_i | |
time.sleep(0.5) | |
with TM.keyboard(): | |
time.sleep(1/30) | |
key = TM.inkey() | |
if key == TM.KeyCodes.LEFT: | |
slide_i -= 1 | |
elif key in (TM.KeyCodes.RIGHT, "\r"): | |
slide_i += 1 | |
elif key in ("q", TM.KeyCodes.ESC): | |
break | |
if slide_i < 0: | |
slide_i = len(slides) - 1 | |
elif slide_i >= len(slides): | |
slide_i = 0 | |
time.sleep(1/30) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment