Partially Applied Functions5:16 with andi mitre
In this video we’ll discuss partially applied functions and currying. Like many functional programming languages, Scala allows us to transform a function, which may take many arguments into a function that takes just one argument.
Partially applied functions -- In Scala when we invoke a function but do not pass all of the required arguments we end up with a partial function.
Currying -- Named after Haskell Curry, currying is a process in which we take a function that takes two or more arguments and turn it into a function which takes one argument.
Hi, in this video we will cover partially applied functions and currying. 0:00 A partially applied function is a function for 0:05 which you do not pass all the arguments at once. 0:08 For instance, we may create a function which takes three parameters and 0:11 you supply either some or none of the arguments. 0:15 Currying on the other hand, is a process named after Haskell Curry. 0:18 And it allows the programmer to take a function which takes two arguments, and 0:23 turn it into a function that takes one argument. 0:27 These may sound a bit unusual, but let's dive into the code and 0:30 comfortable with both. 0:33 Let's take a look at a straightforward multiplication function. 0:36 Let's dive back into the code and comment out any println statements. 0:39 We've created a function called multiply to which we can either 1:03 pass all the arguments required, we can pass some of the arguments or 1:07 even none of the arguments. 1:11 Let's take a look. 1:13 In this case, we pass all the arguments and the result should be equal to six. 1:21 Let's take a look. 1:26 Awesome, now let's take a look at another example where we only pass in some 1:32 of the arguments. 1:36 In this case, we've only passed the first and the second argument, but 1:50 we haven't passed the third argument. 1:53 We need to complete the call by providing the last argument. 1:55 This can be extremely useful if we need to do some other calculations, 1:59 take that calculated value, and later on, pass it to our function. 2:03 However, for now, we're just going to pass the value 3. 2:07 Let's also take a look at an example where we don't pass any of the arguments. 2:16 In this case, we specify the none result, once the call the multiply function. 2:26 However, we haven't passed at any arguments and 2:31 to complete the call, we need to pass the three arguments. 2:34 Let's recompile and run our app. 2:41 Awesome, as expected, all three of our examples returned the value six. 2:44 As we mentioned before, 2:49 currying is a process in which we take a function that takes two or 2:51 more arguments and, in turn, create a function which only takes one argument. 2:54 To learn more about why this is important in functional programming, 2:59 check out the teacher's notes. 3:03 There are quite a few different ways to implement currying. 3:05 Let's take a look. 3:08 This can be read as add is a function that takes an integer x and 3:27 returns another function that takes a parameter y. 3:31 Remember, everything after the equals sign looks just like our function literals. 3:35 We can call our add function by passing in an integer which yields a function 3:40 that takes another function which will be evaluated when we call add again. 3:45 Let's take a look at how to call such a function. 3:50 Here, we've partially applied the function add with one parameter in order to 3:57 complete the call, we provide another parameter and we pass that to sum. 4:02 In this case, we're calling the sum function with an integer value of 2, 4:11 which in turn we'll call the odd function with an integer value of 8, 4:16 the expected result should be 10. 4:20 Let's re-round our app and check out the result. 4:22 Awesome, there's also a much shorter way and clearer syntax for recurring, 4:26 let's take a look at that. 4:30 In this case, we have a function which takes two separate parameters. 4:43 To envoke this function we can simply say 4:47 Let's recompile our app and check out the results. 4:59 Awesome, we learn about partially applied functions incurring in Scala. 5:04 These are some concepts you may not have been introduced to before. 5:09 So, I've added some links in the teacher's notes for additional reading. 5:13
You need to sign up for Treehouse in order to download course files.Sign up