Functional programming in Kotlin

Trailer for the talk

Here's a sneak peek of the talk: A live-coding talk on Kotlin functional programming suitable for all levels

Want to see the whole talk?Watch the full recording here

About the talk

Whether you are just getting started with Kotlin or want to discover the full potential of functional programming, this live-coding talk is for you! We will dive into Kotlin and functional programming with coding sessions on extension functions, lamdas, anonymous functions, functional types, trailing lambdas, and more.

This talk will cover

  • What functional programming is
  • Functional programming with Kotlin
  • How to increase your productivity with functional programming

We’ll use Zoom meeting to hold and record the event. Register to the event to access recordings afterwards.

Mobile Development

About the speaker

Raymond Arteaga Sanchez

Lecturer and Kotlin popularizer, Raymond started his journey as a full stack developer. 5 years later, he became an Android developer. A Software Engineer at Pedidos Ya, he is devoted to helping developers succeed in the world of mobile development.

Transcript

Raymond: So the title of this talk is Functional programming with Kotlin. I just want to know how many of you know Kotlin beforehand, how many of you have used it on your projects? If it's the case or if this is the first time you heard about Kotlin, please let us know in the chat.

Raymond: So I'm a software engineer at Pedidos Ya. I've been an Android developer for about seven years before that I was a full stack developer, mainly developing web applications. My name is Raymond Arteaga, I told you before. Today we will be covering this first question first. What is functional programming? Second, what is a functional programming language? Then, why we can say that Kotlin is a functional programming language? At the end, we will be answering questions or any dot you may have during the talk.

Raymond: So the first section of this talk is called what is a functional programming. What is it? I'm sure you all have heard about functional programming, but some of you might be asking what it really is. So the answer to what is functional programming will be functional programming is programming with functions. As you can see the meme there, you may think, well, that's my brain. Of course, that's functional programming is for running with functions, but well, we might deep dive on this concept of functional programming and try to discover what it is to see that I just want you to see this smart function that we have here. It's called the something. Just give it a couple of seconds and try to figure out how, what this function specifically does. Can you please, in a matter of seconds, determine what this function does.

Raymond: It will probably take you more than a couple of seconds right. Now, please try to identify what this other function does. If at least you have idea, please raise your hand.

Raymond: Okay. This function obviously is written in a more clear way than the previous one. Even if you don't know what a map is, you will probably identify the core parts of this function and will rapidly know what this function does. You will know that this part of his function will filter this element. This next part, we'll group it, using a key or identifier. And this one, we'll create a map of that elements list. So these both functions does exactly the same thing, but this one is really in a more consistent and readable way. Why do we say that this is functional programming? Well, if you see here, we are using a function, but all the code is written there. And when we read it, we need to keep in mind every piece of this code. So we know what this function is doing. We need to remember what this variable contains. We need to remember in part of the cycle we are, we need to remember with our efforts, what these results list is having, if we are adding items or not. So it gets tricky to understand what this function does. So when we read this other function is way more clear for us.

Raymond: Okay. Just move it to the side. Can you all see it now?

Raymond: Okay, great. I was not aware of that. So the screen so grey. When we read this function is way more easier to us to understand what it does. Yes, because it's reading in a more concise and readable, readable way for us. So we are not machines. We are human beings. And when we write code in product to keep in mind that other developers like us will be reading our code, not only the machine.

Raymond: So that's why functional programming is a huge asset for us as developers. Functional programming is, as you can see hide the logic. That you can see here. And this function behind functional names that are self-explanatory like this ones filter, group by, and map. So the logic is the same, but it's hiding behind this function names are easily readable by us. And we can know just by reading the function body, what it does. So that's why a functional problem is good for us. It just using functions, but in our readable way, cascading for the functions. So we can keep in our minds in a more easier way, what a function does.

Raymond: Yes. Let me know if you can continue seeing the presentation.

Raymond: Okay. Great. So what is your functional language then? It depends on who you ask. If there's any hostile or F sharp developers here, you will probably say that your language is the only functional language that is there, but the general agreement is that language can be considered as a functional parameter language when it complies with these items that listed here. So when a linguist treats functions as first-class citizens, it can be considered as a functional programming language. When functions can be passed as an argument, when functions can be returned from a function or when functions can be assigned to a variable. So why can't we say that Kotlin is programming language because it complies with all the previous item that we just listed.

