welcome back to the lectures on higher-order functions
and we just looked at the
mother of all higher are the functions over lists for our
an and we will and this section
read a few other standard a higher-order functions that are available
and the Haskell library and
one of the those functions is function composition and function composition is
another fundamental operation
in functional programming an functional programming as programming with
functions
and if we have functions we want to gonna
combined two functions into another function
just like addition takes two numbers and give this a new number
we want to have a similar operator that takes two functions
and combines them into a new French well
here's the and definition of that operator
and it's written as dot and that's cool so unlike
object-oriented programming where dot means invoking the method
and has called dot means composing to function show
in both cases the dot is used for the most
fundamental operation on there gonna be a object
that the language support an object-oriented language
bf objects and we can call methods on them and Haskell
we have functions that we can combo am
and here's the type and function composition dot
takes a function from BTC there's the function for b2c
and it takes a function from a to be and that readers a function from
a2c now when you look at this
type it can be a little bit confusing
biggest there's a bee here as the output
and the bee is here as the input cell it's gonna be a
seems to be that things are in the wrong order but debt has
everything to do with the fact that
function application shosh age so here's a definition
of composition so what we do is read
two functions and then
we first applied G to axe
and then reapply after to the result
case allege now see
if those types make sense to us an
the title that here
is a function from B to C the typeof
G is a function from a to be well
then in that case X must be a value of type A
so if we apply G to at
to axe we have we are blind dysfunction addiction a.m. 20 NB
so the result of applying G two axes be
hi and that fits into here and we get to see
show to get the
return type here you get the value of type A
and we return a value of type C
alright so at first I thought might look a little bit confusing
but once we figure it out and got you know we used in fact that
function application here shosh age to deride
everything's Folge falls into place I'm
and here's an example %uh using function composition
suppose we want to define the function alt that
checks whether a an integer is art well
alt is defined as not even
well there we go definition it's at
first check whether g++ even and then an
inverter result and this definition here
this is what people often call point freestyle
biggest the other way we could ride at a.m.
alt would be love blacks even %uh axe
not of that but now we have a long time brent is she said that's got kind of a
little bit for books
and a lot of functional programmers preferred to you should function
composition
to remove all the lawn that's and the brain disease but I have to warn you
and and I'm I'm guilty of this too
when you just start using function applique a French
in you're trying to use it everywhere and it becomes a sport
to write things in point freestyle ride them very compactly
an the price you pay said becomes very hard to reach and now the people that
Reacher code
have to do all this function composition
in order to understand what's going on and typically
when they understand what's going on the light bulb goes on it
okay but I think it's bad either to gonna Lu's
function composition sparingly an
because you always have to write code for others to read
okay so you don't write code to look cool yourself and see all ok I can do
this point freestyle
am plus there's our domestic programs that make things point free for you show
your not as cool as you think
okay so use function composition sparingly
and don't go bananas wit function composition
okay
here are a couple of a other functions
and that are in the standard library higher-order functions
am these are most of them are defined
and using and for the are using
and comprehension these are all interchangeably
and I prefer using fold our
and some people a briefer an
this comprehension ce at the reason I prefer for the car is dead
for are and this concept of folding over and
a datatype works for any recursive data type
birch list comprehension am only work for less
an we can use general branches but it gets all a bit tedious
so I at some point it's just easier to
define default function and and the fine
and other functions on top of that all right sir
but let's just look at this wrong here all is defined
as and map
be a VAX to the list access
and then and Jack rather every one of those is true
measure a nicer definition of dish would be
map of P combos and any
is the same thing remapped be a fax I P
over the list access and then we call
or so for example if you want to check that
this string here ABC space deef
days in space you can call any Espace
function from character to bull and this will return true
here's another interesting function
take while take while takes a predicate
and unlike filter rich goes through the whole list and throws away all the
elements
for rates abroad again does not halt take while
is like a while loop so it goes uses predicate
and chop shop elements from this list and returning them
until this predicate this fall so let's look at the definition
if the list is empty well what can we do we can only return the empty list
in case you be have a non-empty lest
it looks like this ex-cons excess
and now we have two cases either
the affects does not halt ever that case we
and terminates the recursion by returning the anti lest
and ft up if the attacks is true
there's a trap door here and in the stadia
take while not fault through the
all an FB have access through
then we Goldman Sachs onto the recursive call
of P take while be affects so for example
began they have a string and we can take
and characters from the string as long as they are
letters by the intake while is all fun and then
it will take ABC and since this space is not
and an often America character as well terminated
right there do I need to take while
we have the function dropped while that removes all the elements
while a predicate holes show take while
keeps all the elements until the predicate is false
drop while drops the element and
until the predicate at turns falls
so you see here that an in this case it drops the value
and and said every during the ante LIJ there it just got
yeah which the the valley on top of the rest
good happy hacking
and make sure you do the exercises because
all the higher branches that we have seen here like take wild ride while map
filter
and you will have to implement them in terms of old R
a ride and so please make these exercises
and if you're not using haskell an say you're using Scala our job on
you can do the same thing these languages typically
also have fold our and maybe it's not called for
are sometimes is called fourth ride or sometimes just called fault
but this idea of having a higher r2 function
that captures the ass and shove rick urging over lest
is available and and the library
an that that supports lists
and for example in c-sharp it's not even cold fold our
it's cold San aggregate
so that comes from Chico lawyer aggregating over list
so don't be fooled by the name and the idea is what matters
and thank you so much and see you next week