Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Functions as Values5:51 with andi mitre
In this video, we will learn how to create and invoke functions as values.
[MUSIC] 0:00 [SOUND]. Hi, welcome to higher order functions in 0:04 Scala.. 0:07 In the following videos, we will learn a ton about functions in Scala. 0:08 We'll discuss first class functions, partially applied functions, currying and 0:12 much more. 0:17 One of the most important design principles of functional programming, 0:19 is to break down programs into several small functions. 0:23 By doing this, we allow for better readability, 0:26 more manageable chunks of code, and we also make our code much more testable. 0:30 In Scala, functions are known as first class functions. 0:35 This means that we can have functions as expressions or 0:39 unsaved literals and even pass them around as values. 0:42 Let's talk about function literals and functions as values. 0:46 The equal sign followed by a right arrow, also known as a rocket, 0:50 just signifies that everything on the left will turn into what's on the right. 0:54 This expression is a function literal, which takes an integer X and 0:59 adds one to it. 1:04 A function literal can be stored in a variable. 1:05 For example, in this instance addOne is the variable containing a function or 1:09 a function value, which we can call or even pass around to other functions. 1:14 Awesome! 1:19 Now, we understand function literals and functions as values. 1:20 Let's open up our code editor. 1:24 Within there, create a new package called functions. 1:26 And we'll copy our SuperHero object, into our functions package. 1:36 Click okay, and we'll delete anything inside of main. 1:46 Additionally, we'll just have to re-edit our run configurations. 1:51 Click the down arrow, edit configurations, and 1:55 within the main class we'll just select our SuperHeroes package, 2:00 click OK, apply, and then OK. 2:06 Let's recall our heroes array and create a function as a value, 2:09 which takes in a string and returns the same string, but in lower case. 2:13 Let's call lowerC by passing it our array values. 2:43 Awesome, let's recompile our app and check out our results. 2:57 As expected, 3:03 our function turns every element in the array into a lowercase string. 3:04 In addition to functions as values, Scala also supports anonymous functions. 3:08 Which are functions with no function names. 3:14 Normally to define a function, we would use the def keyword followed by a function 3:17 name, any arguments we would like to pass, and the function body. 3:21 However, we can achieve the same result, using an anonymous function. 3:35 You're probably wondering, how we could use this within our code. 3:45 Let's revisit our map of avengers and their rankings. 3:50 Let's assume, our superheros amped up their powers via a super potion, and 3:53 now their rankings have increased. 3:58 We will take their original ranking value, 4:00 subtract three from it by using our anonymous function. 4:03 In this example, we passed the same exact anonymous function, 4:49 which would yield the same result as defining a function and calling it. 4:52 Don't worry too much about .map now, we will discuss that in the next few videos. 4:57 In this example, 5:03 we pass the same exact anonymous function, which was created up here. 5:04 This anonymous function would yield the same result as defining a function and 5:09 calling it. 5:13 Don't worry too much about .map now, we will discuss that in the next few videos. 5:14 Let's print out our new rankings and recompile our app. 5:19 Awesome, as expected for 5:32 each element in our map the ranking has gone down by three. 5:34 We've begun to scratch the surface with how powerful function in Scala can be. 5:39 Understanding function literals and 5:44 anonymous functions, provides a great building block for the videos to come. 5:46 See you there. 5:50
You need to sign up for Treehouse in order to download course files.Sign up