Skip to content

Commit 7bdea48

Browse files
committed
small refactor, reduce superfluous differences from lispylet
1 parent 2011faa commit 7bdea48

File tree

1 file changed

+8
-10
lines changed

1 file changed

+8
-10
lines changed

unpythonic/let.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from functools import wraps
88

99
from unpythonic.misc import immediate
10-
from unpythonic._let_support import env
10+
from unpythonic._let_support import env as _env
1111

1212
def let(body, **bindings):
1313
"""``let`` expression.
@@ -128,11 +128,12 @@ def letrec(body, **bindings):
128128

129129
def _let(mode, body, **bindings):
130130
assert mode in ("let", "letrec")
131-
e = env(**bindings)
131+
env = _env(**bindings)
132132
if mode == "letrec": # supply the environment instance to the letrec bindings.
133-
for k in e:
134-
e[k] = e[k](e)
135-
return body(e)
133+
for k in env:
134+
env[k] = env[k](env)
135+
# decorators need just the final env; else run body now
136+
return env if body is None else body(env)
136137

137138
# decorator factory: almost as fun as macros?
138139
def dlet(**bindings):
@@ -173,10 +174,7 @@ def deco(body):
173174
# evaluate env when the function def runs!
174175
# (so that any mutations to its state are preserved
175176
# between calls to the decorated function)
176-
e = env(**bindings)
177-
if mode == "letrec": # supply the environment instance to the letrec bindings.
178-
for k in e:
179-
e[k] = e[k](e)
177+
e = _let(mode, body=None, **bindings)
180178
@wraps(body)
181179
def decorated(*args, **kwargs):
182180
kwargs_with_env = kwargs.copy()
@@ -232,7 +230,7 @@ def see(x):
232230

233231
# context manager only
234232
def f3(lst):
235-
with env(seen = set()) as myenv:
233+
with _env(seen = set()) as myenv:
236234
return [myenv.seen.add(x) or x for x in lst if x not in myenv.seen]
237235
# myenv still lives due to Python's scoping rules.
238236
# This is why we provide a separate let construct

0 commit comments

Comments
 (0)