Repository navigation
Expand file tree
/
Copy pathtest_fun.py
More file actions
217 lines (188 loc) · 6.21 KB
/
Copy pathtest_fun.py
File metadata and controls
217 lines (188 loc) · 6.21 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# -*- coding: utf-8 -*-
from collections import Counter
from ..fun import memoize, curry, apply, \
identity, const, \
andf, orf, notf, \
flip, rotate, \
composel1, composer1, composel, composer, \
to1st, to2nd, tolast, to, \
withself
from ..dynassign import dyn
def test():
evaluations = Counter()
@memoize
def f(x):
evaluations[x] += 1
return x**2
f(3)
f(3)
f(4)
f(3)
assert all(n == 1 for n in evaluations.values())
# "memoize lambda": classic evaluate-at-most-once thunk
thunk = memoize(lambda: print("hi from thunk"))
thunk()
thunk()
evaluations = 0
@memoize
def t():
nonlocal evaluations
evaluations += 1
t()
t()
assert evaluations == 1
# memoizing an instance method
#
# This works essentially because self is an argument, and custom classes
# have a default __hash__. Hence it doesn't matter that the memo lives in
# the "memoized" closure on the class object (type), where the method is,
# and not directly on the instances.
#
# For a solution storing the memo on the instances, see:
# https://github.com/ActiveState/code/tree/master/recipes/Python/577452_memoize_decorator_instance
class Foo:
def __init__(self):
self.evaluations = Counter()
self.x = 42
self.lst = [] # mutable attribute, just to be sure this works generally
@memoize
def doit(self, y):
self.evaluations[(id(self), y)] += 1
return self.x*y
foo1 = Foo()
foo1.doit(1)
foo1.doit(1)
foo1.doit(2)
foo1.doit(3)
foo1.doit(2)
assert all(n == 1 for n in foo1.evaluations.values())
foo2 = Foo()
assert not foo2.evaluations
foo2.doit(1)
foo2.doit(1)
foo2.doit(2)
foo2.doit(3)
foo2.doit(2)
assert all(n == 1 for n in foo2.evaluations.values())
# exception storage in memoize
class AllOkJustTesting(Exception):
pass
evaluations = 0
@memoize
def t():
nonlocal evaluations
evaluations += 1
raise AllOkJustTesting()
olderr = None
for _ in range(3):
try:
t()
except AllOkJustTesting as err:
if olderr is not None and err is not olderr:
assert False # exception instance memoized, should be same every time
olderr = err
else:
assert False # memoize should not block raise
assert evaluations == 1
@curry
def add3(a, b, c):
return a + b + c
assert add3(1)(2)(3) == 6
# actually uses partial application so these work, too
assert add3(1, 2)(3) == 6
assert add3(1)(2, 3) == 6
assert add3(1, 2, 3) == 6
@curry
def lispyadd(*args):
return sum(args)
assert lispyadd() == 0 # no args is a valid arity here
@curry
def foo(a, b, *, c, d):
return a, b, c, d
assert foo(5, c=23)(17, d=42) == (5, 17, 23, 42)
# currying a thunk is essentially a no-op
evaluations = 0
@curry
def t():
nonlocal evaluations
evaluations += 1
t()
assert evaluations == 1 # t has no args, so it should have been invoked
add = lambda x, y: x + y
a = curry(add)
assert curry(a) is a # curry wrappers should not stack
# top-level curry context handling
def double(x):
return 2*x
with dyn.let(curry_context=["whatever"]):
curry(double, 2, "foo") == (4, "foo")
try:
curry(double, 2, "foo")
except TypeError:
pass
else:
assert False, "should fail by default when top-level curry context exits with args remaining"
# flip
def f(a, b):
return (a, b)
assert f(1, 2) == (1, 2)
assert (flip(f))(1, 2) == (2, 1)
assert (flip(f))(1, b=2) == (1, 2) # b -> kwargs
double = lambda x: 2*x
inc = lambda x: x+1
inc_then_double = composer1(double, inc)
double_then_inc = composel1(double, inc)
assert inc_then_double(3) == 8
assert double_then_inc(3) == 7
inc2_then_double = composer1(double, inc, inc)
double_then_inc2 = composel1(double, inc, inc)
assert inc2_then_double(3) == 10
assert double_then_inc2(3) == 8
inc_then_double = composer(double, inc)
double_then_inc = composel(double, inc)
assert inc_then_double(3) == 8
assert double_then_inc(3) == 7
inc2_then_double = composer(double, inc, inc)
double_then_inc2 = composel(double, inc, inc)
assert inc2_then_double(3) == 10
assert double_then_inc2(3) == 8
assert to1st(double)(1, 2, 3) == (2, 2, 3)
assert to2nd(double)(1, 2, 3) == (1, 4, 3)
assert tolast(double)(1, 2, 3) == (1, 2, 6)
processor = to((0, double),
(-1, inc),
(1, composer(double, double)),
(0, inc))
assert processor(1, 2, 3) == (3, 8, 4)
assert identity(1, 2, 3) == (1, 2, 3)
assert identity(42) == 42
assert identity() is None
assert (rotate(-1)(identity))(1, 2, 3) == (3, 1, 2)
assert (rotate(1)(identity))(1, 2, 3) == (2, 3, 1)
# inner to outer: (a, b, c) -> (b, c, a) -> (a, c, b)
assert flip(rotate(-1)(identity))(1, 2, 3) == (1, 3, 2)
def hello(*args):
return args
assert apply(hello, (1, 2, 3)) == (1, 2, 3)
assert apply(hello, 1, (2, 3, 4)) == (1, 2, 3, 4)
assert apply(hello, 1, 2, (3, 4, 5)) == (1, 2, 3, 4, 5)
assert apply(hello, 1, 2, [3, 4, 5]) == (1, 2, 3, 4, 5)
assert const(1, 2, 3)(42, "foo") == (1, 2, 3)
assert const(42)("anything") == 42
assert const()("anything") is None
assert notf(lambda x: 2*x)(3) is False
assert notf(lambda x: 2*x)(0) is True
isint = lambda x: isinstance(x, int)
iseven = lambda x: x % 2 == 0
isstr = lambda s: isinstance(s, str)
assert andf(isint, iseven)(42) is True
assert andf(isint, iseven)(43) is False
pred = orf(isstr, andf(isint, iseven))
assert pred(42) is True
assert pred("foo") is True
assert pred(None) is False # neither condition holds
fact = withself(lambda self, n: n * self(n - 1) if n > 1 else 1)
assert fact(5) == 120
print("All tests PASSED")
if __name__ == '__main__':
test()