Function composition
- [Instructor] The functional programming aspects of Go are often overlooked, but it has native function support, which allows us to save functions to variables, pass them as parameters, and even compose them. Let's discuss the unique power of functions. So far, we have mostly defined methods that are attached to custom types. The custom types have typically then carried the results and related data. This is the usual way we would write Go code. An alternative way of writing code is through functional code. Functional code is composed of pure functions, which only rely on their parameters and produce no side effects. This means that they should not save results to outside variables or files. Functional code consists of applying or invoking, and composing or combining, pure functions. This can be easily done in Go due to its native support for functions. The G function in this example can take other functions as parameters. Then it can invoke the parameter function inside its body and make use of any returned values or results. Pure functions can return results and calculated values, but they just shouldn't produce results outside their scope. Functions can also be assigned to variables with the function F defined in the main function, but not invoked. The function G is then invoked with F as a parameter. It can be tricky to get into the habit of writing functional code, but it's an important tool to fully understand together with the imperative way of writing code that we've been exploring so far. Due to its lack of side effects, functional code is stateless and easier to test. Finally, functional code is easier to parallelize as it solely depends on its input, so we don't need to synchronize access to shared resources or files. You'll practice functional code writing skills by calculating the variance of a dataset a little later. The mean indicates the central tendency of a given dataset, while the variance indicates the variability or spread of the dataset. These two measures are central concepts in statistics. Given a slice X of length N, begin by defining a Square function, which multiplies a number by itself and a parameterized Sum function, which will apply the given function to each number before it calculates the sum of the transformed elements. You should remember this from the previous challenge, the mean uses the Sum function without applying any function to its elements. In order to calculate the variance, first you have to calculate the sum of residuals denoted by the function SumRes. This is calculated by calling the Sum function and passing a Transformation function, which squares the difference between each number and the mean X. The Square function is provided by the standard library in the math.Pow function. You might be wondering why the residuals are squared before being added together. The purpose is to give equal weighting to both positive and negative residuals. Finally, let's calculate the variance by dividing the sum of residuals by N. That's all we need to calculate the spread of our dataset. I have defined a CalculateVariance function for you to implement. This currently simply returns nil and a Not-Implemented error. Your challenge is to implement the variance calculation using a functional code approach by following the outline steps for calculation. Good luck!
随堂练习,边学边练
下载课堂讲义。学练结合,紧跟进度,轻松巩固知识。