什么是“无积分”? 风格(在函数式编程中)?
我最近注意到的一个短语是“point free”风格的概念......
然后,我发现这里他们提到“另一个可能值得讨论的主题是作者的不喜欢无点风格。”
什么是“无点”风格? 有人可以给出简洁的解释吗? 它与“自动”柯里化有关吗?
为了了解我的水平 - 我一直在自学Scheme,并编写了一个简单的Scheme解释器......我理解什么是“隐式”柯里化,但我不知道任何Haskell或ML。
A phrase that I've noticed recently is the concept of "point free" style...
First, there was this question, and also this one.
Then, I discovered here they mention "Another topic that may be worth discussing is the authors' dislike of point free style."
What is "point free" style? Can someone give a concise explanation? Does it have something to do with "automatic" currying?
To get an idea of my level - I've been teaching myself Scheme, and have written a simple Scheme interpreter... I understand what "implicit" currying is, but I don't know any Haskell or ML.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
只需查看维基百科文章即可获得定义:
Haskell 示例:
常规(显式指定参数):
Point-free(
sum
没有任何显式参数 - 它只是以 0 开头的+
的折叠):或者更简单:您可以只写
g = f
,而不是g(x) = f(x)
。所以是的:它与柯里化(或函数组合等操作)密切相关。
Just look at the Wikipedia article to get your definition:
Haskell example:
Conventional (you specify the arguments explicitly):
Point-free (
sum
doesn't have any explicit arguments - it's just a fold with+
starting with 0):Or even simpler: Instead of
g(x) = f(x)
, you could just writeg = f
.So yes: It's closely related to currying (or operations like function composition).
无点风格意味着所定义的函数的参数没有明确提及,该函数是通过函数组合来定义的。
如果您有两个函数,例如
和 如果您想将这两个函数组合成一个计算
x*x+1
的函数,您可以将其定义为“point-full”,如下所示:不会谈论参数
x
:Point-free style means that the arguments of the function being defined are not explicitly mentioned, that the function is defined through function composition.
If you have two functions, like
and if you want to combine these two functions to one that calculates
x*x+1
, you can define it "point-full" like this:The point-free alternative would be not to talk about the argument
x
:JavaScript 示例:
参考
A JavaScript sample:
Reference
点自由风格意味着代码没有明确提及它的参数,即使它们存在并且正在使用。
由于函数的工作方式,这在 Haskell 中有效。
例如:
返回一个接受一个参数的函数,因此没有理由显式键入该参数,除非您也想要。
Point free style means that the code doesn't explicitly mention it's arguments, even though they exist and are being used.
This works in Haskell because of the way functions work.
For instance:
returns a function that takes one argument, therefore there is no reason to explicit type the argument unless you just want too.
对此最好的解释是 Haskell wiki (摘录如下),由 < a href="https://stackoverflow.com/q/944446/1814970">Petr 在评论中。
该点是一个数学点(上面的
x
),因此是“无点”符号。 如果您需要,该链接提供了更多详细信息。The best explanation for this is in Haskell wiki (excerpt below), pointed out by Petr in a comment.
The point is a mathematical point (
x
above), hence "points-free" notation. The link gives more detail, if you need.我无法使 Brunno 提供的 javascript 示例工作,尽管代码清楚地说明了 pointfree 想法(即没有参数)。 因此,我使用 ramda.js 提供另一个示例。
假设我需要找出句子中最长的单词,给定一个字符串
"Lorem ipsum dolor sat amet consectetur adipiscing elit"
我需要输出类似{ word: 'consectetur', length: 11 }
如果我使用纯 JS 风格代码,我将像这样编写代码,使用映射和化简函数
对于 ramda,我仍然使用映射和化简函数。 我
认为我的 ramda 代码的要点是将我的函数链接在一起的管道,它使我的目的更加清晰。 不需要创建临时变量
strArray
是一个奖励(如果我在管道中有超过 3 个步骤,那么它将成为一个真正的奖励)。I can't make the javascript sample provided Brunno work, although the code illustrate pointfree idea (i.e. no arguments) clearly. So I use ramda.js to provide another example.
Say I need to find out the longest word in a sentence, given a string
"Lorem ipsum dolor sit amet consectetur adipiscing elit"
I need output something like{ word: 'consectetur', length: 11 }
If I use plain JS style code I will code like this, using a map and a reduce function
With ramda I still use a map & a reduce but I will code like this
I will argue that the gist of my ramda code is the pipe that chains my functions together and it makes my purpose more clearly. No need to create a temporary variable
strArray
is a bonus (if I have more than 3 steps in the pipe then it will become a real bonus).这是一个没有任何其他库的 TypeScript 示例:
您可以看到无点样式更“流畅”并且更易于阅读。
Here is one example in TypeScript without any other library:
You can see point-free style is more "fluent" and easier to read.