-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhighord.py
47 lines (38 loc) · 1.11 KB
/
highord.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
def equals(a):
def func(x):
return __resolve(x, a)[0] == x
return func
def not_(a):
def func(x):
return not __resolve(x, a)[0]
return func
# get item from tuple or list
# usage: get_item(index)(action)
# where action is callable to handle the item or raise an error
# eg: get_item(0)(get_item(0)(///))
def get_item(index):
def actor(action):
def func(obj):
# TODO: assert
return action(obj[__resolve(obj, index)[0]]) # obj的类型是int
return func
return actor
# TODO: support for dict
def get_attr(item):
def actor(action):
def func(obj):
if type(obj) is not dict:
return action(getattr(obj, __resolve(obj, item)[0]))
else:
pass
pass
return func
return actor
# if arg form args is callable, call arg with x as param
# then replace it by the result
def __resolve(x, *args):
args = list(args)
for i in range(len(args)):
if callable(args[i]):
args[i] = args[i](x)
return args