scala foreach 和 map 初始值设定项
刚刚看到一个有趣的可能性,可以在 Scala 中为 foreach 或 map 等高阶函数初始化代码块:
(1 to 3) map {
val t = 5
i => i * 5
}
(1 to 3) foreach {
val line = Console.readLine
i => println(line)
}
这是一些已记录的功能还是我应该避免这样的构造?我可以想象,“初始化”块进入构造函数,而闭包本身变成了 apply() 方法?
感谢帕特提出原始问题(http://extrabright.com /blog/2010/07/10/scala-question-regarding-readline)
Just seen an interesting possibility to initialize code blocks in Scala for high order functions such as foreach or map:
(1 to 3) map {
val t = 5
i => i * 5
}
(1 to 3) foreach {
val line = Console.readLine
i => println(line)
}
Is this some documented feature or should I avoid such constructs? I could imagine, the "initialization" block comes into the constructor and the closure itself becomes an apply() method?
Thanks Pat for the original Question (http://extrabright.com/blog/2010/07/10/scala-question-regarding-readline)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
虽然所使用的功能并不罕见,但我承认这是一个相当奇怪的功能组合。基本技巧是 Scala 中的任何块都是一个表达式,其类型与块中最后一个表达式相同。如果最后一个表达式是函数,则意味着该块具有函数类型,因此可以用作“map”或“foreach”的参数。在这些情况下发生的情况是,当调用“map”或“foreach”时,将对块进行求值。该块计算为一个函数(在第一种情况下为 i=> i*5 ),然后将该函数映射到该范围。
此构造的一种可能用途是让块定义可变变量,并且每次调用时生成的函数都会改变变量。变量将被初始化一次,由函数关闭,并且每次调用函数时它们的值都会更新。
例如,这是计算前 6 个阶乘数的一种有点令人惊讶的方法
(顺便说一句,很抱歉使用阶乘作为示例。它要么是阶乘,要么是斐波那契。函数式
编程协会规则。如果你对此有疑问,请与大厅里的男孩们一起讨论。)
让块返回函数的一个不太重要的原因是在块的前面定义辅助函数。例如,如果您的第二个示例是,
结果将是读取三行并每行回显一次,而您的示例则读取该行一次并回显三次。
While the features used are not uncommon, I'll admit is is a fairly odd combination of features. The basic trick is that any block in Scala is an expression, with type the same as the last expression in the block. If that last expression is a function, this means that the block has functional type, and thus can be used as an argument to "map" or "foreach" . What happens in these cases is that when "map" or "foreach" is called, the block is evaluated. The block evaluates to a function ( i=> i*5 in the first case ), and that function is then mapped over the range.
One possible use of this construct is for the block to define mutable variables, and the resulting function mutate the variables each time it is called. The variables will be initialized once, closed over by the function, and their values updated every time the function is called.
For example, here's a somewhat surprising way of calculating the first 6 factorial numbers
(BTW, sorry for using factorial as an example. It was either that or fibonacci. Functional
Progamming Guild rules. You gotta problem with that, take it up with the boys down at the hall.)
A less imperative reason to have a block return a function is to define helper functions earlier in the block. For instance, if your second example were instead
The result would be that three lines were read and echoed once each, while your example had the line read once and echoed three times.
首先,原博客的评论“Scala Question About readLine ”帖子提及
博客 Scala for Java Refugees Part 6: Getting Over Java 有一个关于高阶函数的有趣部分,包括:
First, the comment of the original blog "Scala Question Regarding readLine" post mention
The blog Scala for Java Refugees Part 6: Getting Over Java has an interesting section on Higher Order function, including: