this is fun fun function a Monday
morning show where we try to come more
excited and confident about programming
by exploring old wisdom wild ideas
having fun a lot of people have
requested another functional programming
video and I figured that I try my hand
at one this week and I would like to
talk about funk doors functors is
actually something that you are probably
already using in your everyday
programming map and filter our functors
for instance today we are going to
explore what it is that makes a map and
filter part of the functor family and
we’re also going to write a little fun
toolbar oh but before we do that we’re
going to do some programming without
founders to get a sense of the problem
that functors are here to solve behold
the enterprise-grade function plus one
this is a bit of a stupid function but I
am using a super simple example here to
not distract us from the concept that
we’re going to explore imagine that in
the real world plus one is doing
something much more complicated the
function plus one just adds one to
whatever value you pass to it so here we
pass it a three and we get a four
however if we pass in an array of
numbers say 3 & 4 like this things will
of course break but we would like it to
work if we pass in array 3 4 we would
like it to return array 4 5 so we write
something like this there’s no magic
here just a lot of code if value is an
array we iterate over the array and
create a new array adding 1 to every
number pause if you want to have a look
but it’s not that important and this
works now we get a 4/5 if we input 3 for
next up let’s say that we discover that
we need +1 to work on strings in
addition to erase and single numbers
like this if we pass it the string ABC
it should output BCD so
we add another bunch of code to handle
the screen case you can pause the video
if you’re interested in what the code
does but it’s really just incrementing
the chars it’s not too interesting the
important thing to note here is that
it’s not pretty annoying that a function
does a whole lot more than just plus it
but it works it’s big but it works but
let’s say that we also need to write a
minus one function then we would have to
duplicate this whole shebang between the
two functions or I guess that plus and
minus could be generalized into and I
don’t know arithmetic function but when
we add square division floor and every
other math function on the sky we would
end up with a huge generic math function
instead it feels like there is a way to
generalize this upper part here the
thing that does literation of the string
and the array respectively it doesn’t
really feel like it belongs in the plus
function anyway right and this is where
functors coming for the first part the
array javascript actually has a built-in
functor that you are probably already
familiar with map UNK this is all there
is to plussing arrays are you getting a
sense of how functors on making things
easier map should be familiar to you by
the way if it is not go check out my
video on that you if you don’t know map
the rest of this video will not make
sense as I mentioned before if you’ve
been programming JavaScript for a while
you’ve probably been using factors
without knowing that they are part of
this family called factors in a minute
we’re going to talk about what it is
that makes functor qualify as a part of
the functor family but first I want to
show you another example of a func tour
this time we are going to write it
ourselves are using array map we’ve
sorted out the array plus encase but
what about the other case plussing
strings going from ABC to BC
we will solve this by making a string
factor like this this time I really want
you to pause the video and take this
code in for a little bit before
continuing what you see in the string
functor is almost the same logic from
the big version of the plus 1 function
that we saw before nothing strange going
on here really we pass in a string ABC
into the string function and we’re
getting BCD out screen func door takes a
value and the function FN as you see
value in this case is ABC and F n refers
to the plus 1 function string functor
goes on to split the string into an
array called chars it then maps over the
array of chars the char parameter that
is being passed to the map callback is
each character in the form of a string
with the length of one so it’s just you
know a string that is B we convert that
character to a number and we pass that
into FM and again FN is going to be plus
1 in this execution we then take the
return value of F n which is a number
and convert that number to a character
map we’ll return an array of these
incremented characters and we then join
them together using join because string
functor now does this unpacking process
for us we can all let plus 1 and minus 1
do their thing
plus 1 and minus 1 no longer need all
that iteration logic inside of them
because that work is handled by the
functors so string functor and math are
both functors func doors or functions
that take a value and a function the
value might be an array in the case of
map or a string in the case of the
string factor the functor is responsible
for unwrapping the individual values of
the value in passed in and then passes
each of those into the function and then
it takes the processed values and
creates a new structure
for those values that it returns another
example of the functor would be array
dot filter for instance array dot for
each is similar but it’s not a func door
it does take a value and it does take a
function and it does unwrap the value
into its component and passes it to the
function but it doesn’t return anything
in a structured form and that is what
makes it not a function let’s summarize
in the context of JavaScript a functor
is a function that when given a value
and a function will unwrap the value
into its individual parts feed those
parts into the function that it has been
given and take the return values and
return them in a structured form and
that is a function I would really like
your feedback on this video did this
give you anything
I picked functors for this episode
because I I haven’t done a functional
video in a long time and there’s been a
lot of requests for that and especially
the delusive munna have been among those
requests and found torus is one step on
the way to understanding your lands but
the reason that I’ve been dragging my
feet with making functional programming
videos is that from this point on
functors moving on it gets very nice and
maddening the definition of functors
that i’ve given you here today is
correct in the context of JavaScript I
think but in a mathematical context it’s
actually much more abstract and in that
sense I’m not sure that I’m giving you
exactly the right thing because yeah it
gets messy functors is a concept from a
branch of mathematics cool category
theory and I’m not entirely sure how to
proceed with the teaching these days I
mean should we start learning category
theory on the other hand a lot of people
say that you don’t need to understand
category theory in order to understand
or maybe monads is something that we
should stay away from completely maybe I
should start doing a series on Haskell I
think I really need your feedback on
this one let me know your thoughts in
the comments as usual the next episode
will be out early Monday morning GMT
time don’t miss out subscribe and until next Monday stay curious