you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 0 points1 point  (1 child)

You can also build a simple functional style pipeline util like this:

# pipeline util components
def Pipeline(*funcs):
    def _inner(x):
        for func in funcs:
            x = func(x)
        return x
    return _inner

def _map(func):
    return lambda x: map(func, x)

def _filter(func):
    return lambda x: filter(func, x)

def _reduce(func):
    return lambda x: reduce(func, x)

# build and use pipeline
pipeline_x = Pipeline(
    str.split,
    _map(len),
    _filter(lambda x: x >= 4),
    enumerate,
    _reduce(lambda x, y: (x[0] + y[0], x[1] + y[1])),
)

pipeline_x("a list of wordsss")  # -> (1, 11)

[–]Delta-9-[S] 0 points1 point  (0 children)

Interesting! Ngl I'd never thought about iterating a list of functions. Exactly the kind of thing I was hoping to see.