Skip to content

Instantly share code, notes, and snippets.

@snaffi
Created December 11, 2015 21:55
Show Gist options
  • Save snaffi/06b1c2746f5c017638b5 to your computer and use it in GitHub Desktop.
Save snaffi/06b1c2746f5c017638b5 to your computer and use it in GitHub Desktop.
def supermap(funcs, iterable):
for item in iterable:
for func in funcs:
item = func(item)
yield item
def incr(val):
return val + 1
def incr_2(val):
return val + 5
res = supermap([incr, incr_2], [1, 2, 3, 4])
print([i for i in res])
# [7, 8, 9, 10]
@renskiy
Copy link

renskiy commented Dec 12, 2015

Чтобы сохранить семантику оригинального map, я бы предложил следующий вариант:

import itertools


def supermap(fn_list, *iterables):
    for args in zip(*iterables):
        fn_list, fns = itertools.tee(fn_list)
        fn = next(fns)
        result = fn(*args)
        for fn in fns:
            result = fn(result)
        yield result


def summ(a, b):
    return a + b

def incr(i):
    return i + 1

print list(supermap([summ, incr], [3, 1], [7, 5])) # [11, 7]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment