Anonymous functions are true to form in that they have no name. They're useful when defining a function that will only be used once or to create a quick closure.
Let's say we have a function conversions that accepts another function, converter as input:
We could define a function normally and then pass it in by name... but it's usually easier to just define it anonymously:
funcdouble(aint)int{returna+a}funcmain(){// using a named functionnewX,newY,newZ:=conversions(double,1,2,3)// newX is 2, newY is 4, newZ is 6// using an anonymous functionnewX,newY,newZ=conversions(func(aint)int{returna+a},1,2,3)// newX is 2, newY is 4, newZ is 6}
Assignment
Complete the printReports function. It takes as input a sequence of messages, intro, body, outro. It should call printCostReport once for each message.
For each call of printCostReport, give it an anonymous function that returns the cost of a message as an integer. Here are the costs:
Intro: 2x the message length
Body: 3x the message length
Outro: 4x the message length
Use the built-in len() function to get the length of a string: