\$\begingroup\$
\$\endgroup\$
0
How would you name this function (I gave in a name pipe
in the example) which constructs combines a chain of functions with a single attribute into a single callable:
# Python
def pipe(*functions):
"""
pipe(c, b, a)(value) == a(b(c(value)))
"""
if len (functions) == 1:
return functions[0]
def chain(val):
for func in functions:
val = func(val)
return val
chain.func_name = '__'.join(repr(f) for f in functions)
return chain
Jamal
35.2k13 gold badges134 silver badges238 bronze badges
1 Answer 1
\$\begingroup\$
\$\endgroup\$
2
The function performs composition, so the name should be compose
. Or rather reverse_compose
, because it applies functions in the order reversed from natural composition.
answered Jun 19, 2014 at 17:25
-
\$\begingroup\$ Do you think it would be include to include something like
nary_compose
ormulti_compose
, or iscompose
alone sufficient? \$\endgroup\$wchargin– wchargin2014年06月19日 17:30:46 +00:00Commented Jun 19, 2014 at 17:30 -
\$\begingroup\$ Unless you plan to add other compositors, I'd stick with plain compose. Otherwise, a distinctive prefix may be necessary. \$\endgroup\$vnp– vnp2014年06月19日 17:34:17 +00:00Commented Jun 19, 2014 at 17:34
lang-py