1 00:00:00,520 --> 00:00:03,959 welcome everybody to another jam session 2 00:00:03,959 --> 00:00:08,360 where we're taking the I deres that we developed 3 00:00:08,360 --> 00:00:11,780 and Haskell part of the course and show 4 00:00:11,780 --> 00:00:14,849 how we can use dan and other languages 5 00:00:14,849 --> 00:00:18,279 and that you may use at work and 6 00:00:18,279 --> 00:00:21,660 and that are not your functional languages like ask 7 00:00:21,660 --> 00:00:24,869 and this time we're going to take a 8 00:00:24,869 --> 00:00:27,970 great look at scholar escolar 9 00:00:27,970 --> 00:00:32,369 is a and hybrid functional object-oriented language 10 00:00:32,369 --> 00:00:35,620 it's not a pure language and it's in 11 00:00:35,620 --> 00:00:40,059 impaired language it has full side effect ever runs on the JVM 12 00:00:40,059 --> 00:00:44,620 an and if you're a Haskell programmer 13 00:00:44,620 --> 00:00:48,379 you will feel very familiar with Scala but also 14 00:00:48,379 --> 00:00:51,840 if you come from C sharp and you will find 15 00:00:51,840 --> 00:00:58,379 that skyline and fits like a hand in glove an 16 00:00:58,379 --> 00:01:01,930 to you and of course an for Java programmers 17 00:01:01,930 --> 00:01:05,100 Scala also has a lot of benefits 18 00:01:05,100 --> 00:01:10,530 and in terms of conciseness that's a look at this college 19 00:01:10,530 --> 00:01:13,909 cheat sheet to go through some of the features 20 00:01:13,909 --> 00:01:20,150 and Scala has two kinds of 21 00:01:20,150 --> 00:01:23,450 parable decorations and you can define a 22 00:01:23,450 --> 00:01:26,509 far which means that you have immutable variable 23 00:01:26,509 --> 00:01:30,049 or you can define a variable as FL 24 00:01:30,049 --> 00:01:35,850 which means that it's read only and and so in that sense kala supports 25 00:01:35,850 --> 00:01:39,430 a.m. immutable and an values 26 00:01:39,430 --> 00:01:43,710 but this standard library has wide variety 27 00:01:43,710 --> 00:01:48,960 of data structures including mutable data types and immutable data that show 28 00:01:48,960 --> 00:01:51,170 its up to you as a developer 29 00:01:51,170 --> 00:01:55,119 to pick and choose rich gonna programming 30 00:01:55,119 --> 00:01:59,140 an fits better so here 31 00:01:59,140 --> 00:02:02,340 are and Nikon I make this a little 32 00:02:02,340 --> 00:02:06,000 that an larger here's how you define 33 00:02:06,000 --> 00:02:09,000 am functions in skyline an 34 00:02:09,000 --> 00:02:11,650 you right def and then the function 35 00:02:11,650 --> 00:02:15,230 a name parameters and then the right hand side 36 00:02:15,230 --> 00:02:18,860 and and Scala 37 00:02:18,860 --> 00:02:22,659 often can hanford to return died an 38 00:02:22,659 --> 00:02:26,629 and and in that case you know you have to write 39 00:02:26,629 --> 00:02:29,920 equal show in this case this is a function addiction and 40 00:02:29,920 --> 00:02:33,120 and returns X time six and inferred 41 00:02:33,120 --> 00:02:38,700 dive here return type is and we've seen 42 00:02:38,700 --> 00:02:43,200 an type synonyms and has gone and in skyline E 43 00:02:43,200 --> 00:02:46,269 it even using the same syntax so you can define 44 00:02:46,269 --> 00:02:49,840 type alias type re: was double here and then 45 00:02:49,840 --> 00:02:54,069 wherever you used to use double and you can use are 46 00:02:54,069 --> 00:02:57,650 here we see an example 47 00:02:57,650 --> 00:03:00,700 an and how make that 48 00:03:00,700 --> 00:03:04,310 even a little bit larger here for you and 49 00:03:04,310 --> 00:03:07,930 here's an example often and on instruction 50 00:03:07,930 --> 00:03:11,599 of /url on the expression you see the Lund expression 51 00:03:11,599 --> 00:03:14,939 looks very much like ashkelon haskell remembered us 52 00:03:14,939 --> 00:03:18,060 slash parameter single arrow 53 00:03:18,060 --> 00:03:21,680 expression escolar its an 54 00:03:21,680 --> 00:03:25,359 argument list wit types double arrow 55 00:03:25,359 --> 00:03:29,060 and then the body up the and expression 56 00:03:29,060 --> 00:03:35,500 we have seen several times in the lectures and has gone that we're using 57 00:03:35,500 --> 00:03:36,400 the 58 00:03:36,400 --> 00:03:39,680 an range comprehension said you know 59 00:03:39,680 --> 00:03:43,159 square bracket 1.5 and Scala 60 00:03:43,159 --> 00:03:46,760 also support does and then set up dot dot dot 61 00:03:46,760 --> 00:03:50,099 it uses to show if you're right 125 62 00:03:50,099 --> 00:03:56,440 and you get a sequence with the values 125 and of course 63 00:03:56,440 --> 00:04:00,139 scholarship board all their familiar higher functions 64 00:04:00,139 --> 00:04:04,060 on those collections messages map and filter 65 00:04:04,060 --> 00:04:07,989 and an re jus and so on so here for example 66 00:04:07,989 --> 00:04:11,040 we see am mapping the 67 00:04:11,040 --> 00:04:14,269 function times to over the list 68 00:04:14,269 --> 00:04:17,409 and remember and has goal we used 69 00:04:17,409 --> 00:04:22,490 an sectioning an we were dry dysfunction 70 00:04:22,490 --> 00:04:24,910 as opened branch start to 71 00:04:24,910 --> 00:04:29,450 close Prenton and the skyline said using sectioning with Brent sheesh 72 00:04:29,450 --> 00:04:33,330 you can use underscore so and you're right underscore 73 00:04:33,330 --> 00:04:37,010 times too and which means to function love the axe 74 00:04:37,010 --> 00:04:40,670 X times too and I must say often 75 00:04:40,670 --> 00:04:44,470 the underscore looks pretty nice because you know it 76 00:04:44,470 --> 00:04:49,060 it gives very visual indication of where the argument 77 00:04:49,060 --> 00:04:53,460 am is plugged in answer here you see for example that embossing the 78 00:04:53,460 --> 00:04:56,490 operator plus as a function and then 79 00:04:56,490 --> 00:05:00,370 putting the two underscores their but of course 80 00:05:00,370 --> 00:05:04,530 you can also and a ride it 81 00:05:04,530 --> 00:05:09,480 like this and and Scala an there's many ways to ride the same thing to you can 82 00:05:09,480 --> 00:05:09,990 also 83 00:05:09,990 --> 00:05:13,640 ride take on section anything but II lie I prefer 84 00:05:13,640 --> 00:05:17,870 this syntax alright 85 00:05:17,870 --> 00:05:24,870 so an where today the church's I want to carry out a 86 00:05:25,860 --> 00:05:29,660 the go here and doubles 87 00:05:29,660 --> 00:05:33,070 an ass girl we used and doubles a lot 88 00:05:33,070 --> 00:05:36,590 an and the syntax of tuples 89 00:05:36,590 --> 00:05:40,120 in skyline is have very similar to Haskell 90 00:05:40,120 --> 00:05:43,610 round brand to seize and develop use 91 00:05:43,610 --> 00:05:46,630 and just like in Haskell the types 92 00:05:46,630 --> 00:05:49,790 of the values in a couple can be different 93 00:05:49,790 --> 00:05:54,280 and the lengths is part of the type and that's what we see here 94 00:05:54,280 --> 00:05:58,050 they this thing here is doubled three 95 00:05:58,050 --> 00:06:01,990 and it has to retype parameters an 96 00:06:01,990 --> 00:06:06,750 you can do and a simple form of better matching 97 00:06:06,750 --> 00:06:09,750 with double so I can count def the fine 98 00:06:09,750 --> 00:06:13,740 a at couple here on the left hand side and it will bind 99 00:06:13,740 --> 00:06:16,890 in this case X tu wong white 22 and 100 00:06:16,890 --> 00:06:20,169 se23 and 101 00:06:20,169 --> 00:06:23,280 here's the an notation for 102 00:06:23,280 --> 00:06:26,570 that lists if I want to declare lest 103 00:06:26,570 --> 00:06:30,110 I ride to list 123 an 104 00:06:30,110 --> 00:06:33,550 and idiom and skyline is that you 105 00:06:33,550 --> 00:06:36,670 ish and factory classes 106 00:06:36,670 --> 00:06:41,010 an not list and that you don't write new 107 00:06:41,010 --> 00:06:44,390 so you define a class list and the you define 108 00:06:44,390 --> 00:06:48,190 a corresponding and Object List and that hasn't 109 00:06:48,190 --> 00:06:52,600 apply met that takes these parameters and this apply matted 110 00:06:52,600 --> 00:06:55,870 is something that you don't ride and 111 00:06:55,870 --> 00:06:59,800 a year codes but that this column by the roll call 112 00:06:59,800 --> 00:07:05,350 when you don't use a name for your mat and Ghana has many more tricks like that 113 00:07:05,350 --> 00:07:08,640 and wit special Matt it's that 114 00:07:08,640 --> 00:07:12,460 has special meaning to define get their chance at their San 115 00:07:12,460 --> 00:07:17,500 index there's an and strong an another thing that I should mention is that 116 00:07:17,500 --> 00:07:20,250 Scala also supports extension methods 117 00:07:20,250 --> 00:07:24,850 just like C sharp and got then an so when you have a class you can 118 00:07:24,850 --> 00:07:28,750 at and you matter to properties do that class 119 00:07:28,750 --> 00:07:32,350 without having to touch the definition of the class that something 120 00:07:32,350 --> 00:07:36,510 I like very much in that also corresponds to the Haskell style very 121 00:07:36,510 --> 00:07:37,470 defined 122 00:07:37,470 --> 00:07:42,460 on your ads right data type there's no matter it's the find their 123 00:07:42,460 --> 00:07:45,919 you define all their operations on those dives 124 00:07:45,919 --> 00:07:50,950 separately the control structures in Scala 125 00:07:50,950 --> 00:07:54,960 our standards and condition alls 126 00:07:54,960 --> 00:07:58,550 do while loops an foreign Lopes 127 00:07:58,550 --> 00:08:03,890 and scott also 128 00:08:03,890 --> 00:08:08,050 supports comprehension sis aint gonna list branches that we have seen 129 00:08:08,050 --> 00:08:12,950 in Haskell an but it's got like they are called for comprehension San 130 00:08:12,950 --> 00:08:17,350 maybe I should make this a little bit smaller 131 00:08:17,350 --> 00:08:20,380 and and its doesn't look good 132 00:08:20,380 --> 00:08:23,940 I'm going to make bigger layout here's a little bit messed up 133 00:08:23,940 --> 00:08:27,810 but here you see this would be to an has call 134 00:08:27,810 --> 00:08:31,610 and list comprehension and 135 00:08:31,610 --> 00:08:34,940 it's called for calm branches and then it uses the same 136 00:08:34,940 --> 00:08:41,719 left a road that we've seen haskell old Eagle branches work 137 00:08:41,719 --> 00:08:46,430 an in Scala you can do pattern matching as well with the k-state 138 00:08:46,430 --> 00:08:49,990 and so an you can define your own 139 00:08:49,990 --> 00:08:54,290 and and apply Matt it's on an object 140 00:08:54,290 --> 00:08:57,390 and then you can use that to do better matching so it's its kinda 141 00:08:57,390 --> 00:09:00,950 nicer are more powerful pattern matching even 142 00:09:00,950 --> 00:09:04,110 then asked began as going again only better match 143 00:09:04,110 --> 00:09:08,270 on the constructors open algebra datatype and in Scotland 144 00:09:08,270 --> 00:09:12,860 and you can tweak that colleges 145 00:09:12,860 --> 00:09:16,230 rather big language and 146 00:09:16,230 --> 00:09:19,870 it as implicit parameter that 147 00:09:19,870 --> 00:09:25,510 an gonna factor out to the idea of Haskell type classes in high school 148 00:09:25,510 --> 00:09:28,700 we have a typed last year when you see the am 149 00:09:28,700 --> 00:09:32,620 type constraint on the left Ave type with a double arrow 150 00:09:32,620 --> 00:09:38,190 the Hesco compiler will threats through and an instance of that type class 151 00:09:38,190 --> 00:09:41,620 as an implicit parameter and whenever you call that netted 152 00:09:41,620 --> 00:09:44,950 ordered that function it will come to propagate 153 00:09:44,950 --> 00:09:48,029 that implicit parameter an but you cannot 154 00:09:48,029 --> 00:09:51,080 used this concept implicit parameters 155 00:09:51,080 --> 00:09:55,140 separate from type classes now there are haskell extensions 156 00:09:55,140 --> 00:09:58,459 and that allow for implicit parameters 157 00:09:58,459 --> 00:10:01,560 an and Scott to this I'm 158 00:10:01,560 --> 00:10:06,900 a lot for order an and there's many many library functions 159 00:10:06,900 --> 00:10:10,110 that use implicit parameters an 160 00:10:10,110 --> 00:10:13,610 in a very powerful way and you can use that to yourself 161 00:10:13,610 --> 00:10:17,890 okay to an of an looking at the cheat sheets 162 00:10:17,890 --> 00:10:21,330 let's finish by showing some scholar college 163 00:10:21,330 --> 00:10:25,170 and where do we go I wanted 164 00:10:25,170 --> 00:10:32,170 go to mine Scotland I'll here 165 00:10:34,019 --> 00:10:36,230 make this a bit larger for you 166 00:10:36,230 --> 00:10:39,820 and I'm using the same 167 00:10:39,820 --> 00:10:43,940 a more or less the same example as I did before sure there's the main method 168 00:10:43,940 --> 00:10:47,680 I print lol I define a local function I define 169 00:10:47,680 --> 00:10:51,730 wise I print itself and here I'm defining 170 00:10:51,730 --> 00:10:54,950 a local function that closes over 171 00:10:54,950 --> 00:10:58,209 immutable state and mutates that terrible 172 00:10:58,209 --> 00:11:02,060 inside here too easy there's the 173 00:11:02,060 --> 00:11:05,430 mutable variable Z and I muted inside here 174 00:11:05,430 --> 00:11:10,560 goal this thing twice should the second time 175 00:11:10,560 --> 00:11:15,730 and she will be forty and then will be mutated again so that's run this code 176 00:11:15,730 --> 00:11:19,370 to see if everything works at advertised 177 00:11:19,370 --> 00:11:24,200 and hello for and you see here that when I call mutate 178 00:11:24,200 --> 00:11:27,250 twice the first time it brings 40 179 00:11:27,250 --> 00:11:30,370 and the second time branch 15 so 180 00:11:30,370 --> 00:11:33,870 you date is not a at your function 181 00:11:33,870 --> 00:11:38,339 and let's am 182 00:11:38,339 --> 00:11:41,850 at the explicit type definition so 183 00:11:41,850 --> 00:11:46,380 what we see here said you date is a function that takes no arguments and 184 00:11:46,380 --> 00:11:47,430 returns and 185 00:11:47,430 --> 00:11:50,810 but it's not a mathematical your function 186 00:11:50,810 --> 00:11:55,010 because if I call it twice it will give me a different result 187 00:11:55,010 --> 00:11:58,190 but sometimes that is useful 188 00:11:58,190 --> 00:12:01,360 alright so I hope that you a.m. enjoyed 189 00:12:01,360 --> 00:12:04,829 a.m. this little overview of Scala an 190 00:12:04,829 --> 00:12:09,350 and if you're on the JVM I would definitely consider 191 00:12:09,350 --> 00:12:12,899 skyline as a great language to apply 192 00:12:12,899 --> 00:12:16,500 the functional programming techniques that we learned in this course see you 193 00:12:16,500 --> 00:12:16,850 next time