Scala 中的无点风格案例
对于 FP 行家来说,这似乎是显而易见的,但是 Scala 中的无点风格有什么用呢?在这个主题上真正让我感兴趣的是一个插图,它显示了点自由风格在某些方面(例如性能、优雅、可扩展性、可维护性)比以非点自由风格解决相同问题的代码要好得多。
This may seem really obvious to the FP cognoscenti here, but what is point free style in Scala good for? What would really sell me on the topic is an illustration that shows how point free style is significantly better in some dimension (e.g. performance, elegance, extensibility, maintainability) than code solving the same problem in non-point free style.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
很简单,它是为了能够避免在不需要的地方指定名称,考虑一个简单的例子:
在这种情况下,
foreach
希望接受String =>; Unit
,一个接受 String 并返回 Unit 的函数(本质上,没有可用的返回值,它纯粹通过副作用起作用)不需要在此处将名称绑定到传递给 println 的每个 String 实例。可以说,这样做只会使代码更加冗长:
或者甚至就
我个人而言,当我看到不是以无点风格编写的代码时,我将其视为绑定名称可能被使用两次的指示符,或者它对于记录代码有一定的意义。同样,我将无点样式视为我可以更简单地推理代码的标志。
Quite simply, it's about being able to avoid specifying a name where none is needed, consider a trivial example:
In this case,
foreach
is looking to acceptString => Unit
, a function that accepts a String and returns Unit (essentially, that there's no usable return and it works purely through side effect)There's no need to bind a name here to each String instance that's passed to
println
. Arguably, it just makes the code more verbose to do so:Or even
Personally, when I see code that isn't written in point-free style, I take it as an indicator that the bound name may be used twice, or that it has some significance in documenting the code. Likewise, I see point-free style as a sign that I can reason about the code more simply.
一般来说,无点风格的一个吸引力在于,没有一堆“点”(与函数相反的值)四处浮动,必须在多个地方重复这些“点”以将它们线程化地进行计算,因此犯错误的机会就会减少,例如输入变量名称时。
然而,在 Scala 中,无点的优点很快就被其推断类型的能力微弱所抵消,无点代码加剧了这一事实,因为“点”充当类型推断器的线索。在 Haskell 中,由于其几乎完整的类型推断,这通常不是问题。
One appeal of point-free style in general is that without a bunch of "points" (values as opposed to functions) floating around, which must be repeated in several places to thread them through the computation, there are fewer opportunities to make a mistake, e.g. when typing a variable's name.
However, the advantages of point-free are quickly counterbalanced in Scala by its meagre ability to infer types, a fact which is exacerbated by point-free code because "points" serve as clues to the type inferencer. In Haskell, with its almost-complete type inferencing, this is usually not an issue.
我认为除了“优雅”之外没有其他优点:它更短一些,并且可能更具可读性。它允许将函数作为实体进行推理,而无需在心理上“深入”函数应用程序,但当然您需要首先习惯它。
我不知道有什么例子可以通过使用它来提高性能(如果你最终得到一个函数,而一个方法就足够了,那么情况可能会变得更糟)。
I see no other advantage than "elegance": It's a little bit shorter, and may be more readable. It allows to reason about functions as entities, without going mentally a "level deeper" to function application, but of course you need getting used to it first.
I don't know any example where performance improves by using it (maybe it gets worse in cases where you end up with a function when a method would be sufficient).
Scala 的无点语法是神奇的 Scala 运算符(实际上是函数)的一部分。即使是最基本的运算符也是函数:
例如:
...与...相同
...但是,当然,无点样式读起来更自然(加号似乎是一个运算符)。
Scala's point-free syntax is part of the magic Scala operators-which-are-really-functions. Even the most basic operators are functions:
For example:
...is the same as...
...but of course, the point-free style reads more naturally (the plus appears to be an operator).