4 ms·
I think lambda syntax can be a bit cumbersome, but that aside what I really miss is a clean syntax for chaining functional operations. So often I find myself th
by jpulec 10y ago
I think lambda syntax can be a bit cumbersome, but that aside what I really miss is a clean syntax for chaining functional operations. So often I find myself thinking about data in terms of 'pipelines'. i.e. in JS:
_.chain(values)
.map(() => {})
.flatten()
.compact()
.uniq()
.value()
vs Python where doing the same thing becomes either a nested mess of function calls or comprehensions or a for loop.
- SoftwareMaven 10y agoBut that's a function of API, not the language itself. Django (and most ORMs, I believe) support that kind of behavior: MyTable.objects. filter(some_row__gt=5). exlude(other_row='q'). order_by('other_row') The Python iterable APIs have decided to use nesting rather than chaining, but you can still have an underscore-like API: https://github.com/serkanyersen/underscore.py https://github.com/serkanyersen/underscore.py The bigger problem remains: lambda functions are hideous in Python. map() will forever be ugly if you try to use it in the same way it is used in most functional languages.
- chris_7 10y agoThis sort of API is hard to implement in Python though, because there's no formal notion of interfaces, so you cannot extend all iterables generically. So you need to use free functions (which don't read well when chained) or a wrapper object (ick).
- hcrisp 10y agoYou don't like using pytoolz? Pseudocode: result = pytoolz.pipe(values, map, flatten, compact, uniq, value) # or func = pytoolz.compose(value, uniq, compact, flatten, map) results = func(values)
- Bromskloss 10y agoLooks interesting. How do you tell map what function to use?
- hcarvalhoalves 10y agoYou probably need `compose(foo, partial(map, fn), bar)`.
- hcrisp 10y agoYeah, you do this or use curried version: pytoolz.curried.map(fn)
- Bromskloss 10y agoI've been thinking that it might be nice to use chaining (though I didn't know it had a name) in ordinary mathematical notation too, writing "x f g" instead of "g(f(x))".
- moosingin3space 10y agoElixir and F# have my favorite syntax for that: values |> map(&({})) |> flatten |> compact |> uniq Although, the closure syntax is a little clunky before you get used to it.