**Raymond: **So now let's dive deep. Let's deep dive into what is functional programming with coding and the features that Kotlin has to offer to us when we are talking about functiona programmingl. So in Kotlin what's a function is pretty simple. If we want to define the function, we just write the keyword fun that if it comes to, to the case in this scenario, then the name of the function parentheses, and the value of the function. So these might be the way to define a function in column. So it's pretty easy. If you can see if our function assets, any parameter, we type the parameter names on types inside the parenthesis.

Raymond: So let's say we can take an integer, if we are going to return integer. We type it this way and then return for example, number two by two. So this is a pretty basic function now in Kotlin, it is important to know that a function can return at least two integeresting types that will be this one. Here, you can see that we are not specifying any return type, but behind the curtain, we can say that this function is returning on units. So a unit is something like devoid in C plus plus, except it's not the same. So when we inspect this code, we see that unit is an object. In Kotlin, we can have objects and classes, the objects in Kotlin are kind of the objects in JS. So we can normally per purchase in a single object. And that's a good feature on Kotlin to use objects. So if a function is not programmed to return a value, then we just use unit.

Raymond: Now, another integeresting type that a function can return in Kotlin will be nothing. You would say, what is this? Well, nothing is precisely intended to use when a function will not return or will not complete its execution in a forward manner. For example, we can throw an exception here. In this case, this function will never succeed so we can declare it as a returning nothing. And, the IDE will issue a warning whenever we try to use this function and write code after that. For example, if we do this, if you use the function, do something. And after that, we try to write anymore codes. We will see that this code is arrestable, because we declared that this function returns nothing. So, in Kotlin, we can have units, we can have nothing on, and we can have function for returning any. Any will be something like, the object in Java. So it's the base class for every object in Kotlin is called. So, this is how you declare or define a function in Kotlin.

Raymond: Now let's see. What can we say that Kotlin is a functional link. How are these functions considered first-class citizens in Kotlin. First of all we say that language can be considered as a functional language when we can assign functions to variables.

Raymond: How we can do that in Kotlin. It's simple. We just write the keyword will used to define a value, the name, start function and we can just use the column notation, and here it is. Now some function will be start in this value and if we happen to use this function. Okay, I see a hand raised. Let me see if I can see it here.

Raymond: Okay. Well, if we happen to want to use, okay. As I see missing here to use the functional stored in this variable, we just use it this way. And we bought the number that we want to enter as a barometer on this function. So we will pass the two and we now can use the function, start in this, start function variable, just to be more concise Kotlin doesn't impose the necessity to use the invoke and we can just do it this way.

Raymond: So here we can see that Kotlin complies with this item of being possible to store functions inside any variable we want. So this function has a type, as you can see it consumes an integer and returns an integer. So the type of this function will be integer and returns an integer. This will be the type of this function. If we happen to have another function that for example, takes two parameters like sum a and b. And we try to assign this function to this same variable. We will see that this is an error, because we are requiring there's a function that takes an integer or returning to an integer. And we are trying to store a function that takes two integerger and returns one. So there's type safety when we are talking about functions in Kotlin.

Raymond: So that's great for Kotlin. The third item that we had on our list is, I mean, we are by the second, right? Okay, is that it can be return from a function. So as we were telling before, we can have a function. Okay. these functions will be returning an integer. Yes this. So this function right now returns another function, a function that accepts an integer and returns an integer. We can, for example, do it this way by creating a lambda.

Raymond: So these two functions. Okay. I'm reading a question here. Is there a use case used or functional function is already. Well let's, let's dive deep into it. Let's for example, suppose that we are creating a calculator. Yes. Let's just answer that question that we got on the shot. Let's create a calculator on when the user presses a key, for example the user presses a key the divide operator on its keyboard and we are having a character here, make sure that will be operation will be sharp. Let's suppose that the user pressed the divide character on its keyboard. So we might here inside create a one expression operation. If they user for example presses the divide, we can return this. Let's take two part values instead.

Raymond: Okay. I mean, we will be talking about exceptions afterwards.

Raymond: Okay, we're going to do it just like this. If the user, for example press the multiply keyboard, we are going to perform this operation. So we got here on our main function intercept the key that the user pressed, or we can just get the operation with the key that the user pressed that might be stored on the arguments or something like that.

