hello in this video series I will teach
you how to do functional programming in
JavaScript in this first one I will
begin by telling you about one of the
most important concepts in functional
programming and that is higher-order
functions I will talk about what
higher-order functions are and I will
then move on to code and show you how to
use them but before I do that I want to
tell you why you should learn functional
programming in case you don’t know me
i’m mateus pepper you once on mpj for
short I have been a full-time programmer
for about 10 years I work for Absolut
Vodka blackberry and I currently work as
a front-end developer for Spotify I love
functional programming functional
programming has made programming so much
more fun to me if you have been
programming for a while but haven’t yet
tried your hand at functional
programming before this video is going
to change your life learning to program
functionally is gonna make you a so much
better programmer you will feel much
more secure with the quality of your
work and it will be a lot better and you
will be more sought after you will be
able to write your programs with less
bugs in less time your code will have
less bugs because your code will be
easier to reason about and you will be
able to write it in less time because
you will be able to reuse more of your
code higher-order functions really like
saying that it’s is like saying
quintessential it just makes you feel
smart
so one so let me tell you about
higher-order functions because that is
going to make me feel super smart in
JavaScript and open in all functional
programming languages functions are
let me show you what I mean by that here
we will see the basic function in its
Maho so you will recognize this even if
you don’t know JavaScript because all
non insane programming languages have
functions but not all programming
languages can do this create an
anonymous function and assign it to a
variable just like any other value we
can pass it around like this and you
know yes this will be 90 again in
functional programming languages
functions are values just like strings
or numbers functions can be assigned to
variables or pass into other functions
higher-order functions but what are
higher-order functions good for well
composition the fact that we can take
one function and put it into another
function allows us to compose a lot of
small functions into bigger functions
bread that’s all a lot of theory let’s
look at how to actually use one of these
things probably the most basic and
useful higher-order function is filter
it’s really simple filter is a function
on the array that accepts another
function as its argument which it will
use to return a new filtered version of
the array here we see a list of animals
we have fluffykins which is a rabbit we
have Hamilton who is a dog
and we have Ursula who is a cat
we want to filter out the dogs because
we’re writing enterprise-grade software
here I am going to show you how to do
this using filter but before I do I want
to remind you how you do this with a
normal for loop by M so this is just a
ultra normal for loop it creates an
array to hold the dogs it iterates over
the animals animals array and if the
species of the the animal it being
iterated is equal to dog it will push
that dog onto the animals array now
let’s rewrite this using the filter
function I’m gonna comment out the for
loop and keep it on so that you can see
it for comparison purposes and here goes
so let’s have a look at this filter
accepts one argument another function
functions like this functions that you
send into other functions are called
callback functions because the host
function will call back to them filter
will loop through each item in the array
and for each item it’s going to pass it
into the callback function and when it
does it will expect the callback
function to return either true or false
to tell filter whether or not this item
should be in the new array and after
it’s done it will return the new
filtered array and that will be dogs
remember how I said that you’ll write
software faster when you’re doing
functional programming notice here that
the example that uses filter is a lot
less code than the than the for loop and
that is not because the syntax is
cosmetically shorter or anything it’s
because we’re actually writing less code
less logic the reason that we need less
logic is that when we write our software
in small simple functions they compose
together which allows us to reuse
functions all over the place so in the
filter example we are just writing one
line of logic really which is this one
the line that determines what animals
goes into the array and the rest of the
program creating a new array stuffing
animals in it that the for loop has to
implement here and here that is actually
handled inside the the filter function
it’s handled for us so we don’t need to
write it there for the filter function
the filter example is
we just wondered an object the callback
function and the filter function just
slopped into each other they are
composable I’d really like to stress how
well these simple functions compose
let’s break out the callback into a
separate variable so notice here that is
dog is just a function that checks that
an object is a dog it doesn’t really
have anything to do with the filtering
at all it’s completely decoupled from it
so we can use it for four other things
for instance if we wanted the animals
that aren’t dogs we could pass it to
reject which is another a higher-order
function on the on the array object that
does the inverse of filter so it could
be animals don’t reject dog and this
will give us an array of animals that
are not so compare this to the for loop
you’ll see that we very cleanly broken
the problem up into two completely
separate problems the problem of
determining if an animal is a is a dog
or not and the problem of creating an
array and stuffing the objects into them
and this way we’ll we’re allowed to
think and reason and debug these
problems separately and that is a lot
easier than when the two solutions are
all jumbled together like like in the
for loop
today I’ve talked about how functional
programming means that you can write
software with less bugs in less time in
JavaScript functions are values and you
can exploit this by dividing your code
into
all simple functions and composing them
together using higher-order functions as
an example I showed you the higher-order
function filter and how it compares to
normal form we’re just scratching the
surface here learning functional
programming is going to introduce you to
a whole new world that you didn’t know
existed there are many useful
higher-order functions besides filter
next time I will talk about two of them
map and reduce do not miss it hit
subscribe and follow me on Twitter at MP Jamie stay curious see you next time