问题描述
我一次又一次地听到这种说法,我试图理解和验证 FP 和 OO 是正交的想法.
I have heard this time and again, and I am trying to understand and validate the idea that FP and OO are orthogonal.
首先,两个概念正交是什么意思?
First of all, what does it mean for 2 concepts to be orthogonal?
FP 尽可能地鼓励不变性和纯度,而 OO 似乎是为状态和突变而构建的——命令式编程的一个稍微有组织的版本?我意识到对象可以是不可变的,但 OO 似乎对我暗示了状态/变化.
FP encourages immutability and purity as much as possible, while OO seems built for state and mutation – a slightly organized version of imperative programming? I realize that objects can be immutable, but OO seems to imply state/change to me.
他们似乎是对立的.这如何影响它们的正交性?
They seem like opposites. How does that affect their orthogonality?
像Scala这样的语言让OO和FP都可以轻松搞定,这会影响两种方法的正交性吗?
A language like Scala makes it easy to do OO and FP both, does this affect the orthogonality of the two methods?
推荐答案
正交性 暗示两件事无关.它来自数学,意思是垂直.在通常的用法中,它可能意味着两个决定是不相关的,或者在考虑另一个主题时一个主题是不相关的.此处使用的正交意味着一个概念既不暗示也不排除另一个.
Orthogonality implies that two things are unrelated. It comes from mathematics where it means perpendicular. In common usage it can mean two decisions are unrelated or that one subject is irrelevant when considering another subject. As used here, orthogonal means that one concept doesn't either imply or exclude the other.
两个概念面向对象编程和函数式编程 彼此不兼容.面向对象并不意味着可变性.许多以传统方式介绍面向对象程序的人通常首先使用 C++、Java、C# 或类似的语言,其中可变性很常见,甚至受到鼓励(标准库提供了各种可变类供人们使用).因此,很多人将面向对象编程与命令式编程和可变性联系起来是可以理解的,因为他们就是这样学习的.
The two concepts object oriented programming and functional programming are not incompatible with each other. Object orientedness does not imply mutability. Many people who are introduced to object oriented programs the traditional way often first use C++, Java, C# or similar languages where mutability is common and even encouraged (standard libraries provide a varierty of mutable classes for people to use). Therefore it is understandable that many people associate object oriented programming with imperative programming and mutability, as this is how they have learned it.
然而,面向对象的编程涵盖以下主题:
However object oriented programming covers topics like:
- 封装
- 多态性
- 抽象
这些都不意味着可变性,也不排除函数式编程.所以是的,它们是正交,因为它们是不同的概念.它们不是对立的 - 您可以使用一个,或另一个,或两者(甚至都不使用).Scala 和 F# 等语言试图将这两种范式组合成一种语言:
None of this implies mutability, and none of it excludes functional programming. So yes they are orthogonal in that they are different concepts. They are not opposites - you can use one, or the other, or both (or even neither). Languages like Scala and F# attempt to combine both paradigms into a single language:
Scala 是一种多范式编程语言,旨在集成面向对象编程和函数式编程的特性.
F# 是一种用于 .NET 的简洁、富有表现力且高效的函数式和面向对象语言,可帮助您编写简单的代码来解决复杂的问题.
这篇关于FP 和 OO 是正交的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!