在形式语言理论和良好风格方面强调 Scala 的动机? [英] Motivation for Scala underscore in terms of formal language theory and good style?

查看:25
本文介绍了在形式语言理论和良好风格方面强调 Scala 的动机?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么很多人说在 Scala 中使用下划线是好的做法并且使您的代码更可读?他们说动机来自形式语言理论.尽管如此,许多程序员,特别是来自其他语言的程序员,尤其是那些具有匿名函数的程序员,不喜欢使用下划线,尤其是占位符.

Why is it that many people say that using underscore is good practice in Scala and makes your code more readable? They say the motivation comes from formal language theory. Nevertheless many programmers, particularly from other languages, especially those that have anonymous functions, prefer not to use underscores particularly for placeholders.

那么下划线有什么意义呢?为什么 Scala(以及 om-nom-nom 指出的其他一些函数式语言)有下划线?就复杂性和语言理论而言,正式基础是什么,为什么它经常好的风格使用?

So what is the point in the underscore? Why does Scala (and some other functional languages as pointed by om-nom-nom) have the underscore? And what is the formal underpinning, in terms of complexity and language theory, as to why it often good style to use it?

推荐答案

Linguistics

Scala 中大多数下划线使用的起源和动机是允许构建表达式和声明,而无需总是给出语言的每个变量(我的意思是谓词演算中的变量",而不是编程中的)一个名字.我们在自然语言中一直使用这个,例如,我在这句话中使用this"引用了前一句中的一个概念,而我使用this"引用了这句话,但对我的意思没有任何混淆.在自然语言中,这些词通常称为代词"、照应"、cataphors",所指对象先行词"strong>"或后验",理解/取消引用它们的过程称为回指".

Linguistics

The origin and motivation for most of the underscore uses in Scala is to allow one to construct expressions and declarations without the need to always give every variable (I mean "variable" as in Predicate Calculus, not in programming) of the language a name. We use this all the time in Natural Language, for example I referred to a concept in the previous sentence in this sentence using "this" and I referred to this sentence using "this" without there being any confusion over what I mean. In Natural Language these words are usually called "pronouns", "anaphors", "cataphors", the referents "antecedent" or "postcedent", and the process of understanding/dereferencing them is called "anaphora".

如果我们必须先命名自然语言中的每个事物",然后才能引用它,类似于谓词演算和大多数编程语言中的每种类型的事物以对其进行量化,那么说话会变得非常长缠绕.多亏了上下文,我们才能推断出这个"、它"、那个"等词的含义,我们很容易做到.

If we had to name every 'thing' in Natural Language before we can refer to it, similarly every type of thing in order to quantify over it, as in Predicate Calculus and in most programming languages, then speaking would become extremely long winded. It is thanks to context that we can infer what is meant by words like "this", "it", "that", etc, we do it easily.

因此,为什么要将这种简单、优雅和高效的交流方式限制在自然语言上?所以它被添加到 Scala 中.

Therefore why restrict this simple, elegant and efficient means to communicate to Natural Language? So it was added to Scala.

如果我们确实尝试命名每一个事物"或事物类型",句子就会变得如此冗长和复杂,以至于由于冗长和引入冗余符号而变得非常难以理解.添加到句子中的符号越多,理解起来就越困难,这就是为什么它是一种很好的做法,不仅在自然语言中,而且在 Scala 中也是如此.事实上,我们可以根据 Kolmogorov Complexity 来形式化这个断言,并证明一系列采用占位符的句子比那些不必要地命名一切的句子具有更低的复杂性(除非名称在每个实例中都完全相同,但这通常没有意义).因此,我们可以得出结论,与某些程序员的看法相反,占位符语法更简单,更易于阅读.

If we did attempt to name every single 'thing' or 'type of thing', sentences become so long and complicated that it becomes very difficult to understand due to it's verbosity and the introduction of redundant symbols. The more symbols you add to a sentence the more difficult it becomes to understand, ergo this is why it's good practice, not only in Natural Language, but in Scala too. In fact one could formalize this assertion in terms of Kolmogorov Complexity and prove that a sequence of sentences adopting placeholders have lower complexity than those that unnecessarily name everything (unless the name is exactly the same in every instance, but that usually doesn't make sense). Therefore we can conclusively say contrary to some programmers belief, that the placeholder syntax is simpler and easier to read.

它在使用中遇到一些阻力的原因是,如果一个人已经是程序员,那么必须努力重新训练大脑不要命名一切,就像(如果他们能记住)他们可能已经找到了学习首先编写代码需要付出很多努力.

The reason why it has some resistance in it's use, is that if one is already a programmer, one must make an effort to retrain the brain not to name everything, just as (if they can remember) they may have found learning to code in the first place required quite an effort.

现在让我们更正式地看一些具体的用法:

Now let's look at some specific uses more formally:

占位符语法

表示它"、他们"、那个"、他们的"等(即代词),例如1

Means "it", "them", "that", "their" etc (i.e. pronouns), e.g. 1

lines.map(_.length)

可以读作将线映射到他们的长度",同样我们可以将lineOption.map(_.length)读作将线映射到它是长度".在复杂性理论方面,这更简单比对于行中的每个'行',取'行'的长度" - 这将是 lines.map(line => line.length).

can be read as "map lines to their length", similarly we can read lineOption.map(_.length) as "map the line to it's length". In terms of complexity theory, this is simpler than "for each 'line' in lines, take the length of 'line'" - which would be lines.map(line => line.length).

当与类型注解一起使用时,也可以读作the"(定冠词),例如

Can also be read as "the" (definite article) when used with type annotation, e.g.

(_: Int) + 1

整数加1"

存在类型

表示某种类型"(某些"代词),例如

Means "of some type" ("some" the pronoun), e.g

foo: Option[_]

表示foo 是 某种类型的 Option".

means "foo is an Option of some type".

更高级的类型参数

同样,基本上表示某种类型"(某些"代词),例如

