Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Thursday, December 10, 2015

Why Can't I Say: val list = properties.map((key,value) ->

Why Can't I Say: val list = properties.map((key,value) -> "$key = $value")


In Kotlin I can say

//sweet  for ((key,value) in System.getProperties())      println("$key = $value")  

but I cannot say

//sour  val properties = System.getProperties()  val list = properties.map((key,value) -> "$key = $value")  

What is the Kotlin equivalent to properties.map{case (key, value) => s"$key = $value"} in Scala?

Answer by dhg for Why Can't I Say: val list = properties.map((key,value) -> "$key = $value")


You need to use the case keyword to get it to pattern match the tuple:

properties.map { case (key,value) => s"$key = $value" }  

Note that you also have to use => (which creates a lambda function) and curly-braces {} instead of parentheses.

The reason for this is that the argument to map must be a function that takes exactly one parameter. If you write (a,b) =>, then you are creating a function, but it's a function that takes two parameters. When you write { case (a,b) => } you are also creating a function (technically a PartialFunction), that takes one parameter, and pattern matches that parameter to split it into two parts (assuming it's actually a Tuple2).

Answer by pedrofurla for Why Can't I Say: val list = properties.map((key,value) -> "$key = $value")


Your question has absolutely nothing with inference.

In scala you doc:

import collection.JavaConversions._  val properties = System.getProperties()  val list = properties.map{case (key,value) => s"$key = $value"}  

As for your comments.

Having gone from years of Scala, to now exploring Kotlin, I tend to like Kotlin better in its power and simplicity, however, it would be nice to be able to infer things better, given that I am still in learning mode.

"Years of Scala", and yet: the issues in your code have nothing to do with type inference. Not even the syntax was right.

Answer by 0__ for Why Can't I Say: val list = properties.map((key,value) -> "$key = $value")


In addition to @dhg's answer, the following would also be equivalent:

for ((key,value) <- sys.props)    println(s"$key = $value")  

Now that's a bit cooler than hipster Kotlin ;)

So in a for-comprehension you can write pattern matches and don't require a case keyword.

Answer by voddan for Why Can't I Say: val list = properties.map((key,value) -> "$key = $value")


Please correct your syntax for lambda expressions and unpacking:

val properties = mapOf(1 to "First", 2 to "Second")  val list = properties.map {val (value, key) = it; "$key = $value"}   

Answer by mfulton26 for Why Can't I Say: val list = properties.map((key,value) -> "$key = $value")


In Kotlin you can say:

val properties = System.getProperties()  val list = properties.map { "${it.key} = ${it.value}" }  

And if you prefer to unpack the map entries to separate values you can say:

val properties = System.getProperties()  val list = properties.map { val (key, value) = it; "$key = $value" }  

Answer by Eric Kolotyluk for Why Can't I Say: val list = properties.map((key,value) -> "$key = $value")


There is a Kotlin feature that is planned but will not be available in 1.0 which will allow writing {(key,value) -> "$key = $value"} to unpack a value type like a Pair ? cypressious

Thanks @cypressious that was exactly what I was hoping for. Basically the answer is that Kotlin is still evolving and people are still getting around to polishing off cool stuff.

For now I have found that

//savory  val properties = System.getProperties()  val list = properties.map {property -> "${property.key} = ${property.value}"}  

comes pretty close.


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 71

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.