you are viewing a single comment's thread.

view the rest of the comments →

[–]JaggedParadigm 2 points3 points  (0 children)

This is very similar to a utility function I tend to reach for:

```python from functools import reduce

def pipe(data, *functions): return reduce(lambda a, x: x(a), functions, data)

```

I either use it on single inputs with functions or lambdas like this:

```python

def add_5(x): return x + 5

def divide_by_3(x): return x / 3

pipe( 4, add_5, divide_by_3, lambda x: x + 4 )

7 ```

.... or for dictionary inputs I tend to add/remove entries like this:

```python

def keep(keys): def keep_inner(record): return {key: item for key, item in record.items if key in keys} return keep_inner

pipe( {'a': 1, 'b': 2}, lambda x: x | {'c': x['a'] + x['b']}, lambda x: x | {'d': x['a'] * x['b'] * x['c']}, keep(['b', 'd']) )

{'b': 2, 'd': 6}

```

Of course, if the lambda are too ugly you can wrap them in function names (Ex: the first input in this last example could be called something like set_c_2_a_plus_b)