logo

Itnig

Itnig es un ecosistema de startups, un fondo de inversión para proyectos en etapa inicial, un espacio de coworking y un medio de comunicación con el objetivo de construir y ayudar a otros emprendedores a crear negocios escalables. Nuestro objetivo es liderar negocios de alto crecimiento, y construir un ecosistema y una economía independientes donde nuestras startups y equipos puedan colaborar, fortalecerse y crecer más rápido. El podcast de Itnig es un podcast de negocios, tecnología y emprendimiento. Invitamos semanalmente a emprendedores y perfiles tecnológicos para hablar sobre sus startups de éxito. Siempre estamos buscando aprender y compartir conocimiento de las personas más interesantes del ecosistema. A través del fondo de inversión de Itnig, buscamos invertir en equipos con el talento y la ambición de crear negocios escalables con el potencial de cambiar mercados e industrias. Itnig es un ecosistema de startups, un fondo de inversión para proyectos en etapa inicial, un espacio de coworking y un medio de comunicación con el objetivo de construir y ayudar a otros emprendedores a crear negocios escalables. Nuestro objetivo es liderar negocios de alto crecimiento, y construir un ecosistema y una economía independientes donde nuestras startups y equipos puedan colaborar, fortalecerse y crecer más rápido. El podcast de Itnig es un podcast de negocios, tecnología y emprendimiento. Invitamos semanalmente a emprendedores y perfiles tecnológicos para hablar sobre sus startups de éxito. Siempre estamos buscando aprender y compartir conocimiento de las personas más interesantes del ecosistema. A través del fondo de inversión de Itnig, buscamos invertir en equipos con el talento y la ambición de crear negocios escalables con el potencial de cambiar mercados e industrias.

Transcribed podcasts: 697
Time transcribed: 26d 23h 57m 17s

This graph shows how many times the word ______ has been mentioned throughout the history of the program.

