Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Functional Programming with Kotlin: The Main Things You Need to Know

It is great that you have developed an interest and would like to learn Functional Programming. But it’s important for you that your doubts are cleared and you have the basic knowledge of what programming language is, how to use it and how it benefits you. That’s the reason you are reading this article, to learn the basics.

What Is Functional Programming?

Functional programming (FP) is actually not a replacement for object-oriented programming (OOP) like many say it is. They are in fact, very different, and it’s easier to think of functional programming as an evolution of object-oriented programming.

Both functional and object-oriented programming rely on Polymorphism and Abstraction, but the FP focuses on Composition, unlike the OOP which focuses on inheritance. Another major difference between both types of programming is that OOP relies a lot on classes, and the properties of the classes. But in functional programming, it’s not necessary that you have properties in the classes, apart from data (POJO) classes. It is in fact, discouraged. What FP uses instead is Immutability and more Pure Functions, and pass data across different functions. The objective with FP is to stay very close to mathematics.

Functional Programming has many benefits which can be summed up in one sentence, which is, “Functional Programming helps the developer to write codes that are pragmatic, concurrency ready and are less prone to error.”

Basic Concepts of Functional Programming

There are few basic concepts upon which every programming paradigm is built. For example, if you want to learn Object-Oriented Programming (OOP), you have to learn some basic concepts such as Polymorphism, Inheritance, Abstraction, etc. the same thing goes for Functional Programming (FP). To learn FP, there are 4 basic concepts which you have to first understand. They are

  • Immutability
  • Lambda
  • High-order functions
  • Pure Functions

Immutability

Immutability is a basic concept of Functional Programming is simply that a variable should give back the same values at every point it’s assessed. It is very similar to constants to some extent but it’s not actually the same as a constant.

In Java, the immutable variable is the String class. But it is possible to modify the String variable in Java. What happens when you do this is that, the String uses an internal copy constructor to set the modified value in a new String instance. It then updates the register, so that, the variable points to a new location where the updated instance resides in the program memory. But the old variable, before the modification, is still there in its memory place.

If you are already thinking about the efficiency of memory, then you should know that java did that only for improved memory efficiency. Every String variable that create in java with the same values are stored in the same memory location. If you modify a part of the String variable, the variable starts pointing to another location with its updated value.

That’s how immutability works in java for improved memory efficiency. It works quite differently with Kotlin.

Kotlin, unlike java, gives you many easy ways to use immutability. It also does not enforce immutability on you, unlike many other functional languages, it only encourages you to use it.

For Kotlin, you can use the val variable instead of the var variable. You should also not try to change the val value as you please. The way val works is very similar to the way the final variable in java works. Immutability is not guaranteed. It allows you to choose to either respect immutability or not to. Because of the getters, and setters that Kotlin uses for variable access, it’s possible for a developer to use custom getter to bypass immutability. This is not advisable though, if you need to carry out such operations, it’s just better to use the var variable instead.

All collection objects such as List, Set, Map, etc. are by default, immutable in Kotlin. This is how Kotlin supports immutable collections. If there’s a need for you to modify them after creating them, you can use a mutable variant, such as MutableSet, MutableList, MutableMap, etc.

Lambda

Lambda is the 11th letter of the Greek alphabet often used to denote a function in mathematics. In programming, they are referred to as anonymous functions.

Lambda allows you to write a function very much like a mathematical expression, using arrow notation and a pair of curly braces.

With a functional language like Kotlin, which treats functions as its first-class citizen, you are freed to assign a lambda to a particular variable or pass it to another function.

It is possible to invoke a lambda function, like a usual function or by just calling invoke (). If you want to denote that a variable contains a lambda, you can do that with this: (PARAMETERS) -> RETURN_TYPE. But you will have to replace the PARAMETERS with a list containing all the parameters. And you replace RETURN_TYPE with the lambda’s return type.

High-Order Functions

High-order functions refer to functions that have higher class and higher order than normal functions. High-order functions are functions that return another function or take another function as a parameter. This is where lambda, and high-order functions are strongly related. High-order functions basically returns a Lambda or takes Lambda as a parameter.

Pure Functions

The concept of pure functions is simply that, to get the best performance, functions should be cache-able with their parameters. To achieve this, functions and their mathematical counterparts should resemble. This implies that a function (f), whenever x is passed as its parameter, should always return the same value of y.

To denote this mathematically, f(x) = y, should be true at all times.

It is also important that a function doesn’t modify anything outside its scope.

For example,

Fun power (number: Int, exponent: Int):
Int {
var result = 1
if (exponent {
throw Exception (“Negative value of exponent isn’t supported, passed $exponent”)
}
for (i in 1.exponent)
{
result = result * number
}
return result
}

The power function in the program above will always give you back the same value for an exponent and a given combination of numbers. This function does not modify anything outside its scope, so, the power () is a Pure Function.

When a Pure Function modifies anything that’s out of its scope, then it is considered to have side effects. An example is adding log/print Into the above function. It will no longer be pure, so it will be considered as having side effects, and the side effect is the log/printIn that was added.

Conclusion

One thing that’s common to developers that had already have a background in OOP is what’s called FOMO (fear of moving out).

From this article, you understand better what functional program is and the basic concept that underlies it, which are immutability, lambda, high-order functions, pure functions and side effects. This means that you have crossed the FOMO stage and have started learning FP. This is, however, the very beginning.

Author’s Bio

John Trogbon is a blogger and writer at essay writing service UK. He’s also a digital marketer with strong interests in communication, social media and self-improvement. You can connect with John on LinkedIn and Facebook.

The post Functional Programming with Kotlin: The Main Things You Need to Know appeared first on TechBuzz Talk.



This post first appeared on TechBuzzTalk, please read the originial post: here

Share the post

Functional Programming with Kotlin: The Main Things You Need to Know

×

Subscribe to Techbuzztalk

Get updates delivered right to your inbox!

Thank you for your subscription

×