Defining a Function That Accepts a Function
You have already seen a few ways that lambdas can customize the work of standard library functions. The count function, for example, can take in a lambda to influence what it counts. You can also pass lambdas into functions that you write yourself.
A function parameter can accept arguments of any type, including arguments that are functions. A function-type parameter is defined like a parameter of any other type: You list it in the parentheses after the function name and include the type. To see how this works, you will add a new argument to narrate that will override the narrator’s current mood to allow for one-off formatting.
Add a parameter called modifier to the narrate function in ...
Get Kotlin Programming: The Big Nerd Ranch Guide, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.