welcome everybody to another jam session
where we're taking the I deres that we developed
and Haskell part of the course and show
how we can use dan and other languages
and that you may use at work and
and that are not your functional languages like ask
and this time we're going to take a
great look at scholar escolar
is a and hybrid functional object-oriented language
it's not a pure language and it's in
impaired language it has full side effect ever runs on the JVM
an and if you're a Haskell programmer
you will feel very familiar with Scala but also
if you come from C sharp and you will find
that skyline and fits like a hand in glove an
to you and of course an for Java programmers
Scala also has a lot of benefits
and in terms of conciseness that's a look at this college
cheat sheet to go through some of the features
and Scala has two kinds of
parable decorations and you can define a
far which means that you have immutable variable
or you can define a variable as FL
which means that it's read only and and so in that sense kala supports
a.m. immutable and an values
but this standard library has wide variety
of data structures including mutable data types and immutable data that show
its up to you as a developer
to pick and choose rich gonna programming
an fits better so here
are and Nikon I make this a little
that an larger here's how you define
am functions in skyline an
you right def and then the function
a name parameters and then the right hand side
and and Scala
often can hanford to return died an
and and in that case you know you have to write
equal show in this case this is a function addiction and
and returns X time six and inferred
dive here return type is and we've seen
an type synonyms and has gone and in skyline E
it even using the same syntax so you can define
type alias type re: was double here and then
wherever you used to use double and you can use are
here we see an example
an and how make that
even a little bit larger here for you and
here's an example often and on instruction
of /url on the expression you see the Lund expression
looks very much like ashkelon haskell remembered us
slash parameter single arrow
expression escolar its an
argument list wit types double arrow
and then the body up the and expression
we have seen several times in the lectures and has gone that we're using
the
an range comprehension said you know
square bracket 1.5 and Scala
also support does and then set up dot dot dot
it uses to show if you're right 125
and you get a sequence with the values 125 and of course
scholarship board all their familiar higher functions
on those collections messages map and filter
and an re jus and so on so here for example
we see am mapping the
function times to over the list
and remember and has goal we used
an sectioning an we were dry dysfunction
as opened branch start to
close Prenton and the skyline said using sectioning with Brent sheesh
you can use underscore so and you're right underscore
times too and which means to function love the axe
X times too and I must say often
the underscore looks pretty nice because you know it
it gives very visual indication of where the argument
am is plugged in answer here you see for example that embossing the
operator plus as a function and then
putting the two underscores their but of course
you can also and a ride it
like this and and Scala an there's many ways to ride the same thing to you can
also
ride take on section anything but II lie I prefer
this syntax alright
so an where today the church's I want to carry out a
the go here and doubles
an ass girl we used and doubles a lot
an and the syntax of tuples
in skyline is have very similar to Haskell
round brand to seize and develop use
and just like in Haskell the types
of the values in a couple can be different
and the lengths is part of the type and that's what we see here
they this thing here is doubled three
and it has to retype parameters an
you can do and a simple form of better matching
with double so I can count def the fine
a at couple here on the left hand side and it will bind
in this case X tu wong white 22 and
se23 and
here's the an notation for
that lists if I want to declare lest
I ride to list 123 an
and idiom and skyline is that you
ish and factory classes
an not list and that you don't write new
so you define a class list and the you define
a corresponding and Object List and that hasn't
apply met that takes these parameters and this apply matted
is something that you don't ride and
a year codes but that this column by the roll call
when you don't use a name for your mat and Ghana has many more tricks like that
and wit special Matt it's that
has special meaning to define get their chance at their San
index there's an and strong an another thing that I should mention is that
Scala also supports extension methods
just like C sharp and got then an so when you have a class you can
at and you matter to properties do that class
without having to touch the definition of the class that something
I like very much in that also corresponds to the Haskell style very
defined
on your ads right data type there's no matter it's the find their
you define all their operations on those dives
separately the control structures in Scala
our standards and condition alls
do while loops an foreign Lopes
and scott also
supports comprehension sis aint gonna list branches that we have seen
in Haskell an but it's got like they are called for comprehension San
maybe I should make this a little bit smaller
and and its doesn't look good
I'm going to make bigger layout here's a little bit messed up
but here you see this would be to an has call
and list comprehension and
it's called for calm branches and then it uses the same
left a road that we've seen haskell old Eagle branches work
an in Scala you can do pattern matching as well with the k-state
and so an you can define your own
and and apply Matt it's on an object
and then you can use that to do better matching so it's its kinda
nicer are more powerful pattern matching even
then asked began as going again only better match
on the constructors open algebra datatype and in Scotland
and you can tweak that colleges
rather big language and
it as implicit parameter that
an gonna factor out to the idea of Haskell type classes in high school
we have a typed last year when you see the am
type constraint on the left Ave type with a double arrow
the Hesco compiler will threats through and an instance of that type class
as an implicit parameter and whenever you call that netted
ordered that function it will come to propagate
that implicit parameter an but you cannot
used this concept implicit parameters
separate from type classes now there are haskell extensions
and that allow for implicit parameters
an and Scott to this I'm
a lot for order an and there's many many library functions
that use implicit parameters an
in a very powerful way and you can use that to yourself
okay to an of an looking at the cheat sheets
let's finish by showing some scholar college
and where do we go I wanted
go to mine Scotland I'll here
make this a bit larger for you
and I'm using the same
a more or less the same example as I did before sure there's the main method
I print lol I define a local function I define
wise I print itself and here I'm defining
a local function that closes over
immutable state and mutates that terrible
inside here too easy there's the
mutable variable Z and I muted inside here
goal this thing twice should the second time
and she will be forty and then will be mutated again so that's run this code
to see if everything works at advertised
and hello for and you see here that when I call mutate
twice the first time it brings 40
and the second time branch 15 so
you date is not a at your function
and let's am
at the explicit type definition so
what we see here said you date is a function that takes no arguments and
returns and
but it's not a mathematical your function
because if I call it twice it will give me a different result
but sometimes that is useful
alright so I hope that you a.m. enjoyed
a.m. this little overview of Scala an
and if you're on the JVM I would definitely consider
skyline as a great language to apply
the functional programming techniques that we learned in this course see you
next time