-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathLecture_10_simple.py
67 lines (46 loc) · 1.38 KB
/
Lecture_10_simple.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
# 10. Data flow design
# --------------------
class Sink:
def __init__(self, initial):
self._initial = initial
def end(self):
return self._initial
def __call__(self, some_arg):
print('Flow finished')
return (None, None)
class Pipe:
def __init__(self, initial):
self._initial = initial
def for_each(self, method):
self._method = method
self._next = Sink(self._initial)
return self._next
def __call__(self, enumerable):
if (enumerable == None):
raise Exception("Invalid enumerable")
if (self._method == None):
raise Exception("Invalid method")
for x in enumerable:
self._method(x)
return (self._next, None)
class ListSource:
def __init__(self):
pass
def take(self, n):
self._take_n = n
self._next = Pipe(self)
return self._next
def __call__(self, list_arg):
print('Flow started.')
if (list_arg == None):
raise Exception("Invalid data")
if (self._take_n != None):
return (self._next, list_arg[:self._take_n])
return (None, None)
def runFlow(flow, init_arg):
next = flow
arg = init_arg
while next != None:
next, arg = next(arg)
flow1 = ListSource().take(3).for_each(lambda x: print(x)).end()
runFlow(flow1, [1,2,3,4,5])