4 ms·
I would more like to support some kind of pipes, eg.: 123 | add10 | str | print UPD: Some of my workarounds (dont try this at home lol): [twitter thread] http
by tandav 6y ago
I would more like to support some kind of pipes, eg.:
123 | add10 | str | print
UPD:
Some of my workarounds (dont try this at home lol):
[twitter thread]
https://twitter.com/tandavaya/status/1155925017848242176 https://twitter.com/tandavaya/status/1155925017848242176
- phyrex 6y agoI’ve used this in production: def thread_first(current_value, args): for funcargs in args: if isinstance(funcargs, list): func, *restargs = funcargs else: func = funcargs restargs = [] current_value = func(current_value, *restargs) if current_value is None: break return current_value It’s modelled after clojure’s ‘->’. Your example would be thread_first(123, [add10, str, print])
- drivers99 6y agoLooks like forth (uses stack instead of pipes though). The first line will define add10 (which is assumed to exist in your example already, but I wanted to show how easy it was to create). 123 puts 123 on the stack. add10 adds ten to the number on the stack. "." prints (and removes) the top of the stack. Don't have to convert it to a string in this case. : add10 10 + 123 add10 .
- geophile 6y agoTake a look at marcel: https://github.com/geophile/marcel https://github.com/geophile/marcel. It is a pipe-objects-instead-of-strings shell, implemented in Python. But it is also an API allowing scripting from inside Python, e.g. for file, size in ls('/home/jao') | map(lambda f: (f, f.size)): print(f'{file.name}: {size}')
- bjourne 6y ago+1! I don't like your suggested syntax, but definitely, something like that would be a great addition to Python. Especially if it could be made to work with lists. Rough idea: range(10) | @ + 2 | @ % 2 == 0 | print(@)