Kotlin Thursdays - Introduction to Functional Programming in Kotlin Part 1


Resources

Introduction

Welcome to Kotlin Thursdays! This week I’m starting a series on functional programming with Kotlin. Functional programming is something I’m passionate about, and Kotlin has some great functional programming support! Functional programming can lead to code that is easier to think about, has fewer bugs and is easier to test.

What better way to dive into functional programming than to learn about functions! It might help to think of functions as “mini-programs”. Functions allow us to write some code once and then use that code multiple times throughout the rest of our program.



The Main Function

To make a Kotlin program, we need to make a function to “kickstart” our program. This function is called the main function. To create the mainfunction, let’s make a file called FunWithFunctions.kt and write the following code in it:

fun main() {
println(“Hello World!”)
}

We can compile this into a jar file that we can run with the following command on our terminal:

kotlinc-jvm FunWithFunctions.kt -include-runtime -d functions.jar

Then we can run the program with the following command:

java -jar functions.jar

For those not familiar, a jar file is how Java Virtual Machine programs are packaged into programs that we can easily run. Kotlin, like Java, uses the Java Virtual Machine to run.

Kotlin knows that when we run our code, it should start by running the mainfunction. But we can make our own custom functions as well! Let’s write our first custom function in my FunWithFunctions.kt file:

fun myFirstFunction(): String {
return "I made my first Kotlin function!"
}

We’re using the same fun keyword as before, and we’re giving this function the name of myFirstFunction. Notice the : String that comes after the function name; this tells Kotlin that when this function finishes running, it is going to return a String object. If we don’t specify that, Kotlin assumes that our function is returning Unit, which is the same thing as void in Java. Finally, we use the return keyword to return our string.

This new function we’ve created can now be used in the main function of our program. Here’s the final content of the FunWithFunctions.kt file:

fun myFirstFunction(): String {
return "I made my first Kotlin function!"
}
fun main() {
val result = myFirstFunction()
println(result)
}

Now we can compile and run it:

kotlinc-jvm FunWithFunctions.kt -include-runtime -d functions.jar
java -jar functions.jar

Higher Order Functions

Kotlin has support for higher order functions. A function is a higher order function if it can do at least one of the following things:

  • Accept another function as an argument.
  • Return a function.

Passing Functions to Other Functions

Let’s examine passing functions into other functions. First, let’s define a function that returns a String:

fun a(): String {
return "I can haz functionz!"
}

Next, we’ll create a second function that can accept this function as a parameter:

fun b(parameter: () -> String): String {
return parameter()
}

We can call function b, passing in function a in our main function:

fun main() {
println(b(::a))
}

When function b runs, it will take function a as the parameter and execute it, returning the value:

I can haz functionz!

Functions That Return Functions

Let’s talk about the second capability that higher order functions possess: returning other functions. Let’s start off again with our function a from before:

fun a(): String {
return “I can haz functionz!”
}

Next we define a new function c that will return the function a we defined earlier:

fun c(): () -> String {
return ::a
}

Note that function c has a return type of () -> String. This is because c is returning the function a which returns a String. Kotlin, being strongly typed, requires us to match our return types correctly. This is different from dynamically typed languages like Ruby, which do not require types to be specified. This might seem like a nuisance at first, but in future episodes we’ll examine how using a strongly typed language like Kotlin actually helps us prevent bugs in our code by using types.

When the function c executes, it returns the function a, but it does not execute function a. Let’s see what happens when we call this in the main function:

fun main() {
println(c())
}

This will print:

function a (Kotlin reflection is not available)

This is because function a is being directly returned as a function, instead of executing and returning a String type. To make it execute, we would have to add two sets of parentheses in the println statement:

fun main() {
println(c()())
}

Now the function a, which is returned by the function c, will execute and return the String value:

I can haz functionz!

This prints out correctly because now we’re calling c which returns a function, then calling that returned function (c is returning a).

To help clarify what’s going on here, let’s write our main function a different way:

fun main() {
val functionA = c()
println(functionA)
}

Note the lack of parentheses around functionA in the println statement. Kotlin will print this out again:

function a (Kotlin reflection is not available)

We can add parentheses to functionA in the println statement to make functionA execute:

fun main() {
val functionA = c()
println(functionA())
}

Now we get the result we wanted:

I can haz functionz!

Higher order functions are a key component of functional programming, as it allows us to build up our program by connecting functions to functions. In future episodes we’ll start to examine how this becomes useful especially as we later learn about function composition, which allows us to write software that is easier to comprehend.

Join me next week on Kotlin Thursdays when I talk about anonymous functions and lambdas!

Views: 111

Happy 10th year, JCertif!

Notes

Welcome to Codetown!

Codetown is a social network. It's got blogs, forums, groups, personal pages and more! You might think of Codetown as a funky camper van with lots of compartments for your stuff and a great multimedia system, too! Best of all, Codetown has room for all of your friends.

When you create a profile for yourself you get a personal page automatically. That's where you can be creative and do your own thing. People who want to get to know you will click on your name or picture and…
Continue

Created by Michael Levin Dec 18, 2008 at 6:56pm. Last updated by Michael Levin May 4, 2018.

Looking for Jobs or Staff?

Check out the Codetown Jobs group.

 

Enjoy the site? Support Codetown with your donation.



InfoQ Reading List

Presentation: Adaptable Innovation: How Microsoft Leverages React Native for Strategic Advantage

Lorenzo Sciandra unveils the use of React Native across Microsoft's diverse ecosystem, emphasizing the strategic and technological reasons for its continued selection.

By Lorenzo Sciandra

How to Delight Your Developers with User-Centric Platforms and Practices

By focusing on the users, platform development teams can ensure that they build a platform that tackles the true needs of developers, Ana Petkovska said at QCon London. In her talk, Delight Your Developers with User-Centric Platforms & Practices, she shared what their Developer Experience (DevEx) group looks like and what products and services they provide.

By Ben Linders

Article: Reactive Real-Time Notifications with SSE, Spring Boot, and Redis Pub/Sub

Explore the power of reactive programming for building scalable real-time notification systems. Using Spring Boot Reactive and Spring WebFlux, leverage non-blocking operations to handle high-volume, asynchronous data flows efficiently. Discover how Redis Pub/Sub enables event-driven messaging and how the SSE protocol provides persistent connections for instant client updates without polling.

By Matteo Rossi

ASP.NET Core 9: Enhancements in Static Asset Handling, Blazor, SignalR, and OpenAPI Support

Microsoft has released .NET 9, which contains features regarding ASP.NET Core 9. This latest release focuses on optimizing static asset handling, refining Blazor's component interaction, enhancing SignalR's observability and performance, and streamlining API documentation through built-in OpenAPI support.

By Robert Krzaczyński

QCon SF: Mandy Gu on Using Generative AI for Productivity at Wealthsimple

Mandy Gu spoke at QCon SF 2024 about how Wealthsimple, a Canadian fintech company, uses Generative AI to improve productivity. Her talk focused on the development and evolution of their GenAI tool suite and how Wealthsimple crossed the "Trough of Disillusionment" to achieve productivity.

By Anthony Alford

© 2024   Created by Michael Levin.   Powered by

Badges  |  Report an Issue  |  Terms of Service