Discover that since closures/functions have types, parameters of another function can be of that type. That means when calling a function with a closure as a parameter, a function or closure must be passed in. Once that parameter is assigned in the call of the function, it can be called just like the function/closure that was passed in.
- [Instructor] As I mentioned in the last video,…since closures and functions have a type,…you can make a parameter that type…and pass the closure or function in.…So just like a type for a var or a let,…you can just a type in your parameter list for a function.…And then you pass a closure or function of that type in.…So let's look at an example.…We had our stringify function that takes a string,…an int, and returns a string.…So its type is string int return string.…Well now we have this function called usesStringFactory…and it takes a parameter called stringFunc.…
Well what's the type of that parameter?…It's string and return string.…So it needs to be passed in something of that type,…string, int, return, string.…Do we know something of that type?…We sure do.…Stringify.…So we can pass in stringify as this parameter.…And then this function uses stringFactory…could call it and return its output.…In this case, it's calling it with number and 11.…Stringify concatenates those two, passes it back.…
But we could pass in anything of that type.…
- What are closures?
- Closures and function types
- Basic higher-order functions, including sort and forEach
- Powerful higher-order functions, including map and reduce
- Passing functions
- Closure optimizations
- Higher-order functions on sets, dictionaries, and strings