zhuangzhuang1988
2016-09-15 20:03:25 +08:00
```python
def read_file_gen(name):
with open(name, 'r') as fp:
for line in fp :
yield line
def map_gen(from, fn):
for item in from:
yield fn(item)
def merge_gen(from1, from2, choice_fn):
gen1 = from1()
gen2 = from2()
while True:
gen1:
try:
item1 = gen1.next()
except StopIteration as e:
yield from from2
break
gen2:
try:
item2 = gen2.next():
except StopIteration as e:
yield from from1
break
item = fn(item1, item2)
if item == item1:
yield item
goto gen1
# Todo 实现不完整
def write_file_gen_stop(from, fname):
with open(fname, 'w') as fp:
for line in
def map_fn(line):
score = int(line.split(' '))
return (score, line)
def map_fn2(item):
return item[1]
def choice_fn(item1, item2):
if item1[0] > item2[0]:
return item2
else:
return item1
def _f(n):
g_f = read_file(n)
return map_gen(g_f, map_fn)
g_merge = merge_gen(_f('a'), _f('b'), choice_fn)
g_out = map_gen(g_merge, map_fn2)
write_file_gen_stop(g_out)
```
代码尚未完整, 不够大概是这个意思。。, (如果 a , b 是分别有序的话)