2022-05-13 16:38:20 +02:00

44 lines
790 B
Python

from itertools import starmap, tee
from funcy import curry, compose
def lift(fn):
return curry(map)(fn)
def llift(fn):
return compose(list, lift(fn))
def starlift(fn):
return curry(starmap)(fn)
def lstarlift(fn):
return compose(list, starlift(fn))
def parallel(*fs):
return lambda *args: (f(a) for f, a in zip(fs, args))
def star(f):
return lambda x: f(*x)
def duplicate_stream_and_apply(f1, f2):
return compose(star(parallel(f1, f2)), tee)
def foreach(fn, iterable):
for itm in iterable:
fn(itm)
def parallel_map(f1, f2):
"""Applies functions to a stream in parallel and yields a stream of tuples:
parallel_map :: a -> b, a -> c -> [a] -> [(b, c)]
"""
return compose(star(zip), duplicate_stream_and_apply(f1, f2))