Last active
August 12, 2024 09:02
-
-
Save Summertime/b239eefcaafaf0f3d4c2f0aa2cbb23cb to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env python | |
from functools import wraps | |
class Interpolation: | |
def __init__(self, getvalue): | |
self.getvalue = getvalue | |
def eager_tag(func): | |
@wraps(func) | |
def wrapper(*args): | |
newargs = [] | |
for arg in args: | |
match arg: | |
case Interpolation() as ip: | |
# Avoiding binding issues | |
newargs.append( | |
Interpolation( (lambda v: lambda: v)(ip.getvalue()) ) | |
) | |
case other: | |
newargs.append(other) | |
return func(*newargs) | |
return wrapper | |
@eager_tag | |
def lazy(*args): | |
def _lazy(): | |
for arg in args: | |
match arg: | |
case Interpolation() as ip: | |
print(ip.getvalue()) | |
case other: | |
print(other) | |
return _lazy | |
a,b = 1,2 | |
l = lazy( | |
'a', Interpolation(lambda: a), | |
'b', Interpolation(lambda: b), | |
) | |
a,b = 3,4 | |
l() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment