-
Notifications
You must be signed in to change notification settings - Fork 3
/
quickstart_test.py
182 lines (146 loc) · 5.61 KB
/
quickstart_test.py
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
from liquidata import *
from testhelpers import *
a,b,c,f,g,h = symbolic_functions('abcfgh')
abc012 = namespace_source()
def test_quickstart():
# ANCHOR: guinea_pigs
assert f (3) == 'f(3)'
assert f(g(4)) == 'f(g(4))'
assert sym_add( 2 , 3) == '(2 + 3)'
assert sym_mul(f(2), g(3)) == '(f(2) * g(3))'
# ANCHOR_END: guinea_pigs
# ANCHOR: function_composition
doit = pipe(f, g, h)
assert doit(range(3)) == ['h(g(f(0)))', 'h(g(f(1)))', 'h(g(f(2)))']
# ANCHOR_END: function_composition
# ANCHOR: equivalence_to_multiple_maps
piped = pipe( f, g, h)(range(10))
mapped = list(map(h, map(g, map(f, range(10)))))
assert piped == mapped
# ANCHOR_END: equivalence_to_multiple_maps
# ANCHOR: filter
assert pipe( odd )(range(4)) == [False, True, False, True]
assert pipe({odd})(range(4)) == [ 1 , 3 ]
# ANCHOR_END: filter
# ANCHOR: join
assert pipe(join)(['abc', '', 'd', 'efg']) == list('abcdefg')
assert pipe(lambda n: n*str(n), join)([3,1,0,2]) == list('333122')
assert pipe(range, join)(range(4)) == [0, 0,1, 0,1,2]
assert pipe(flat(range))(range(4)) == [0, 0,1, 0,1,2]
# ANCHOR_END: join
# ANCHOR: fold
assert pipe(out(sym_add))('xyzw') == '(((x + y) + z) + w)'
assert pipe(out(sym_add))('xyzw') == reduce(sym_add, 'xyzw')
# ANCHOR_END: fold
# ANCHOR: fold_with_initial
assert pipe(out(sym_add, 'A'))('xyz') == '(((A + x) + y) + z)'
assert pipe(out(sym_add, 'A'))('xyz') == reduce(sym_add, 'xyz', 'A')
# ANCHOR_END: fold_with_initial
# ANCHOR: side_effects
result = []
pipe(f, sink(result.append))(range(3))
assert result == list(map(f, range(3)))
# ANCHOR_END: side_effects
# ANCHOR: branch
side = []
main = pipe(f,
[g, sink(side.append)], # This is a branch
h)(range(3))
assert side == ['g(f(0))', 'g(f(1))', 'g(f(2))']
assert main == ['h(f(0))', 'h(f(1))', 'h(f(2))']
# ANCHOR_END: branch
# ANCHOR: named_outs
result = pipe(f,
[g, out.side], # This is a branch
h,
out.main)(range(3))
assert result.side == ['g(f(0))', 'g(f(1))', 'g(f(2))']
assert result.main == ['h(f(0))', 'h(f(1))', 'h(f(2))']
# ANCHOR_END: named_outs
# ANCHOR: spy
spy1, spy2 = [], []
pipe(f, [sink(spy1.append)], g)
#pipe(f, spy(spy2.append) , g)
pipe(f, [out.X], g)
#pipe(f, spy.X , g)
# ANCHOR_END: spy
# ANCHOR: name_single
# Make a namespace containing one name out of a single value
one = pipe(name.x)(range(3))
two = list(Namespace(x=n) for n in range(3))
assert one == two
# Make a namespace containing two names, out of a 2-tuple
data = [(1,2), (3,4), (9,8)]
one = pipe(name.a.b)(data)
two = list(Namespace(a=a, b=b) for (a,b) in data)
assert one == two
# ANCHOR_END: name_single
# ANCHOR: get_as_attrgetter
ns = name.a.b.c('xyz')
assert get.b (ns) == attrgetter('b') (ns)
assert get.a.c(ns) == attrgetter('a', 'c')(ns)
expected = [('a0', 'c0'), ('a1', 'c1'), ('a2', 'c2')]
assert pipe( get .a .c) (abc012) == expected
assert pipe(attrgetter('a', 'c'))(abc012) == expected
# ANCHOR_END: get_as_attrgetter
# ANCHOR: abc012
assert abc012 == [Namespace(a='a0', b='b0', c='c0'),
Namespace(a='a1', b='b1', c='c1'),
Namespace(a='a2', b='b2', c='c2')]
# ANCHOR_END: abc012
# ANCHOR: get_as_star
assert sym_add(2,3) == '(2 + 3)'
assert pipe(get.a.b * sym_add)(abc012) == ['(a0 + b0)', '(a1 + b1)', '(a2 + b2)']
# ANCHOR_END: get_as_star
# ANCHOR: star
data = [(1,2), (3,4), (9,8)]
assert pipe(star(sym_add))(data) == ['(1 + 2)', '(3 + 4)', '(9 + 8)']
# ANCHOR_END: star
# ANCHOR: put
data = pipe(name.a.b)([(1,2), (3,4), (9,8)])
result = pipe(get.a.b * mul >> put.ab)(data)
assert pipe(get.a )(result) == [1 , 3 , 9 ]
assert pipe(get.b )(result) == [ 2, 4, 8]
assert pipe(get.ab)(result) == [1*2, 3*4, 9*8]
# ANCHOR_END: put
# ANCHOR: multiple_put
data = pipe(name.a.b)([(1,2), (3,4), (9,8)])
def hilo(a,b):
return max(a,b), min(a,b)
result = pipe(get.a.b * hilo >> put.hi.lo)(data)
assert pipe(get.a )(result) == [1 , 3 , 9 ]
assert pipe(get.b )(result) == [ 2, 4, 8]
assert pipe(get.hi)(result) == [ 2, 4, 9 ]
assert pipe(get.lo)(result) == [1 , 3 , 8]
# ANCHOR_END: multiple_put
# ANCHOR: get_put_after
inner = pipe( f, g )
outer = pipe(a, inner, b)
assert outer(range(3)) == pipe(a, f, g, b)(range(3))
# ANCHOR_END: get_put_after
# ANCHOR: nested_pipes
inner = pipe( f, g )
outer = pipe(a, inner, b)
assert outer(range(3)) == pipe(a, f, g, b)(range(3))
# ANCHOR_END: nested_pipes
# ANCHOR: as_function
fn = pipe(f, g).fn()
assert fn(1) == 'g(f(1))'
assert fn(2) == 'g(f(2))'
# ANCHOR_END: as_function
# ANCHOR: as_function_multiple
fn = pipe({odd}, f).fn()
assert fn(3) == 'f(3)'
assert fn(4) == Void
fn = pipe(range, join, f).fn()
assert fn(0) == Void
assert fn(1) == 'f(0)'
assert fn(3) == Many(('f(0)', 'f(1)', 'f(2)'))
# ANCHOR_END: as_function_multiple
# ANCHOR: many_and_void
assert Many((1,2,3)) == (1,2,3) == tuple((1,2,3))
assert Void == Many( ) == () == tuple( )
assert isinstance(Many(), tuple)
assert isinstance(Void , tuple)
assert isinstance(Void , Many)
# ANCHOR_END: many_and_void