5 ms·
No. In idiomatic python, you would not use a functional approach for something like this, but if you really wanted to, you could do: def teeing(reducer1,
by lovasoa 5y ago
No. In idiomatic python, you would not use a functional approach for something like this, but if you really wanted to, you could do:
def teeing(reducer1, reducer2):
return lambda acc,elem: (
reducer1(acc[0],elem),
reducer2(acc[1],elem)
)
functools.reduce(
teeing(
(lambda l,elem: [elem] + l),
(lambda l,elem: elem.price + l)
),
cart.getProducts(),
([], 0)
)
- mjburgess 5y agoIt is quaint how there is supposed to be an "idiomatic python" just as mypy, "pattern statements", walrus operators, etc. are being added. "Idomatic python" is dead, and lives now only as a pretty dumb ideology which states: everything must be phrased as a naively-typed naively-imperative program. Python is being made, retroactively, a bad imperative programming language: largely because its creator always thought that's what he'd made (he is wrong). It's a great tragedy "pythonic" has become this: a back-reaction against the times (of increasing adoption of functional programming driven by increasing data-transformation needs).
- eskaytwo 5y agoThat seems a very broad statement. Are there any such articles on this topic? I don’t see how Pythonic as a concept has to be mutually exclusive with new language features. To me, the features you cite seem to fit the Pythonic approach intuitively.
- cma 5y agoAh I didn't read their description well and didn't realize it was doing reductions.