Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is putting the lambda expression after the parameters on a mapTo call legal syntax?

Tags:

I found a piece of code I do not understand.

I am transforming a JSONArray into a List.
Kotlin provides the function mapTo in it's stdlib(link)

mapTo

inline fun <T, R, C : MutableCollection<in R>> Iterable<T>.mapTo(     destination: C,      transform: (T) -> R ): C (source) 

Applies the given transform function to each element of the original collection and appends the results to the given destination.

This functions has 2 parameters and can be used like this (as expected):

(0..jsonArray.length() - 1).mapTo(targetList, {it -> jsonArray[it].toString()}) 

But apparently this is also valid syntax (not expected):

(0..jsonArray.length()-1).mapTo(targetList) {it -> jsonArray[it].toString()} 

As you can see, the function parameters end after outputList and the lambda expression is just put at the end of the function call.


Furthermore this is legal (as expected):

val transformation = {it : Int -> jsonArray[it].toString()} (0..jsonArray.length()-1).mapTo(targetList, transformation) 

but this is not (???):

val transformation = {it : Int -> jsonArray[it].toString()} (0..jsonArray.length()-1).mapTo(targetList) transformation 
like image 916
JDurstberger Avatar asked Mar 30 '17 09:03

JDurstberger


People also ask

What is the syntax of a lambda expression?

Java Lambda Expression Syntax Java lambda expression is consisted of three components. 1) Argument-list: It can be empty or non-empty as well. 2) Arrow-token: It is used to link arguments-list and body of expression. 3) Body: It contains expressions and statements for lambda expression.

How to create lambda expression?

To create a lambda expression, you specify input parameters (if any) on the left side of the lambda operator and an expression or a statement block on the other side. When you use method-based syntax to call the Enumerable. Select method in the System.

How to write lambda expression in c#?

Lambda expressions in C# are used like anonymous functions, with the difference that in Lambda expressions you don't need to specify the type of the value that you input thus making it more flexible to use. The '=>' is the lambda operator which is used in all lambda expressions.

When to use lambda expressions java?

Java lambda expressions are commonly used to implement simple event listeners / callbacks, or in functional programming with the Java Streams API. Java Lambda Expressions are also often used in functional programming in Java .


1 Answers

As stated in the documentation:

In Kotlin, there is a convention that if the last parameter to a function is a function, and you're passing a lambda expression as the corresponding argument, you can specify it outside of parentheses:

lock (lock) {     sharedResource.operation() } 

Another example of a higher-order function would be map():

fun <T, R> List<T>.map(transform: (T) -> R): List<R> {     val result = arrayListOf<R>()     for (item in this)         result.add(transform(item))     return result } 

This function can be called as follows:

val doubled = ints.map { it -> it * 2 } 

Note that the parentheses in a call can be omitted entirely if the lambda is the only argument to that call.

The documentation states clearly that for the above to work the last argument must be lambda expression and not a variable of matching type.

like image 69
miensol Avatar answered Oct 21 '22 20:10

miensol