1 00:00:00,410 --> 00:00:03,600 In this video, we will build our first Scala app. 2 00:00:03,600 --> 00:00:08,470 We'll learn how to create functions for loops, guards, and k statements. 3 00:00:08,470 --> 00:00:10,140 I'll be using IntelliJ as my IDEA. 4 00:00:10,140 --> 00:00:15,270 And I've included a link in the teacher notes if you'd like to download the IDEA 5 00:00:15,270 --> 00:00:16,080 and follow along. 6 00:00:17,330 --> 00:00:19,190 Let's fire up IntelliJ. 7 00:00:19,190 --> 00:00:21,360 Click create new project. 8 00:00:21,360 --> 00:00:26,170 Choose Scala from the list, click next, and provide a project name. 9 00:00:27,910 --> 00:00:28,590 Click finish. 10 00:00:29,920 --> 00:00:34,540 In the project source directory, we'll right click and create a use Scala class. 11 00:00:36,020 --> 00:00:38,619 For now we'll create an object called superheroes. 12 00:00:44,126 --> 00:00:48,108 Within that, we will create a main functions that prints out Hello World. 13 00:01:04,152 --> 00:01:06,470 Let's compile and run our application. 14 00:01:06,470 --> 00:01:08,000 On the upper right hand corner, 15 00:01:08,000 --> 00:01:11,930 we'll click the down arrow, click edit configurations, 16 00:01:11,930 --> 00:01:16,480 click the plus sign to add a new configuration to run as an application. 17 00:01:17,760 --> 00:01:21,160 And for our main class, we'll select our super heros object. 18 00:01:23,653 --> 00:01:26,210 We can also provide a name for this configuration. 19 00:01:27,520 --> 00:01:31,260 Then we'll click Apply, and then select OK. 20 00:01:31,260 --> 00:01:33,610 To run our app we'll click on the green arrow. 21 00:01:35,230 --> 00:01:40,090 Great, we've created our first Scala app which prints Hello World to the screen. 22 00:01:40,090 --> 00:01:43,800 For the next few examples, we'll be using what is known as a work sheet. 23 00:01:43,800 --> 00:01:46,900 A Scala file which is evaluated on save. 24 00:01:46,900 --> 00:01:51,150 Scala work sheets are very useful as they can show the output of our expressions 25 00:01:51,150 --> 00:01:52,780 on the right hand side. 26 00:01:52,780 --> 00:01:57,670 To define a function in Scala, we use the def keyword along with a functions name, 27 00:01:57,670 --> 00:02:01,110 followed by a comma separated list of parameters 28 00:02:01,110 --> 00:02:03,860 we will like to pass in a function body. 29 00:02:03,860 --> 00:02:08,152 For each parameter we will have to provide the type annotation proceeded [SOUND] by 30 00:02:08,152 --> 00:02:08,665 a colon. 31 00:02:08,665 --> 00:02:13,227 As a Scala compiler cannot infer function parameter types. 32 00:02:13,227 --> 00:02:18,560 We'll right click the root of our directory, select new. 33 00:02:18,560 --> 00:02:23,540 Then click on Scala Worksheet, and then we'll provide a name for our worksheet. 34 00:02:23,540 --> 00:02:25,181 In our case, we'll call it functions. 35 00:02:48,343 --> 00:02:53,002 The following code creates a function name multiplied by two, which takes in a single 36 00:02:53,002 --> 00:02:57,060 integer parameter followed by an equal sign in the function body. 37 00:02:57,060 --> 00:03:02,090 The function checks of x is equal to zero in which case it'll return the value of x 38 00:03:02,090 --> 00:03:04,490 or otherwise both applies multiplies x by two. 39 00:03:04,490 --> 00:03:08,960 The last expression evaluated in a function is always returned. 40 00:03:08,960 --> 00:03:13,430 So you don't have to explicitly call return as you might do so in Java. 41 00:03:13,430 --> 00:03:18,690 Also, it is almost always not necessary to provide the result type of a function. 42 00:03:18,690 --> 00:03:21,890 Look at a case where it is required here in a bit. 43 00:03:21,890 --> 00:03:26,386 To explicitly return the type would have to add a colon followed by the type 44 00:03:26,386 --> 00:03:29,953 annotation before the equal sign in our function like so. 45 00:03:32,856 --> 00:03:36,260 Scala also supports the fold arguments for functions. 46 00:03:36,260 --> 00:03:40,890 So if an argument is not passed, they'll provide the default value to the function. 47 00:03:40,890 --> 00:03:45,210 Additionally, if a function body exceeds to multiple lines. 48 00:03:45,210 --> 00:03:47,135 You can used a block implement your code. 49 00:03:47,135 --> 00:03:48,334 Let's take a look. 50 00:04:08,279 --> 00:04:12,746 In this case, X will default to ten and we'll multiply the value passed in for 51 00:04:12,746 --> 00:04:14,450 the variable Y. 52 00:04:14,450 --> 00:04:18,450 This is exactly how we would invoke the function with one parameter which would 53 00:04:18,450 --> 00:04:25,590 return 50 as variable Y takes the value five and variable X is defaulted to ten. 54 00:04:25,590 --> 00:04:29,880 And as you can see on the right-hand side the result of our function call 55 00:04:29,880 --> 00:04:31,110 is equal to 50. 56 00:04:31,110 --> 00:04:35,090 In Java, you may have come across functions that don't return anything. 57 00:04:35,090 --> 00:04:37,680 We try to find with the return type of void. 58 00:04:37,680 --> 00:04:42,380 Similarly in Scala, you can create functions that don't return anything 59 00:04:42,380 --> 00:04:44,710 by setting the result type to unit. 60 00:04:44,710 --> 00:04:45,275 Let's take a look 61 00:04:55,481 --> 00:04:59,655 Here we've created a function called greeting which does not return a value but 62 00:04:59,655 --> 00:05:02,032 simply prints out Hello World to the screen. 63 00:05:05,095 --> 00:05:08,777 Earlier we mentioned that there's a case when we need to explicitly provide 64 00:05:08,777 --> 00:05:11,190 the return type of a function. 65 00:05:11,190 --> 00:05:14,380 In Scala, we need to do so with the recursive function. 66 00:05:14,380 --> 00:05:17,600 A recursive function is a function which calls itself. 67 00:05:17,600 --> 00:05:21,187 Finding the factorial of a number is easily implemented as a recursive 68 00:05:21,187 --> 00:05:21,803 function. 69 00:05:42,453 --> 00:05:42,953 Great. 70 00:05:44,410 --> 00:05:47,530 Unlike Java, Scala does not directly support the for 71 00:05:47,530 --> 00:05:50,200 statement you may be familiar with. 72 00:05:50,200 --> 00:05:52,928 Let's dive into Scala loops and see how they differ. 73 00:06:03,414 --> 00:06:06,290 You can read the left arrow as, in. 74 00:06:06,290 --> 00:06:10,000 So for i in one to ten, print i. 75 00:06:10,000 --> 00:06:13,850 Therefore, we loop from one to ten and print the element i. 76 00:06:13,850 --> 00:06:18,840 In this case, one to ten creates an immutable range, and it's also inclusive. 77 00:06:18,840 --> 00:06:21,911 For an exclusive range, we can use the keyword until. 78 00:06:30,825 --> 00:06:35,320 The form variable left arrow expression is known as a generator. 79 00:06:35,320 --> 00:06:39,825 Each generator in Scala can have a guard, which is a boolean condition preceded by 80 00:06:39,825 --> 00:06:43,759 an if that allows for processing of elements that match the condition. 81 00:06:58,025 --> 00:07:02,697 In this example, we use a guard which allows for processing of the range and 82 00:07:02,697 --> 00:07:05,540 we print all the even numbers from one to ten. 83 00:07:05,540 --> 00:07:09,530 A for comprehension is a type of loop which returns a collection of values. 84 00:07:09,530 --> 00:07:11,840 These are great for iterating over collections and 85 00:07:11,840 --> 00:07:14,380 attaining a new collection of the same type. 86 00:07:14,380 --> 00:07:17,130 We can achieve this in Scala with the yield keyword. 87 00:07:31,992 --> 00:07:32,882 As we can see, 88 00:07:32,882 --> 00:07:37,655 yield creates a new immutable collection which is stored in numbers. 89 00:07:37,655 --> 00:07:40,645 Scala also provides a very useful case statement 90 00:07:40,645 --> 00:07:43,075 which is similar to Java switch statement. 91 00:07:43,075 --> 00:07:44,840 Let's take a look at how that works 92 00:08:17,270 --> 00:08:23,525 The last line, the case_ is the catch-all phrase which you always want to provide. 93 00:08:23,525 --> 00:08:27,755 Otherwise if a match is not found there will be a match error thrown. 94 00:08:27,755 --> 00:08:33,010 This is similarly specified using the default keyword in Java However 95 00:08:33,010 --> 00:08:36,330 unlike Java switch statements, there's no fall through problem. 96 00:08:36,330 --> 00:08:40,220 So we do not need to have a break statement after each case. 97 00:08:40,220 --> 00:08:42,637 We can also save the result into value or 98 00:08:42,637 --> 00:08:45,798 even create a function based on pattern matching. 99 00:09:36,153 --> 00:09:39,393 Calling findAge passing the number 20, 100 00:09:39,393 --> 00:09:43,630 returns us the age as a string with a value 20, great.