concept function in category kotlin

appears as: functions, function, function, A function, functions, The functions, The function, The function, A function
Functional Programming with Kotlin

This is an excerpt from Manning's book Functional Programming with Kotlin.

This is all it means for an expression to be referentially transparent—in any program, the expression can be replaced by its result without changing the meaning of the program. And we say that a function is pure if calling it with RT arguments is also RT. We’ll look at some examples next.

We will begin by implementing the functions of the API that we’ve developed up to this point. Now that we have a representation for Par, a first attempt should be straightforward. What follows is a naive implementation using this initial representation.

Listing 7.10. Primitive operations for Par can be found in the Pars object
object Pars {
    fun <A> unit(a: A): Par<A> =
        { es: ExecutorService -> UnitFuture(a) } #1

    data class UnitFuture<A>(val a: A) : Future<A> {

        override fun get(): A = a

        override fun get(timeout: Long, timeUnit: TimeUnit): A = a

        override fun cancel(evenIfRunning: Boolean): Boolean = false

        override fun isDone(): Boolean = true

        override fun isCancelled(): Boolean = false
    }

    fun <A, B, C> map2(
        a: Par<A>,
        b: Par<B>,
        f: (A, B) -> C
    ): Par<C> = #2
        { es: ExecutorService ->
            val af: Future<A> = a(es)
            val bf: Future<B> = b(es)
            UnitFuture(f(af.get(), bf.get())) #3
        }

    fun <A> fork(
        a: () -> Par<A>
    ): Par<A> = #4
        { es: ExecutorService ->
            es.submit(Callable<A> { a()(es).get() })
        }
}

As stated before, this example is a naive solution to the problem. We will now identify the issues and address them one by one. The unit operator is represented as a function that returns a UnitFuture, which is a simple implementation of Future that simply wraps a constant value and never uses the ExecutorService. It is always executed and can’t be canceled. Its get method simply returns the value that we gave it.

[23] We do mean algebra in the mathematical sense of one or more sets, together with a collection of functions operating on objects of these sets, and a set of axioms. Axioms are statements assumed true from which we can derive other theorems that must also be true. In our case, the sets are particular types like Par>A< and List>>A<<, and the functions are operations like map2, unit, and sequence.

The Joy of Kotlin

This is an excerpt from Manning's book The Joy of Kotlin.

The first thing you need to do before even starting coding (whether it be the main program or the tests) is to think about the problem in terms of the properties that should be verified when considering the input and the output of the function. You can immediately see that a function with no side effects is much easier to deal with in that case.

Considering your maximum multiple problems, when writing the interface, you realize that it’s not always easy to find such properties. Remember that you must find a set of properties that can be verified on all tuples of type ((Int, List<Int>), Int) that will be true if the last Int is the correct result of applying the function to the pair Int, List<Int>. Here’s a reminder of the function signature:

fun maxMultiple(multiple: Int, list: List<Int>): Int = TODO()

Finding some properties might seem easy, but finding significant ones is more difficult. Ideally, you should find the smallest set of properties that guarantees that the result is correct. But you must do that without using the same logic that you’ll use for implementing the function. Two ways of finding such properties include

In chapter 1, you learned that one of the most important techniques for safer programming is to clearly separate the parts of the programs that don’t depend on anything other than the input data from the part which depends on the state of the outside world. With programs composed of subprograms (called procedures, methods, or functions), this separation transitively applies to these subprograms as well. In Java these are called methods, but in Kotlin they're called functions, which is probably more appropriate because function is a mathematical term with a precise definition (as is usual with mathematics). Kotlin functions can be compared to mathematical functions when they’ve no effect beside returning a value that’s only dependent on their argument. Such functions are often called pure functions by programmers. To write safer programs, therefore, one must

The set of elements of the codomain that have a corresponding element in the domain is called the image of the function.

Figure 3.1 illustrates a function. You can, for example, define the function

Figure 3.1 All elements of a function’s domain must have one and only one corresponding element in the codomain.

Fig03_01.png

Thus far I’ve spoken only about functions of one argument. What about functions of several arguments? Simply put, there’s no such thing as a function of several arguments. Remember the definition of a function? A function is a relationship between a source set and a target set. It isn’t a relationship between two or more source sets and a target set. A function can’t have several arguments. But the product of two sets is itself a set. A function from such a product of sets into a set may seem like a function of several arguments. Let’s consider the following function:

f(x, y) = x + y

There can be a relationship between N x N and N; in which case, it’s a function. But it has only one argument that’s an element of N x N. N x N is the set of all possible pairs of integers. An element of this set is a pair of integers, and a pair is a special case of the more general tuple concept that’s used to represent combinations of several elements. A pair is a tuple of two elements.