1
\$\begingroup\$

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
asked Jun 19, 2014 at 15:25
\$\endgroup\$
0

1 Answer 1

2
\$\begingroup\$

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
\$\endgroup\$
2
  • \$\begingroup\$ Do you think it would be include to include something like nary_compose or multi_compose, or is compose alone sufficient? \$\endgroup\$ Commented 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\$ Commented Jun 19, 2014 at 17:34

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.