Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Kotlin have an identity function?

Tags:

Scala has a generic identity function in the Predef:

def identity[A](x: A): A 

Does Kotlin have a similar facility in the standard library? Of course I could simply use { it } instead, but I find identity easier to read, and instantiating all those lambdas is a little wasteful.

I must be able to use this identity function in places where a function (Foo) -> Foo is expected, for any type Foo. Is such a function even possible in Kotlin's type system? (In the case of Scala, there is an implicit conversion that wraps the method inside a function object or something.)

like image 223
fredoverflow Avatar asked Sep 01 '15 16:09

fredoverflow


People also ask

How do you pass a function in Kotlin?

In Kotlin, a function which can accept a function as parameter or can return a function is called Higher-Order function. Instead of Integer, String or Array as a parameter to function, we will pass anonymous function or lambdas. Frequently, lambdas are passed as parameter in Kotlin functions for the convenience.

What is trailing lambda in Kotlin?

To help deal with this, Kotlin supports a specific kind of syntax referred to as trailing lambda syntax. This syntax states that if the final parameter to a function is another function, then the lambda can be passed outside of the function call parentheses.

How do you assign a function to a variable in Kotlin?

You can assign them to variables and constants just as you can any other type of value, such as an Int or a String . This function takes two parameters and returns the sum of their values. Here, the name of the variable is function and its type is inferred as (Int, Int) -> Int from the add function you assigned to it.

What is identity function in Scala?

The identity function's type parameter is A , which like the value parameter a is simply a unique identifier. It is used to define the type of the value parameter a and the return type of the function.


2 Answers

If you need to pass the identity function as a parameter to another function, you can simply use { it }. For example, it you have a List<List<String>> and want to flatten it to a List<String>, you could use:

list.flatMap(identity) 

where identity is the identity function. This can be written as:

list.flatMap { it } 

This is equivalent to:

list.flatMap { x -> x } 

The alternative would be to define the identity function somewhere, such as:

val identity: (List<String>) -> List<String> = { it } 

But we can't create a generic val, so we would have to define an identity function for each type. The solution, (as it is done in Java Function interface) is to define it as a constant function:

fun <A> identity(): (A) -> A = { it } 

and use it as:

list.flatMap(identity) 

Of course, it is much easier to write:

list.flatMap { it } 

Declaring an identity function once for all (that would work for all types) is not possible because it would have to be parameterized. What is possible is to use a function returning this identity function:

fun <T> identity(): (T) -> T  = { it } 

Although it does the job, it is not very helpful since one has now to write:

list.flatMap(identity()) 
like image 163
Pierre-Yves Saumont Avatar answered Sep 17 '22 20:09

Pierre-Yves Saumont


There's no such function at the moment, but you can easily define it yourself:

fun <T> identity(x: T): T = x 

If you think there are enough use cases for this function to be declared in Kotlin standard library, please file an issue at youtrack.jetbrains.com. Thanks!

like image 35
Alexander Udalov Avatar answered Sep 20 '22 20:09

Alexander Udalov