FP和OO是正交的吗? [英] Are FP and OO orthogonal?

查看:194
本文介绍了FP和OO是正交的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一次又一次听到了这个消息,我试图理解和验证FP和OO是正交的想法。首先,它是什么意味着两个概念是正交的?FP $鼓励尽可能的不变性和纯度,而OO似乎是为状态和变异而建立的 - 一个有组织的版本的命令式编程?我意识到对象可以是不可改变的,但OO似乎暗示了对我的状态/改变。



它们看起来像是对立的。如何影响它们的正交性?



像Scala这样的语言可以很容易地执行OO和FP,这是否会影响两种方法的正交性?

解决方案

正交性意味着两件事情是不相关的。它来自数学,这意味着垂直。在通常的用法中,它可能意味着两个决定是不相关的,或者当考虑另一个主题时一个主题是不相关的。正如这里使用的那样,正交意味着一个概念既不会暗示也不会排斥其他概念。

org / wiki / Object-oriented_programmingrel =noreferrer>面向对象编程和函数式编程不相互矛盾。面向对象并不意味着可变性。许多被介绍给面向对象程序的人通常首先使用C ++,Java,C#或类似的语言,这些语言在可变性是常见的甚至是鼓励的情况下(标准库提供了供人们使用的可变类的变种)。因此,许多人将面向对象编程与命令式编程和可变性联系在一起是可以理解的,因为这是他们如何学习它的。

然而,面向对象的编程涵盖以下主题: / b>


  • 封装

  • 多态

  • 抽象



这并不意味着可变性,也不排除函数式编程。所以是的,他们是正交的,因为他们是不同的概念。他们不是相反的 - 你可以使用一种或另一种,或者两者兼用(甚至两者都不)。 Scala和F#等语言试图将这两种模式结合成一种语言:


Scala是一种多范式编程语言, 面向对象编程和函数式编程

来源


F#是一个简洁,和高效的功能面向对象的语言,它可以帮助您编写简单的代码来解决复杂的问题。

来源


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 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?

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.

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:

  • Encapsulation
  • Polymorphism
  • Abstraction

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 is a multi-paradigm programming language designed to integrate features of object-oriented programming and functional programming.

Source

F# is a succinct, expressive and efficient functional and object-oriented language for .NET which helps you write simple code to solve complex problems.

Source

这篇关于FP和OO是正交的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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