thiagola92 / learning-interpreters

0 stars 0 forks source link

Add defer #1

Open thiagola92 opened 8 months ago

thiagola92 commented 8 months ago

Execute after function block ends

Python:

from pathlib import Path

suffixes = [".h", ".hpp"]

def get_headers_dir(path: str) -> list[str]:
    subpaths: list[str] = []

    for p in Path(path).iterdir():
        if p.is_file() and p.suffix in suffixes:
            return [path]
        elif p.is_dir():
            subpaths.append(str(p))

    paths: list[str] = []

    for p in subpaths:
        paths.extend(get_headers_dir(p))

    return paths

for d in get_headers_dir('.'):
    print(d)

Idea:

from pathlib import Path

suffixes = [".h", ".hpp"]

def get_headers_dir(path: str) -> list[str]:
    subpaths: list[str] = []

    for p in Path(path).iterdir():
        if p.is_file() and p.suffix in suffixes:
            return [path]
        elif p.is_dir():
            defer:
                subpaths.extend(get_headers_dir(p))

    return subpaths

for d in get_headers_dir('.'):
    print(d)

Maybe make it more versatile

defer def: defer after function block
defer for: defer after for block
defer if: defer after if block
defer 3: defer after 3 blocks