I'm Ruben and today I'm going to talk to you about functional programming, and the title
is quite misguided because the examples are in JavaScript, but functional programming,
or at least this presentation has nothing to do with JavaScript at all, yes, I need a language
to make some code, okay, so who am I? I'm the lead front-end developer at Lullabox, and
Lullabox, in case you don't know it's an online supermarket where you sure if you are not already,
you can make your, it's like a normal grocery, so you actually can make, I was saying you
can buy food, but you can actually buy everything, like food, things for the house, like we have
even, if I don't remember, but microwaves, so, but especially food. I think why you should believe
me is because I've been working with JavaScript since I started developing, so this is more than
three years ago, it's not too much, but if you are working a lot every day, there are quite
a thousand slides of code, and this, of all this time, the past year, I've been working
almost full time with functional programming paradigms, and this is important because there
is a lot of people, not only in Barcelona, but around the world, who's starting functional
programming now, right? It's important to have in, for all the paradigms, all the language,
some background, so you actually face the common problems that all the ecosystems have.
So yeah, we are going to talk about functional programming, and it's an introduction, I'm going
to try to start from the very beginning, because there are some things that change quite a lot,
comparing with other languages, and we will try to reach a point where you can actually
continue exploring functional programming by yourself, so let's go. This is, like, the
very basic for functional programming, and has very little to do with actually code, okay?
So this is the two pillars of functional programming, in case you don't know the terministic, well,
we are going to review in deep later, but the terministic is, you can think about a code
that, whatever you get, the sum input, you will get always the same output, right? So
like the signature or the values of the signature will always determine the output, and declarative
is just the opposite of imperative, which is the most common paradigm, especially with
objector in the programming, and it's just a way, it's a paradigm, actually, that you
code telling the language what you want to have, not how to have it, right? So, let's
go a review. Here it goes, and the terministic example, most of the examples are with ES6
in JavaScript, so this is, in case anyone doesn't know, this is a function, it's a narrow function,
which takes the argument and returns the, in this case, plus, plus x, so if we get this
number, we get the number plus 1. So what's the important of the terministic? As I told
you, given a particular input, you always get the same output, and this is a concept
that is super important, it's taken from calculus, and this enables programming as if it was equations.
This is something that we talk later, but this is called reference transparency, and
the important thing is that this will make all the code more simple, smaller, not smaller
in lines, but like in units of code, which in JavaScript will be functions, and in functional
languages, they are always functions, and therefore, by extension, composable, and we make it later,
we go in deep later. For the coders that are in the room, think about a state machine.
State machine is the final example for the terministic code, where you get always a lot
of inputs, and if the input is over the same, you get the same output. The opposite will
be something like a neural network, where you input the same data, one time, and again,
and again, and so on, and maybe you get different outputs, because you don't know the relationships
that are being made inside. So, important, this is avoiding side effects, spoilers, and
the other pillar is declarative. Instead of saying that here, by the way, we are just
getting those who feel this statement, which is the modulus of two should be one, so false.
Complex code, my bad. Whatever. The idea is that I'm not saying the code how to do the
things. I want pairs, right? So, I'm not actually going through the, give me this, and then check
if the value is bigger than, and then so on, blah, blah, blah, blah. It's like, no, I just
want this result. So, this idea here is maybe too simple, but you will say something like,
I want filter. Those where x are x, then filter. Those where x are x modulus two, and then I
want to map it, and then I want to, so you are making a smaller units of code. Here's
this important thing with declarative. So, what we're doing, we're all looking at purity,
we're looking at no side effects. What's purity? In case you don't know, pure function
is the function that is deterministic, so takes an input, returns always the same output,
but it also never relies on external sources. It never calls another function. This function,
if it's pure, it's not going to use maybe a flag or, you know, another scalar or a structure
that you have around or class or whatever. Actually, this is one of the, whoa, it's one
of the ways you get to ensure the deterministic, right? So, it's, you have, the determinism
helps you to have purity, but purity also helps you to have determinism. Don't worry.
And the side effects, I guess that all of you know what the side effects, but, yeah.
Ah, deterministic, deterministic and declarative, you mean? Whoops. Go back, doesn't go back.
I will go back, but it doesn't go. Don't worry. Deterministic and declarative, you mean?
So, the first one. This example, do you mean? So, the idea is that, let me continue, because
I have a better example. What can happen, right? So, we're going to review the opposite,
the errors. So, I was telling you about the side effects, which is important. Let's see,
but now I'm going to the question. Side effects are these situations that you don't expect
and you don't actually want it to happen, right? So, if I'm adding, I have a function
which adds two numbers, right? It's a sum, and I pass to the function two and two, I will
expect a four, but never a five or another number, right? So, if I get a five, there
will be a side effect. Here, it's the basic example, but maybe you have race conditions
or in distributed systems, whatever, right? You can have more complex problems. Difficult
to debug. So, side effects, here is like the first example you were asking for. So, this
is a, whoops, side effects. This is a non-deterministic code, because you cannot know what will be
the return of plus every time you execute it. At the first execution, it will return 1338,
but at the second execution, it will return 1339 and so on. So, this, you cannot warranty
at any point that you know the answer of the execution, because this code is not deterministic,
but it's also relying on an external source, right? So, this is the opposite as before.
En case you want to add a number, you will pass always x as a parameter, and so on, you
will be concatenating the function calls. Yeah, that's pretty important. And then the
imperative, as I told you before, the important, here we are making a few things, right? We
are relying on external sources and so on, but we also say how we want the information.
I want it in a right, and I want to push the resource, yada, yada, right? If I want more
things, I will add everything here in the block statement and so on. For me, at least,
the more important thing between imperative and declarative, both are okay, depending
on what you're doing, but the most important thing is that with declarative, you have a
more maintainable code, because it's less hard to read from about, right? It's more clear
in a quick view to understand what's doing. If you have a small function or a small unit
of code. So, the principles. Again, this is not actually binded to the code. Those are
in an abstract way, and all of them came from mathematics, especially category theory. So,
first-class citizens. What does it mean? All the languages have first-class citizens. Just
in the case of the functional programming, functions are first-class citizens. And a first-class
citizens is a unit of a structure, you can call it that, that can be operated as all the
others as colors. So, a color, right, is like an integer or a float or whatever, a string,
right? In JavaScript, function is a first-class citizen, because you can actually pass it as
arguments on other functions, a function can return another function, a function can be
a chain, a function can be operated as any other scholar. So, this is the first principle
that you need in a language in order to say that you are doing or you are working with
functional programming. This came from calculus, and the most important thing, like the abstract
for the first-class function, is that you can, as I told you, pass it as arguments and return
it as outputs, right? Because this enables higher-order functions, which will enable the
composibilidad I'm talking about. Here's a higher-order function, a higher-order function
is a function which intakes or outputs another function. That's a more basic example. Usually,
we call higher-order functions, two-dose functions, which actually return functions, right? It
depends on the language also. I told you, it's abstract thing. And here we have, instead
of having, this is query, right? This is a query function, because usually you may have
a poll function, right? And you pass two arguments, the two and the five. And here we are chaining
the function calls. So, query is basically a set of, or a chain of functions, which every
function takes just one argument and exposes to the next closure. And here, this will be
a closure and this will be the other closure, right? This closure can access exponent, but
this closure cannot access base, right? It's just a nested call, right? So, immutability
is the other important principle, because if you think about, I told you that the purity,
you cannot rely on external sources and so on. So, what happens when I want to change
the state or I want to actually change the value, right? So, what you do is return a
new one, right? Instead of changing n, in this case, we are constructing a new scalar
called n from n, right? This is a stupid example. We are just changing 3 for 11. But the idea
is that you are never changing the values, right? The values are immutable. Among all the things
related to performance, yada, that all the people is talking, you again are forcing a
proper maintainability and a code, which is less hard to reason about and so on. All the
benefits that we are talking about, the idea here is to remake instead of change values.
Okay. So, let's go a bit in deep. I guess that it's clear, everything, from now, since
now, I don't know. Okay. So, map reduce, funture and monads. I completely sure that you
actually listen these terms before, but there is a lot of people who explain it like in
a very scientific way and not like for coders, right? Well, maybe someone here is working
in the university or whatever. But important things, this comes from category theory, which
is the abstract thing. It's a field of mathematics, which study the numbers in groups. So, think
about the Venn diagram, right? The bubbles, which fill up one with the other. This is category
theory. We are just comparing sets of groups. Importante thing, map reduce. Nothing too complex,
right? This is the base for a lot of technologies nowadays. Like in JavaScript, we are like forcing
this kind of modifying the values, but also like big data, machine learning, they are relaying
a lot of the concept of map reduce, especially because the easy of think about performance
and composability, right? You can think about this like a pipe. So, I want to map, imagine
that here instead of saying the callback itself, it says, this will be multiplied by two, and
here it says sum everything. So, you can change things. You can say it so dot map and also
change the type to flow out and so, yada, yada, yada. So, if you have a big data pipeline
like for hype, this kind of thinking is super powerful because you can actually compose the
pipelines that you want to, okay? And think about the importance related with immutability,
right? You are not changing the data. You are just creating new values. So, the functor.
So, bear with me. A functor is an entity that defines a behavior, which given a morphism,
maps it onto a category and generates a new functor, right? Completely right. So, this
is because it comes from mathematics. Let's review it again. The definition is functor
is an entity. Df is the functor, right? Entity. Which defines a behavior. This is the behavior.
In this case, the behavior is a non associative array, blah, blah, blah, objects, right? In
this case, JavaScript is an array, yada, yada, right? Which given a, sorry, this is a morphism
and this is the behavior. My bad. Yes. Morphism, behavior. Behavior in this case is super cool.
Given some input, return the same, right? Super easy. And map it onto a new functor.
G is another functor, right? Why is a functor? Because you can actually make the same thing
again on G, right? This concatenation of things, it's infinite. You can actually be, you could
be doing map x to x, x to x, x to x forever, right? So, this is a functor. Easiest thing
to reason about the functor. Functor is some scalar, which exposes ways to iterate to its
own. And returns another functor. So, two things. You have a scalar, right? It can be
an array, for example, in JavaScript, which exposes ways, map, reduce, filter, so, right?
Which enables to you to iterate over them, over themselves, right? They are callbacks and
they are going, it's an iterative, right? And returns another functor. All the map filters,
the result is another functor. You can, again, filter map and that's where you can change
the goals, right? In the previous slide, here what's happening is that map is actually returning
another functor and then you are reducing the new functor. But between here and here,
you are not creating, but in an abstract way of thinking, you are creating another functor.
Yep. So, the monads. Monads is one of the things that I listen the most complex definitions
in my life. But, actually, a monad is a functor that you can flat map. And what does flat
map mean? Flat map is like a map where the mapper doesn't expect to have the value but
a functor. So, if we think in the most English way to reason about the flat map, right? From
flatten and the map. Flat map is something that you can do something like this, right?
This is a flat map which takes the scholar that you can iterate and then comes the mapper
and the mapper returns an array, which is not a scholar, which is a functor in JavaScript,
right? You can map this thing. And this is what doing the flat map. It's taking the functor
and then it's mapping it. It's a complex syntax in JavaScript, but you are basically, in this
case, like, calling the function map, okay, with this callback to its own and to the values
that the previous mapper is returning. This is complex by definition. But, basically, given,
I mean, if you see the input, you see what's doing the map, which is returning maps, right?
This will, with a normal map, this will return a matrix, right? This will return a series
of 7, 1, n of the array, 49, 7, n of the array, 91, n of the array. So, the flat map is mapping
also the functor which is returning the first mapper. This is a very basic implementation
of a monad, right? A monad would also take a value and handle properly. In a monad, theoretically,
you can pass, the turning of the functor can be any dip inside of the function called chains,
right? So, but the idea, right, again, and the important idea that you can explore is
that the monad is like a functor, but expects, the mapper expects another functor. A real
case scenario for this thing, it's super common. It's quite stupid, but it's super common.
So, you'll have mobiles and you'll have apps. And most of the apps have lists. And the list,
at some points, they have headers, right? Usually, what you have is a list of sections.
And the renders, what I'm actually doing is flat mapping the sections. So, at the first
item of every section, instead of returning the value, they return another functor, usually
an array, they flap up so they have the extra header. This is like a super common example.
And it's a very good example because if you don't use the monad, the flat map, you rely
into the imperative coding. And you're saying, oh, so, if it's the first, then go to the
previous array and add one position, which is super costly for the machine, and so on.
So, real cases scenarios for things that are quite abstract.
Like, in an imperative, yeah, yeah, it would be like, not only nesting the for loops or
wilds or whatever. Recursion can be functional. Usually, recursion is functional. Usually,
recursion is functional, and usually, recursion is not pure because you rely, you understand
the sources, but you can make recursion pure. But usually, it's functional and not pure.
Yeah, the alternative would be making for loops and not also going to how deep is this,
but checking the types, because it's a functor, execute the functor, if it's not, then return
the value, and so on. Benefits, okay.
So, we want to make functional things. Disclaimer, first of all, as I told you, I've been working
quite a lot with functional programming right now, and I release a few products full functional
to production, so it works, okay. Just in case you want to combine your CTO, she can call
me. Stateless. Stateless is like the straight away benefit, right, because if it's pure,
if you're unrelated on external sources, and so on, if it's deterministic, then you cannot
have an estate. And the main benefit of not having an estate is that it's really difficult
to make side effects. It's superdifficult, because you don't have, this is called the
impotent, right, in programming. It's like the last instruction that you get is the true,
and that's all, okay. So, the last statement that you get is the true, that would be the
idea. With a stateful code, the last statement that you get modifies the previous state,
and then you get a new state, right. So, this would be, and the code is quite easy, right,
it's like, just add one number, but always keep the value at the maximum, in this case
it's 10. So, if we say 8, and we pass the max, it says 9, if we say 10, we pass the
max, it says 10. This code is still deterministic and pure, right. We don't have the max like
a constant here, which is being read inside of the closure, but we are sending every code
the max value that we have. The opposite would be to have maybe max, so maybe the previous
value store somewhere in state and using this. Yeah, reference transparency, this is another
of the super abstract things, it came from metamathematics, in case anyone ever explored
the field. The idea is not something that you have to do, but it's like some sort of best
practices for functional programming. The most important thing is it promotes thinking about
the function as equations, right, and this means one very important thing, that you can
replace the values from the right to the left, and the final rule would be still true, like
an equation, right. So, you can, the property of substitution into a equation, you have it
also in functional programming, because it's deterministic and doesn't rely on the states.
Here we have, for example, the square, right, the sum 3, and then we have, forget the mean
for now, so if we concat everything, we get it, that's cool, reference transparency is
this thing, which is also really related to the maps, filters, it's not about writing
code as one liners, right, but expressing as equations. It enables an easy control flow,
as I told you before, you can pipe it, or you can compose it, so on, whatever you want
to, and the other thing that we get with reference transparency, it's called point free style,
is this mean function, point free style is a super fancy name to saying that you don't
have arguments, you expect whatever arguments, or what it's called in functional programming,
the arguments, right, which comes from nothing to whatever, any. Mean, in this case, I don't
know if you ever face this problem in JavaScript, that math mean, it's a factor function because
it doesn't accept an array, you have to pass it as arguments of the function, so it's like
fuck you mean, I'm going to use my own implementation, which takes whatever they want to pass, we could
be passing also here a flat mapeable array, for example, and it will work also. Again,
very with me, this is like best practices, nothing religious about, and then it comes,
the cool thing, composition, it's like, alongside with my tenability, for me, this is my opinion,
this is like the other super benefit of functional programming, this is called the Gorilla banana
problem, I want a banana, but I actually get, with object programming, I want a banana, but
to get the banana, I actually need a gorilla, and all the fucking jungle, and I don't want
neither the gorilla, neither the jungle, so in this case, you have, for example, we have
two functions, full legs, and hustlers, this is doing just basically the first function
adds an attribute legs, value four, for a given object, and has lasers, the same lasers
true for a given object, so we have sounds like a cat, because it means, so if sounds
like a cat, we add it for legs, we have a cat, cool thing, right, but we also want
a fancy cat, so if to the cat, we add lasers, we have the catinator 4,000, now this is
a fun example, but the important thing would be, imagine how is this in the classical object
or in the programming, right, we have like the animal class, and then it extends, you
know, vertebrates, which extends domestic animals, and then you have the cat, right,
we are here, and then you have another class, which is machinery, which extends robots,
which extends combat robots, and extend with lasers, and then you say, I want a cat with
lasers, well, it's going to be a pain in the ass to merge everything, so with functional
programming, what you get is just composition over the inheritance, right, there are a lot
of studies, which actually merge object oriented programming with composition, in javascript,
for example, you can do it, because javascript is not actually object oriented, but prototype
oriented, but in classical object oriented languages, you cannot compose the different
inherit classes, composition, it's modular, it's cool, and then we get testability, which
is a thing that is not very popular in the development world, but should be more popular,
why it's very easy to test the functional programming, basically because the determination,
right, pure function is stable by itself, because you know 100% true, that for a given input,
it will return the same output again, but also it goes a step further, because you can
test things like the progtypes, or you can embrace the randomness, right, I know, because
the determinism, I know that function, given an input will return an output, so I can,
for example, on my test suite, not test always the same steps, the same mocks, I can passing
around, I don't know, for the sum, I can say, math random, then math random, and I expect
to return an integer, bigger than the first number, that's true, that's the condition,
that's true, you can do it, you don't have side effects, so this is pro-testing, and
it's a step beyond, right, this is another thing that it's very easy to do, this is
flow, it's from Facebook, and it's for static typing, so we say I have a p function, which
takes an integer, another integer, and returns an integer, so I would expect to throw an
error if I pass it a number and then a string, right, if you put this in a whole system,
what you get is, with the progtypes, right, or concatenating functions, I can catch, but
returns, but errors, functions that are not working okay, side effects, race conditions,
because the progtypes, they are not going to be okay, and this is super useful, this
is a problem that I was fixing today, for example, a toolbox, race condition writing
into a database, one of the super problems, right, and we catch it because, a thing like
that, because it should be returning a number from 1 to 0, and it was a negative number,
so jump, jump, jump, all the lights turn on, and that's basically it, I hope you, oops,
you should be moving, okay, in case you have more doubts, you can ask now, I hope you like
it, don't be shy, you have a question, you, okay, so enjoy the beers, and thank you for
your time, y nos vemos en el próximo vídeo, nos vemos en el próximo vídeo, ¡hasta la próxima!