Kotlin - forEach(Kotlin - forEach)
我是Kotlin的初学者。 你如何解释以下代码片段?
fun main(args: Array<String>) {
var k = listOf<Double>(1.2,77.8,6.3,988.88,0.1)
k.forEach(::println)
}
运行正常并给出列表,但有人可以帮助解释k.forEach(:: println)如何真正起作用?
I am beginner in Kotlin. How do you explain the following code snippet ?
fun main(args: Array<String>) {
var k = listOf<Double>(1.2,77.8,6.3,988.88,0.1)
k.forEach(::println)
}
This runs fine and gives the list but can someone please help with the explanation for how does k.forEach(::println) really work?
原文:https://stackoverflow.com/questions/47074091
更新时间:2020-02-17 11:05
最满意答案
forEach
将每个元素放在k
并执行您指定的操作。 在您的示例中,“ what ”参数是::println
,它引用stdlib函数println(message: Any)
。 ::
引入了对此函数的函数引用 。 每个元素作为参数message
给println
,因此它将被打印在控制台上。
为了更清楚,你可以传递一个lambda而不是像这样的函数引用:
k.forEach{
println(it)
}
forEach
takes each element in k
and does what you specify it to do. In your example, the "what" argument is ::println
, which refers to the stdlib function println(message: Any)
. The ::
introduced a function reference to this function. Each element is passed as the argument message
to println
and thus it's being printed on the console.
To make it more clear, you could pass a lambda instead of the function reference like this:
k.forEach{
println(it)
}
2018-01-29
我建议避免在你的习惯用语中重复Label这个词: val labels = arrayOf("A", "B", "C", "D")
.map { Label(it).apply { prefWidth = 50.0 } }
.toTypedArray()
这会创建更多的瞬态对象,但它可以减少噪音,并且更容易看到标签之间的差异。 What I'd suggest is avoiding repeating the word Label in your idiom:
...
为了避免存储中间值,可以使用Sequence ,它稍微等同于Iterable (有关详细信息,请参阅另一个问答 )。 要将Map的条目作为Sequence ,请使用.asSequence()转换(同样,不创建中间集合),然后在序列上使用.filter { ... }和.forEach { ... } : myMap.asSequence().filter { someCondition(it) }.forEach { doSomething(it) }
这将创建一个管道,它将逐个查询条目,检查它
...
不, it符号总是引用最内层的隐式单个lambda参数。 为了解决这个问题,并且为了提高代码的可读性,每次在嵌套带参数的lambda表达式时使用命名的lambda参数,如编码约定中所建议的那样: data.arrayresults.forEach { result ->
result.myData.let { myData ->
val itemView - inflater.inflate(R.layout.somelayout)
itemVie
...
您可以使用Spark中的路径*来匹配所有路径,如下所示: before("*", { req, res ->
logger.info("${req.requestMethod().toUpperCase()} ${req.fullUri()} by ${req.ip()} (${req.userAgent()})")
})
// or:
before("*") { req, res -> // ...
You can use the path * in Spark to match
...
Kotlin中的范围是包容性的,因此0..foodObjects!!.size从0开始并以foodObjects.size结束,包括两端。 当您的循环尝试使用自己的大小索引列表时,这会导致异常,该大小比最大的有效索引多一个。 要创建不包含上限的范围(如Java循环),您可以使用until : for(i in 0 until foodObjects!!.size) {
// ...
}
如果您对预先使用的集合进行空检查,您还可以稍微清理一下代码: if (foodObjects != n
...
Kotlin问题跟踪器中有一个打开的功能请求 ,标题为“支持具有默认值的函数引用作为其他函数类型” ,这似乎是您的用例缺少的功能。 该功能目前的版本为1.3。 There's an open feature request in the Kotlin issue tracker with the title "Support function references with default values as other function types", which seems to be wha
...
这些类是Kotlin编译器的直接输出,之后应由Gradle打包到JAR中。 要构建JAR,您可以像在Java项目中一样运行jar任务: gradle jar
由于任务依赖性,此任务通常gradle build期间运行。 这将把Kotlin类打包成一个JAR存档(与其他JVM类一起,如果你有一个多语言项目),通常位于build/libs/ yourProjectName .jar 。 关于运行JAR,请参阅此问答以获取详细说明:( 链接) Thanks for @hotkey answer, it
...
只要你,任何Kotlin都与Android兼容 compile "org.jetbrains.kotlin:kotlin-stdlib" // or
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk7"
这个带有两个参数回调的forEach方法来自Java 8,它仅在API 24之后可用。这也是为什么在API 24以下你不能 compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
您正在寻找的是Kotli
...
forEach将每个元素放在k并执行您指定的操作。 在您的示例中,“ what ”参数是::println ,它引用stdlib函数println(message: Any) 。 ::引入了对此函数的函数引用 。 每个元素作为参数message给println ,因此它将被打印在控制台上。 为了更清楚,你可以传递一个lambda而不是像这样的函数引用: k.forEach{
println(it)
}
forEach takes each element in k and does wha
...
map调用在forEach之前终止,因此每次launch都将在第一次join之前调用(在forEach内部)。 这些收集操作不像Java的Streams那样工作。 阅读此答案以获取更多信息。 在Kotlin,它是这样的: launch协同程序1-100 join协程1-100 The map call terminates before forEach, thus every single launch will be called before the first join (inside fo
...