Repository navigation
Expand file tree
/
Copy pathtest_lambdatools.py
More file actions
197 lines (168 loc) · 6.55 KB
/
Copy pathtest_lambdatools.py
File metadata and controls
197 lines (168 loc) · 6.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# -*- coding: utf-8 -*-
"""Multi-expression lambdas with implicit do; named lambdas."""
from ...syntax import macros, multilambda, namedlambda, quicklambda, f, _, \
envify, local, let, curry, autoreturn
from functools import wraps
# not really redefining "curry", the first one went into MacroPy's macro registry
# (although this does mean the docstring of the macro will not be accessible from here)
from ...fun import withself, curry
from ...tco import trampolined, jump
from ...fploop import looped_over
def test():
with multilambda:
# use brackets around the body of a lambda to denote a multi-expr body
echo = lambda x: [print(x), x]
assert echo("hi there") == "hi there"
count = let((x, 0))[
lambda: [x << x + 1,
x]] # redundant, but demonstrating multi-expr body.
assert count() == 1
assert count() == 2
test = let((x, 0))[
lambda: [x << x + 1, # x belongs to the surrounding let
local[y << 42], # y is local to the implicit do
(x, y)]]
assert test() == (1, 42)
assert test() == (2, 42)
myadd = lambda x, y: [print("myadding", x, y),
local[tmp << x + y],
print("result is", tmp),
tmp]
assert myadd(2, 3) == 5
# only the outermost set of brackets denote a multi-expr body:
t = lambda: [[1, 2]]
assert t() == [1, 2]
with namedlambda:
f1 = lambda x: x**3 # assignment: name as "f1"
assert f1.__name__ == "f1"
gn, hn = let((x, 42), (g, None), (h, None))[[
g << (lambda x: x**2), # env-assignment: name as "g"
h << f1, # still "f1" (RHS is not a literal lambda)
(g.__name__, h.__name__)]]
assert gn == "g"
assert hn == "f1"
foo = let[(f7, lambda x: x) in f7] # let-binding: name as "f7"
assert foo.__name__ == "f7"
# naming a decorated lambda
with namedlambda:
f2 = trampolined(withself(lambda self, n, acc=1: jump(self, n-1, acc*n) if n > 1 else acc))
f2(5000) # no crash since TCO
assert f2.__name__ == "f2"
# works also with custom decorators
def mydeco(f):
@wraps(f) # important! (without this the name is "decorated", not "f")
def decorated(*args, **kwargs):
return f(*args, **kwargs)
return decorated
f3 = mydeco(lambda x: x**2)
assert f3(10) == 100
assert f3.__name__ == "f3"
# parametric decorators are defined as usual
def mypardeco(a, b):
def mydeco(f):
@wraps(f)
def decorated(*args, **kwargs):
return (a, b, f(*args, **kwargs))
return decorated
return mydeco
f4 = mypardeco(2, 3)(lambda x: x**2)
assert f4(10) == (2, 3, 100)
assert f4.__name__ == "f4"
# to help readability of invocations of parametric decorators on lambdas,
# we recognize also curry with a lambda as the last argument
f5 = curry(mypardeco, 2, 3,
lambda x: x**2)
assert f5(10) == (2, 3, 100)
assert f5.__name__ == "f5"
# also autocurry with a lambda as the last argument is recognized
# TODO: fix MacroPy #21 properly; https://github.com/azazel75/macropy/issues/21
with namedlambda:
with curry:
f6 = mypardeco(2, 3, lambda x: x**2)
assert f6(10) == (2, 3, 100)
assert f6.__name__ == "f6"
# presence of autocurry should not confuse the first-pass output
with namedlambda:
with curry:
foo = let[(f7, None) in f7 << (lambda x: x)]
assert foo.__name__ == "f7"
f6 = mypardeco(2, 3, lambda x: x**2)
assert f6(10) == (2, 3, 100)
assert f6.__name__ == "f6"
# looped_over overwrites with the result, so nothing to name
with namedlambda:
result = looped_over(range(10), acc=0)(lambda loop, x, acc: loop(acc + x))
assert result == 45
try:
result.__name__
except AttributeError:
pass
else:
assert False, "should have returned an int (which has no __name__)"
result = curry(looped_over, range(10), 0,
lambda loop, x, acc:
loop(acc + x))
assert result == 45
with quicklambda, multilambda:
func = f[[local[x << _],
local[y << _],
x + y]]
assert func(1, 2) == 3
# formal parameters as an unpythonic env
with envify:
def foo(x):
x = 3 # should become a write into the env
assert x == 3
foo(10)
def foo(x):
x = 3
assert x == 3
del x
try:
x # IDE complains about undefined name (correctly); that's the whole point of this test
except AttributeError: # note it's AttributeError since it's in an env
pass
else:
assert False, "should have deleted x from the implicit env"
foo(10)
# Star-assignment also works, since Python performs the actual unpacking/packing.
# We just use a different target for the store.
with envify:
def foo(n):
a, *n, b = (1, 2, 3, 4, 5)
assert n == [2, 3, 4]
foo(10)
# The main use case is with lambdas, to do things like this:
with envify:
def foo(n):
return lambda i: n << n + i
f = foo(10)
assert f(1) == 11
assert f(1) == 12
# solution to PG's accumulator puzzle with the fewest elements in the original unexpanded code
# http://paulgraham.com/icad.html
with autoreturn, envify:
def foo(n):
lambda i: n << n + i
f = foo(10)
assert f(1) == 11
assert f(1) == 12
# or as a one-liner
with autoreturn, envify:
foo = lambda n: lambda i: n << n + i
f = foo(10)
assert f(1) == 11
assert f(1) == 12
# pythonic solution with optimal bytecode (doesn't need an extra location to store the accumulator)
def foo(n):
def accumulate(i):
nonlocal n
n += i
return n
return accumulate
f = foo(10)
assert f(1) == 11
assert f(1) == 12
print("All tests PASSED")
if __name__ == '__main__':
test()