Again, basically means "of some type" ("some" the pronoun), e.g.

class A[K[_],T](a: K[T])

可以读作A类需要一些K 某种类型 ..."

Can be read "class A takes some K of some type ..."

模式匹配通配符

表示任何事情"或随便"(代词),例如

Means "anything" or "whatever" (pronouns), e.g.

case Foo(_) => "hello"

可以读作对于包含任何内容的 Foo,返回 'hello'",或对于包含任何内容的 Foo,返回 'hello'"

can be read as "for a Foo containing anything, return 'hello'", or "for a Foo containing whatever, return 'hello'"

导入通配符

表示一切"(代词),例如

Means "everything" (pronoun), e.g.

import foo._

可以理解为从 foo 导入所有内容".

can be read as "import everything from foo".

默认值

现在我读这个像a"(不定冠词),例如

Now I read this like "a" (indefinite article), e.g.

val wine: RedWine = _

给我一杯红酒",服务员应该给你红房子.

"Give me a red wine", the waiter should give you the house red.

下划线的其他用法与本问答的重点无关,但我们还是简要讨论一下

The other uses of underscores are not really related to the point of this Q&A, nevertheless we breifly discuss them

忽略的值/参数/提取

允许我们以明确的模式安全"方式忽略事物.例如

Allow us to ignore things in an explicit 'pattern safe' way. E.g.

val (x, _) = getMyPoint

说,我们不打算使用第二个坐标,所以当你在代码中找不到用途时,不必感到奇怪.

Says, we are not going to use the second coordinate, so no need to get freaky when you cant find a use in the code.

导入隐藏

只是一种表达除了"(介词)的方式.

Just a way to say "except" (preposition).

函数应用

例如

val f: String => Unit = println _

这是一个有趣的例子,因为它在语言学中有一个完全类似的东西,即名词化,使用动词、形容词或副词作为名词短语的中心词,带有或没有形态变换" - 维基百科.更简单地说,就是将动词形容词转化为名词的过程.

This is an interesting one as it has an exact analogue in linguistics, namely nominalization, "the use of a verb, an adjective, or an adverb as the head of a noun phrase, with or without morphological transformation" - wikipedia. More simply it is the process of turning verbs or adjectives into nouns.

在特殊方法名中使用

纯粹是语法问题,与语言学无关.

Purely a syntax thing and doesn't really relate to linguistics.

这篇关于在形式语言理论和良好风格方面强调 Scala 的动机?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