by tandav · 6y ago · 0 comments
123 | add10 | str | print
UPD: Some of my workarounds (dont try this at home lol): [twitter thread] https://twitter.com/tandavaya/status/1155925017848242176
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]) for file, size in ls('/home/jao') | map(lambda f: (f, f.size)):
print(f'{file.name}: {size}') : add10 10 +
123 add10 . range(10) | @ + 2 | @ % 2 == 0 | print(@)