Raymond: Let's assume the user just press the multiply option. And we can pass the other numbers that we got. For example, from the arguments again, let's take two. So if the parameters that they use are introduced on the arguments of the program is this symbol. Then the get operation will return this function, otherwise it will return this function if this happens to be the character that they use introduced only parameters. Or else, this will throw a not found operation. So this might be an use case. I mean, it's pretty basic, but you will probably be able to find more use cases for this specific feature of coding, of returning multiple functions, using the sa,e signature.

Raymond: So let's see how much time we have. Okay, great. We are on time, so great. Okay. Maybe when you want to pass that stuff, function or function. Okay. That's another use case. So let's go back to where we were before, where we're talking about being return from a function. Okay. Assigned to a variable and being passed as an argument.

Raymond: We already covered the three options. So here the function is stored in a variable here. This is a return from a function. We gone for example, let's just clear this up. Let’s have our duplicate function.

Raymond: I think I have these functions. Now we have a defined, pretty simple function here, as you can see, it just takes an integer and it returns the same number multiplied by two. Now let's say that we want to pass this function to another function. How we do that.

Raymond: Your order function. That's why, how we call order. That's how we call in Kotlin functions that take another functions as parameters. Let's put this operation again. Just enter the signature of the function we are expecting. So it takes an integer and it will return another integer. Okay, great. okay. It's okay.

Raymond: So if we want to pass a function to these other functions, it's pretty simple. We just call function but by its name or the function in this case, we can pass inside the parentheses, the name of the function we want to pass as a parameter. Just using the two columns annotations before. So probably that would be it. And then when we execute this function, no, we are not returning anything yet. So let's add that. So this function will not return an integer. what we are going to do inside this function is to execute the operation that we got as a parameter. So we are going to return operation, we are going to invoke it. It will take an integer, so, okay, great. This is actually not a good example of this.

Raymond: So, okay. Let's move to two here because I'm going to complicate your lives. I'm about to complicate your life with this. Let's use this example that I got prepared instead. So let's control set here.

Raymond: Just erase it. Okay, let's use it here. So, okay. We have, here, inside this function, that takes the same function we had before as a parameter. So we can do it in many ways. For example, we can use just execute operation inside the parenthesis we can call or use this function or this one as a parameter, we just use two columns, executes, suspend.

Raymond: Duplicate. Okay, great. So we got to call this function, passing this function that we have here, and when we execute this, we will have let's run the program. Yes. The circle we will have. So we have an arrow here, all that. Well, now I prepare a long sample. I will not have time to go over it all, so, okay. I don't, I don't know if these have even happened to us too. But I'm probably sure that you're all developers and all of you have been in this position, suffering from the computer's rage upon you. So he will have a duplicated number. In this case, we pass to us a parameter. The operation that we pass on the parameter operation was duplicate.

Raymond: So. To duplicate will be for happens all the time to me. Yes. That happened. That happens. Okay. But that's fine. It happens. Okay, great. And now with we pass, instead of the triplicates function, we will see that instead of four, we have six because we pass this function as a performer. Now what happened if we want to pass another operation instead of functions that we have already defined when in Kotlin, we can also pass Lambdas, I showed you before to perform any kind of operation, we can have the number will be type integer. And we just are going to, to quadruplicate the, the value in this case will be number, multiplied by four. Okay. Here we have another way to pass functions as a parameter using lambdas, but in Kotlin, we can do it in a more expressive way.

Raymond: So in Kotlin, when the last value of, or the last parameter of a function is another function, we can write that function or lambdas after the parentheses. So this will be the case. Now it looks prettier. And as we don't have any parameters inside the parentheses in this function, we just are expecting a single parameter. We can also erase the parentheses. But it can get even better. When we have single parameter lambdas, we can only the number of. yes, the type inference of Kotlin will tell us that it's an integer and it will be automatically named as if it's something like ruby, I guess. So you might or might not be related to this concept of automatic air naming of variables inside the specific scopes.

Raymond: So we can pass the lambdas after the function. In this case. And it looks pretty awesome. So, okay. Let's pass to the next thing we can do in Kotlin with functions, which are extension functions. Here. We have a list of elements, as you can see, we have only three strings, red, green, and blue. Let's say we want to create a function that allows us to pick a random element or item inside that list. What can we do? In all languages, we might do something like this. We create a function that takes the list as a parameter, realizes all the computation and just return he number that we want, the random element inside that list. So this is easy, but what happened is some other developer, let's say we are working on a big project and some other developer needs to do these exact same thing.

