Functional Programming in Kotlin: Pure and Total Functions

Functional Programming in Kotlin: Pure and Total Functions

Pure Functions

One of the key concepts in functional programming is the idea of pure functions. A pure function is a function that has no side effects and always returns the same output given the same input. For example, the follwing it's a pure function in Kotlin that calculates the square of a number:

fun square(x: Int): Int = x * x

Pure functions have a number of advantages over non-pure functions, including increased readability, testability, and maintainability. They are also less prone to bugs and easier to reason about.

Total/Partial Functions

Another important concept in functional programming is the distinction between total and partial functions. A total function is a function that is defined for all possible inputs, while a partial function is a function that is only defined for some inputs. In Kotlin, partial functions can be spotted because they have a throw keyword to indicate that the function may throw an exception. For example, consider the following partial function that divides two numbers:

fun divide(x: Int, y: Int): Int {
    if (y == 0) throw IllegalArgumentException("Cannot divide by zero")
    return x / y
}

Partial functions are useful in situations where it is not possible to define a total function, such as when dealing with external dependencies or user input.

Exceptions in Functional programming

In functional programming, it is common to avoid exceptions and use data types that represent possible failures instead. This helps to ensure that the program's behavior is predictable and the handling of errors is more explicit. Instead of Exceptions is common to use a desing pattern to handle error or success: the Either data type

In Kotlin, we can use the Either data type to represent either a success (Right) or a failure (Left) value. This allows us to return and handle errors in a more functional and explicit way.

Here's an example:

sealed class Either<out E, out A> {
    data class Left<out E>(val value: E) : Either<E, Nothing>()
    data class Right<out A>(val value: A) : Either<Nothing, A>()

    fun <B> map(f: (A) -> B): Either<E, B> = when (this) {
        is Left -> this
        is Right -> Right(f(value))
    }

    fun <EE, B> flatMap(f: (A) -> Either<EE, B>): Either<EE, B> = when (this) {
        is Left -> this
        is Right -> f(value)
    }
}

With this, we can define functions that may fail and return an Either type. For instance:

fun divide(a: Int, b: Int): Either<String, Int> =
    if (b == 0) {
        Either.Left("division by zero")
    } else {
        Either.Right(a / b)
    }

And then we can use pattern matching to handle the Either value and decide what to do next:

val result = divide(10, 0)
when (result) {
    is Either.Left -> println("Error: ${result.value}")
    is Either.Right -> println("Result: ${result.value}")
}

Wrapping Up

The Either type is a powerful tool for functional programming that can help developers handle error cases in a safe and concise manner. In future articles, I will expand on this concept and explore its many uses and applications in more detail. I hope that this introduction has sparked your curiosity and encouraged you to explore the possibilities of functional programming in Kotlin. See you soon!