Anonymous Functions and Multiple Dispatch
1. Multiple dispatch
We'll now look at anonymous functions and recap multiple dispatch.2. Multiple dispatch recap
You've seen multiple dispatch in the introduction to Julia course, but it is worth recapping one of the most powerful features of Julia. Remember, multiple dispatch allows us to run a different method based on the type of argument we pass into a function. Here, we have a function called add_values, returning the sum of x and y. Passing one and two to this function, we get three. What if we want to pass a string into this function? We can't add two strings together in Julia using the plus, so we get an error. We must define another function with type restrictions on the arguments for string input. This way, if we call add_values with any data type that is not a string, they will be added together using the plus. If we pass two strings into the function, as here with A and B, it will instead use the multiplication operator.3. Anonymous functions
So far we have seen functions and many types of function arguments. However, anonymous functions are different. We don't give them a name when declared. Anonymous functions are often used to create quick functions for local use, discarded afterward. A quick way to define an anonymous function is by writing the expression in brackets. Here, we pass a value for x into our function, and the function to evaluate is x squared plus three. We then, in brackets, pass a value for x, in this case, two. Executing this line will define the anonymous function, pass two, evaluate the function, and then return an output of seven. This is a speedy way to define a function for immediate evaluation.4. Complex anonymous functions
We can also perform more complex operations with anonymous functions, for example, using the map function. The map function applies each value in a collection to a function on an element-wise basis. Here, we have combined an anonymous function with the map function. Our anonymous function is defined with x as our function argument and a mathematical function to evaluate. This forms the first argument of the map function. We then define a vector with three values and pass that as the second argument to the map function. map has now evaluated our anonymous function for each value in our vector. We can also pass multiple function arguments to an anonymous function by providing the function arguments as a tuple. In this example, we pass values for both x and y into the function. The only change in our equation is adding y at the end. We pass one vector to x and another vector to y. We get the expected result - one has been added to each result. Passing an anonymous function to the map function makes it easy to evaluate a function on a range of values quickly.5. Filtering DataFrames
One final use case is the filter function which allows us to filter a data structure for specific values. Here, we filter our stock_data DataFrame on the Date column, looking for any data on the 21st of January, 2022. We use the filter function, passing in the Date column, and then we define an anonymous function that will check if the date is equal to 21/01/2022 in stock_data. This function call can be read as "for each element in the column Date, call the element n and check whether n equals "21/01/2022". Running this line we can see that we have successfully filtered on the date column and got the expected output of one row.6. Let's practice!
We've covered some of the more advanced topics in Julia. Let's practice them!Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.