Skip to content

Commit 673c890

Browse files
committed
unify ordering of presentation with lispylet
1 parent a60742a commit 673c890

File tree

1 file changed

+30
-31
lines changed

1 file changed

+30
-31
lines changed

unpythonic/let.py

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -126,16 +126,6 @@ def letrec(body, **bindings):
126126
"""
127127
return _let("letrec", body, **bindings)
128128

129-
def _let(mode, body, **bindings):
130-
assert mode in ("let", "letrec")
131-
env = _env(**bindings)
132-
if mode == "letrec": # supply the environment instance to the letrec bindings.
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)
137-
138-
# decorator factory: almost as fun as macros?
139129
def dlet(**bindings):
140130
"""``let`` decorator.
141131
@@ -168,21 +158,6 @@ def dletrec(**bindings):
168158
"""
169159
return _dlet("letrec", **bindings)
170160

171-
def _dlet(mode, **bindings):
172-
assert mode in ("let", "letrec")
173-
def deco(body):
174-
# evaluate env when the function def runs!
175-
# (so that any mutations to its state are preserved
176-
# between calls to the decorated function)
177-
e = _let(mode, body=None, **bindings)
178-
@wraps(body)
179-
def decorated(*args, **kwargs):
180-
kwargs_with_env = kwargs.copy()
181-
kwargs_with_env["env"] = e
182-
return body(*args, **kwargs_with_env)
183-
return decorated
184-
return deco
185-
186161
def blet(**bindings):
187162
"""``let`` block.
188163
@@ -199,18 +174,42 @@ def result(*, env=None):
199174
def _(*, env=None):
200175
print(env.s)
201176
"""
202-
dlet_deco = dlet(**bindings)
203-
def deco(body):
204-
return immediate(dlet_deco(body))
205-
return deco
177+
return _blet("let", **bindings)
206178

207179
def bletrec(**bindings):
208180
"""``letrec`` block.
209181
210182
This chains ``@dletrec`` and ``@immediate``."""
211-
dletrec_deco = dletrec(**bindings)
183+
return _blet("letrec", **bindings)
184+
185+
def _let(mode, body, **bindings):
186+
assert mode in ("let", "letrec")
187+
env = _env(**bindings)
188+
if mode == "letrec": # supply the environment instance to the letrec bindings.
189+
for k in env:
190+
env[k] = env[k](env)
191+
# decorators need just the final env; else run body now
192+
return env if body is None else body(env)
193+
194+
# decorator factory: almost as fun as macros?
195+
def _dlet(mode, **bindings):
196+
assert mode in ("let", "letrec")
212197
def deco(body):
213-
return immediate(dletrec_deco(body))
198+
# evaluate env only once, when the function def runs
199+
# (to preserve state between calls to the decorated function)
200+
env = _let(mode, body=None, **bindings)
201+
@wraps(body)
202+
def decorated(*args, **kwargs):
203+
kwargs_with_env = kwargs.copy()
204+
kwargs_with_env["env"] = env
205+
return body(*args, **kwargs_with_env)
206+
return decorated
207+
return deco
208+
209+
def _blet(mode, **bindings):
210+
dlet_deco = _dlet(mode, **bindings)
211+
def deco(body):
212+
return immediate(dlet_deco(body))
214213
return deco
215214

216215
def test():

0 commit comments

Comments
 (0)