I know that obj.func1().func2()
is called method chaining, but what is the technical term for:
func1(func2(), func3());
Where return of a function is used as an argument to another.
I know that obj.func1().func2()
is called method chaining, but what is the technical term for:
func1(func2(), func3());
Where return of a function is used as an argument to another.
I don't think it's function composition. Function composition means taking two or more functions and turning them into a new function, like f . g . h
in Haskell. Note that no function is called at this point.
Personally, I would refer to constructs like func1(func2(), func3())
as "nested function calls".
f(g())
is doing is calling f()
with an expression for the first argument, and it so happens the function call g()
qualifies as an expression. But if there has to be a name for it, this is as close as it gets.
– Blrfl
Nov 17 '11 at 20:34
In mathematics, it's called function composition. I don't think I've heard the term applied to programming, though. That may be because the usage is largely avoided for a few reasons. It can introduce strange bugs when the functions have side effects, due to compilers being free to evaluate func3 before func2. It's more difficult to debug because you can't set breakpoints on or print out intermediate results, and most people just plain find it harder to read.
printf("%d %d\n", strlen(a), strlen(b));
instead of using intermediary variables.
– user281377
Nov 17 '11 at 07:50
func2
or func3
will run all the way to completion and then the other will run. The solution is to write programs that don't depend on unspecified behavior.
– David Thornley
Nov 17 '11 at 17:40
func1(func2(func3(x)))
. Or technically, it refers to the functions themselves: if g(x) = f1(f2(f3(x)))
, then g
(not g(x)
) is the composition of f1
, f2
, and f3
. While I could see a way in which you could call the structure in the question "function composition," it strikes me as a very unusual use of the term, in a mathematical sense.
– David Z
Nov 18 '11 at 08:12
newfunc = lambda x, y : func1(func2(x), func3(y))
. Then you've "composed" a new function that can be called later: newfunc(x, y)
– Izkata
Jul 26 '12 at 14:25
As @Karl Bielefeldt points, it's called function composition in Math.
There is NO technical term for this thing in programming. And I think this is a Good thing, because it indicates that the operation is normal and Orthogonal.
Orthogonality in programming languages means that you can use an instruction/operation independent of it's context. For example, you can call a function/method in all of the following ways, and it would behave the same...
f1()(f2(), f3());
x = y + f4();
if ( f5() && !f6() ) doSomething();
f7() = f8() + f9(); // in C++ when a function returns a reference
x = f10() ? f11(f12(f13(x))) : f14();
You can read more on Orthogonality in Programming on Wikipedia, and there's a question on StackOverflow on this.
I really am not that good with terms, but I just read an article a few days ago that referred to the term Higher order functions, and here's an abstract of the definition per Wikipedia:
http://en.wikipedia.org/wiki/Higher-order_function
In mathematics and computer science, higher-order functions, functional forms, or functionals are functions which do at least one of the following:
- take one or more functions as an input
- output a function
All other functions are first-order functions. In mathematics higher-order functions are also known as operators or functionals. The derivative in calculus is a common example, since it maps a function to another function.
so in this case, since that scenario does take at least one function as an input/param, it'd be considered a higher order function, i believe.
func1(func2, func3)
. Note the absent parenthesis.
– fredoverflow
Nov 18 '11 at 05:23
func2
and func3
could be higher-order functions themselves and return a new function when called.
– Frerich Raabe
Jan 09 '20 at 11:33
obj.func1().func2() is called method chaining
- Correction: It's called a train wreck. – Yam Marcovic Nov 17 '11 at 10:18obj.func1()
always returns an object that hasfunc2()
as a member function. – Shadur-don't-feed-the-AI Nov 17 '11 at 11:33parser .setOption1(111) .setOption2("aaa").parse()
Properly indented. – ripper234 Nov 20 '11 at 23:32