-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
60 lines (34 loc) · 1.03 KB
/
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
from pipe_fn import e
# e is the identity mapping
def add(this, other):
return this + other
print([1, 2, 3] | e / sum)
# sum([1, 2, 3])
# => 6
print([2, 3, -1] | e ** {'key': lambda x: -x} / sorted) # set kwargs
# sorted([2, 3, -1], key=lambda x: -x)
# => [3, 2, -1]
print([[1], [2], [3]] | e / sum * ([],)) # set args
# sum([[1], [2], [3]], [])
# => [1, 2, 3]
def my_func(self, *args, **kwargs):
return self, args, kwargs
print([1, 2, 3]
| e
** dict(a=1, b=2, c=3) # you should set kwargs first because of the high priority of `**` operator.
* (4, 5, 6) # it'okay to change the order of setting `args` and setting `function`.
/ my_func)
# => ([1, 2, 3], (4, 5, 6), {'a': 1, 'b': 2, 'c': 3})
print(1 | e / add * (1,))
# add(1, 1)
# => 2
def double(x):
return 2 * x
# and then composition
print(1 | (e / add * (2,) + double + double))
# double(double(add(1, 2))
# => 12 = (1 + 2) * 2 * 2
# set single arg
[['a'], ['b']] | e / sum @ [] | e / print
# print(sum([['a'], ['b']], []))
# => [1, 2]