高阶函数很复杂?
Higher order functions is complex?
我看了很多文章,但还是有一些难以理解的地方。我不明白的地方在哪里?我的问题在代码中。希望我问对了。
fun main() {
/*
1- Is it argument or parameter in numbers{} block?
where is it sent to the argument? why do we send "4" if it is parameter?
Are all the functions I will write in (eg println) sent to the numbers function? But this HOF can
only take one parameter.
*/
numbers {
/*
2-How does this know that he will work 3 times?
According to the following 3 functions? Is there a for loop logic??
*/
println(it)
"4" // 3- Which one does this represent? f:(String) or ->String?
}
}
fun numbers(f: (String) -> String) {
f("one")
f("two")
f("three")
println(f(" - "))
}
- 上面的 lambda 块中没有定义参数。这只是您的 lambda 函数的内容。您已经使用了
it
的隐式单个参数名称。 "4"
是您的 lambda 的 return 值。
- lambda 本身并不“知道”它会被调用多少次。在本例中,它被调用了四次,因为您的数字函数调用了参数
f
四次。
- lambda 的 return 值是其最后一个表达式的计算结果。在这种情况下,它 return 是
String
"4"
。
也许这会有所帮助。 Lambda 语法很方便。但是我们可以一次去掉每一块语法糖,看看它的实际含义。
下面的所有代码块都具有完全相同的含义。
这是您的原话:
numbers {
println(it)
"4"
}
首先,当 lambda 省略单个参数时,它获得隐式参数名称 it
。如果我们避免使用这种糖,它看起来像这样:
numbers { inputString ->
println(inputString)
"4"
}
lambda 中最后一个表达式的计算值就是它 return 的值。你也可以明确地写一个 return 语句,但是你必须指定你是从 lambda 中 returning 的,所以你必须输入它的名字。所以如果我们把它放进去,它看起来像这样:
numbers { inputString ->
println(inputString)
return@numbers "4"
}
当 lambda 是传递给函数的最后一个参数时,您可以将它放在括号外。这称为“尾随 lambda”。如果函数是唯一的参数,则根本不需要括号。如果我们跳过这个便利,它看起来像这样:
numbers({ inputString ->
println(inputString)
return@numbers "4"
})
lambda 只是定义函数的一种非常紧凑的方式。如果我们直接定义函数,它看起来像这样:
numbers(fun(inputString: String): String {
println(inputString)
return "4"
})
您传递的函数是numbers()
函数的参数。您也可以单独定义它,然后像这样传递函数引用:
fun myFunction(inputString: String): String {
println(inputString)
return "4"
}
numbers(::myFunction)
我看了很多文章,但还是有一些难以理解的地方。我不明白的地方在哪里?我的问题在代码中。希望我问对了。
fun main() {
/*
1- Is it argument or parameter in numbers{} block?
where is it sent to the argument? why do we send "4" if it is parameter?
Are all the functions I will write in (eg println) sent to the numbers function? But this HOF can
only take one parameter.
*/
numbers {
/*
2-How does this know that he will work 3 times?
According to the following 3 functions? Is there a for loop logic??
*/
println(it)
"4" // 3- Which one does this represent? f:(String) or ->String?
}
}
fun numbers(f: (String) -> String) {
f("one")
f("two")
f("three")
println(f(" - "))
}
- 上面的 lambda 块中没有定义参数。这只是您的 lambda 函数的内容。您已经使用了
it
的隐式单个参数名称。"4"
是您的 lambda 的 return 值。 - lambda 本身并不“知道”它会被调用多少次。在本例中,它被调用了四次,因为您的数字函数调用了参数
f
四次。 - lambda 的 return 值是其最后一个表达式的计算结果。在这种情况下,它 return 是
String
"4"
。
也许这会有所帮助。 Lambda 语法很方便。但是我们可以一次去掉每一块语法糖,看看它的实际含义。
下面的所有代码块都具有完全相同的含义。
这是您的原话:
numbers {
println(it)
"4"
}
首先,当 lambda 省略单个参数时,它获得隐式参数名称 it
。如果我们避免使用这种糖,它看起来像这样:
numbers { inputString ->
println(inputString)
"4"
}
lambda 中最后一个表达式的计算值就是它 return 的值。你也可以明确地写一个 return 语句,但是你必须指定你是从 lambda 中 returning 的,所以你必须输入它的名字。所以如果我们把它放进去,它看起来像这样:
numbers { inputString ->
println(inputString)
return@numbers "4"
}
当 lambda 是传递给函数的最后一个参数时,您可以将它放在括号外。这称为“尾随 lambda”。如果函数是唯一的参数,则根本不需要括号。如果我们跳过这个便利,它看起来像这样:
numbers({ inputString ->
println(inputString)
return@numbers "4"
})
lambda 只是定义函数的一种非常紧凑的方式。如果我们直接定义函数,它看起来像这样:
numbers(fun(inputString: String): String {
println(inputString)
return "4"
})
您传递的函数是numbers()
函数的参数。您也可以单独定义它,然后像这样传递函数引用:
fun myFunction(inputString: String): String {
println(inputString)
return "4"
}
numbers(::myFunction)