These exercises test your knowledge of function values and closures.
-
Assign an anonymous function to a variable that prints its int argument. Then call it inside a for i
:=range 5 loop. -
Write a function called makeCounter that takes in no parameters and returns a closure with the function signature func() int. The first call to the closure should return 1, and each subsequent call should return the next-largest integer. Demonstrate that these two separate counters are independent. 6.Write two functions, double(int) int, which returns two times the value of its parameter, and square(int) int, which returns the square of its parameter. Write a third function transform(nums []int, f func(int) int) []int. This function applies f to each element and returns a new slice. Call it twice, once with double as a parameter, and once with square.