good Monday morning this is fun fun
function a Monday morning show where we
try to become more excited and confident
about programming by exploring all
wisdom wild ideas and having fun in
order for this video to make sense you
need to have watched my previous video
on funk tours because I make a lot of
corrections to that video in this video
you also need to be familiar with the
concepts of map and promises if you’re
feeling rusty on those links to videos
about them is in the description
I would like to geek out on the
definition of functors for one more
video because the last video got a very
big response I didn’t expect to be
people people to be so interested in
founders to be honest but it also got a
lot of good Corrections from some
wonderful people in the functional
programming community especially doctor
boolean on twitter who deserves an extra
shout-out for helping me you should
really follow him if you’re interested
in functional programming today I’m
going to make a stab at a more accurate
definition than the one I made in the
previous video in the last video I said
that founders are functions like map
that is not correct functors
or objects that have a map method so for
example it is array that is the functor
because it has an implementation of map
it’s not map that is the founder it is
the object that is implementing that
that is the founder the most common
example of a functor in the world is the
JavaScript array other objects that are
very often implemented as functors
or promises streams or trees for example
so you can think of functors as some
sort of the generic equivalent of array
having a map method filters are since in
general not just arrays that we can map
in the last video I mentioned that a
raid of filter is a functor
that is completely wrong filter has
nothing to
with functors not fit all its objects
that implement map that we are
interested in and the reason I got
confused about this is that the
community is a bit confused there are a
lot of blog posts describing functions
functions and referring to filter as a
functor
and the reason that the community is
confused is that in category theory the
subset of math that relates to functors
a functor is actually a function and to
make matters even more confusing the
word functor is often used in computer
science to be completely synonymous with
higher-order function through whom
however hope is not lost for a common
definition 95% of the functional
programming community including Haskell
closure f-sharp Scala settled on the
definition that objects that implement
map or founders add to keep us sane that
is the definition that we are going with
in this video so what exactly do we mean
when we say that the object should
implement map I mean we cannot just
implement map and have it return and
call it a func door there needs to be
some garden rules if I had a bigger
beard huh
I would now refer to the functor laws in
Haskell yeah but the functor laws are
great and all but I think that they
sacrifice understandability in order to
be correct and terse it’s good to check
them out eventually but you know the
HTML spec is not a good place to start
learning web development either instead
let’s have a look at the bunker that we
all know and love array and look at what
it is that makes it qualify as a functor
so here we have an array of dragon
objects and we use map to just get the
names of the dragons again I would like
to stress here that map itself is not
what we refer to with the word
functor it is a ray that is a functor
because it has a madman we’re going to
explore three crucial things that Ray
map does in order to qualify as a
functor but before I do that I must show
let’s let’s talk about three crucial
things that array dot map does in order
to qualify array as a functor one
transformation of contents the basic
idea of the functor is that the map
functions takes the contents of the
function and uses the callback pass to
map to transform the contents into
something else this this function right
here is the transformation function it
transforms the dragon object into just a
dragon named string and this
transformation that’s the first barrier
of entry that array passes in order to
be called a function some people uh and
these are the same some people that like
homework or to do taxes like to point
out that the transformation callback
that we pass to array dot map actually
gets two arguments the object and the
array index which kind of stretches the
definition of a functor but I think that
there is a point where a secret Club
becomes so secret that it has members so
let’s just give a rate of map some slack
here promises are often implemented as
functors the promise is built into Atma
script six do not have a map function
but most promise libraries do so for
example if you use Bluebird you can do
this if you look at this code we create
a promise that yields a dragon object
after after two seconds and
when we have it we map the name and then
write that name to the console and note
that the map callback is exactly the
same as the previous example in that
example we saw the array functors
shielding the transformation callback
from the complex reality that there are
more dragons than one while the promised
factor instead protects the
transformation callback from the complex
reality that there isn’t any dragon
until later I like to make a small
detour here and remind you that this is
a really good example of what functional
programming is all about breaking
problems into teeny-tiny super simple
functions and then composing those
functions together using super cool glue
like func doors functional programming
allows us to do this create a solution
separately for iterating arrays are
waiting for objects until later and
actually extracting the name and once
we’ve created those solution and have
them separately we can easily compose
them together and since they’re separate
Lego blocks we can reuse these things
all over our app so we don’t clutter
them with the waiting logic or array
iteration logic that’s sold now in one
place and this allows us to reuse way
more code than we would generally be
able to in an object-oriented world to
maintain structure the second thing that
array dot map does in order to qualify
array for the title of functor is that
it maintains structure if you call map
on an array that is three items long you
will get out an array that is three
items law array map doesn’t change the
length of the array it doesn’t return no
and it doesn’t return anything other
than an array with the same length like
you see in the example we transform the
individual values contained in the array
and we even change their types actually
but map cannot alter the structure of
the array itself and this is why this
string functor in the
example was wrong it does get the
intuitive spirit of functors right so if
none example made you get functors then
don’t worry you don’t need to erase that
part of your brain but string doesn’t
strictly work as a functor
even if we give it a map method because
functors need to be able to contain any
type punkers are meant to be generic
containers for anything like array or
promise or stream or tree string doesn’t
work as a functor because it means that
we cannot maintain structure let’s say
that a transformation callback return
false and try to shove that into a
string ABC like we just a it doesn’t
compute
a string with a map method welded onto
it gets the spirit of the function right
but it needs to be a container for any
object in order to be called a functor
and string isn’t three return a new funk
tour the third and final thing that
array map does in order to be functor
material is that the value that map
returns must be another functor of the
same type it’s because of this strain
that we can chain map calls like this
here we have the same array of dragons
but after we extract the names we also
get the length of each name because the
first map function returns a functor we
can keep calling map on it you can do
this map map map map map chaining with
promises or streams or any other
functors
it’s pretty cool in summary a functor is
an object that has a map method arrays
in JavaScript implement map and are
therefore functors
promises streets and trees often
implement map in functional programming
languages and when they do they too are
considered territories the map method of
the founder takes the functor contents
and transforms them using the
transformation callback that is passed
to man na then returns a new funk or of
the same type which
Taine’s the transform values today has
been a little dry and a little
theoretical I know we have explored the
definition of a functor even more and we
have looked at the most common factor
array but in the next episode we are
going to have a look at the coolest and
most underused function
streets as usual that episode will be
released on Monday o 800 GMT do not miss
it put it in your calendar until next Monday morning stay curious