I was reading this question: Decorator functions in Go and am wondering why the execution order of the example in the accepted answer seems reversed to me.
I have broken it down to the following minimal example and am wondering if the effect is due to function chaining.
// Interesting Part
some_string := "Some_String"
var fn3 StringManipulator = ident
fn3 = AppendDecorator(" GOLANG", ToLower(PrependDecorator("DECORATED ", fn3)))
fmt.Println(fn3(some_string))
// Prints "DECORATED some_string golang"
// Function Definitions
func ToLower(m StringManipulator) StringManipulator {
return func(s string) string {
lower := strings.ToLower(s)
return m(lower)
}
}
func AppendDecorator(x string, m StringManipulator) StringManipulator {
return func(s string) string {
return m(s + x)
}
}
func PrependDecorator(x string, m StringManipulator) StringManipulator {
return func(s string) string {
return m(x + s)
}
}
As mentioned in the code this yields "DECORATED some_string golang" indicating that the functions were executed from left to right, whereas normal functions evaluate from innermost to outermost, i.e. right to left. [This reminds me of postmultiplication of transformation matrices - there the order is also "reversed", i.e. M_1 * M_2 * M_3] Is this due to function chaining or what is the reason? Could somebody help me understand how this executes in detail?
Thank you in advance.