Raymond: What can happen is that that developer will not know that we created this function here. If it's not documented, or even if it is documented that the other developer will need to go to check if that function, is it fine. That in practice, doesn't work. Developers don't look for pre-existing functions if they're not easily accessible. So that creates the risk to have duplicated code across our code base, which is pretty bad for us and coming back also our productivity, because we may be missing things that already are done. So we developers just duplicate this function and do it on it's file.

Raymond: So we don't want that. What Kotlin offers to give a solution to this problem is extension functions. What are extension functions? Well, this function that we have here can be defined us this other way. We are trying to apply this extension function to a list of strings. So we start typing. Yes that, what are we trying to extend? Yes, pick random item. Okay, great. This was already fine.

Raymond: This was already defined, it's two times. Oh, it's here too. Just erase it. It's okay. I understand. Okay. This can be written in this other style that is more relatable for us with we just copied the inside code of this previous function. We can use this, that will always point to this element we are just trying to extend.

Raymond: Okay. This function will return a string. Okay, great. Now these two functions does exactly the same thing, but what's the benefit of using extension functions that when a developer tries to access these lists of elements automatically when it writes a dot or types it out after the name of the collection. He will get the name of that extension function right away. So he will know the capabilities of your program, or if it's implemented somewhere else, we of course have to use and names that reflect what the function does, because if we use names that absolutely have nothing to do with what we are trying to achieve, then the earth will be misled. But if we use names that reflect where we are doing there will be available for everyone that wants to use all the things that we have done. For example, for processing this list of strings. We can just get rid of this. Now, whenever we try to process a list of strings and we type it out after it, we will get all the extension functions listed as if they were the members of this list all strings. So that's a nice part of Kotlin too

Raymond: There are other things to cover, but I think we will not have time today to call it all. Just, the last thing I have to share with you is for example, of these kinds of functions that accept more than one parameters or exactly two parameters. Oh, I had the sample over here and I don't find it.

Raymond: Let's just try it quickly. If we have a function that accepts exactly two parameters, for example.

Raymond: If we had this, we can call this in two ways. This goes to the main function again. So we can do some one on two. This is perfectly okay, but we have another way of typing this in a more concise or readable way. We can just use this name for a function. This is called a fixed function.

Raymond: It's a keyword. Okay. Okay. Yes. It's actually reading like this. These normally okay. At this notation and we can start this in another variable. So now we see that this function can be used between two values and we can read it in a more, in a better way, let's say. I mean, it's an option you're having. Kotlin is good to know. Another thing is that you can just get rid of all these boilerplate if you are writing just really small functions. These aren't Kotlin willing for the type that you are turning from the right side of the expression.

Raymond: So there were also many things to cover, but I think that the time is running for us. Yes, make a quick QA session. If you have any questions, just type it on the chat. And we will try to explain it in the minutes that are left to the talk. Maybe we'll create another talk in the future to the remaining topics for Kotlin, because it's a huge world when we are talking about functional programming with Kotlin. So if you have any questions, please type it on the chat and let’s cover it.

Raymond: Okay. Well, I think there are no questions. So in lots it was pretty well covered. Understood. Okay. Great.

Raymond: So I will be sharing on the event page. I will be sharing some links, containing, all the code that we look at today. I will be sharing also some documentation links that you can use to original knowledge about functional programming and specifically Kotlin. So hope to see you all soon. Let's wrap it up.

Raymond: I wish you all a good night. I had to know what time is in your country, but wish you all the best. So let's wrap it up. Let's throw a quick message on the chat. So y'all know and follow along with the rest of the information. So with that link, you can access the event page of the event and can follow along with any other resources that I will be sharing soon on the event page.

Raymond: So thank you all for being here. Okay, great. I think I'm not sharing.

Raymond: Okay. Can you see now the message you can follow along on that specific link of the errand and continue learning Kotlin on functional programming. So, okay. Just to resend it so goodnight to everyone and see you soon on the internet.

Want a high-income, remote career in Mobile Development?

Join Arc and receive offers from high-growth tech startups paying from $60,000 to 175,000 USD!

Discussion 

Loading...