scala 混合面向对象和函数式编程

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1015606/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 01:28:00  来源:igfitidea点击:

Mixing object-oriented and functional programming

scalafunctional-programmingimperative-programming

提问by Kai

What languages are available that promote both object-oriented and functional programming? I know that any language that supports first-class functions can be considered functional, but I'm looking for a syntax that's specifically targeted for both coding styles.

哪些语言可以同时促进面向对象和函数式编程?我知道任何支持一流函数的语言都可以被认为是函数式的,但我正在寻找一种专门针对这两种编码风格的语法。

Using such a language, I'm imagining isolating all state changes to a single portion of code and having the rest of the program be purely functional. Just thinking of it makes me drool (debugging heaven!).

使用这种语言,我想将所有状态更改隔离到单个代码部分,并使程序的其余部分完全是功能性的。想想就让我流口水(调试天堂!)。

So far I've discovered Scalaalthough I've only just heard of it (and it looks amazing). Are there any big contenders in this "mixed style" paradigm?

到目前为止,我已经发现了Scala,尽管我只是听说过它(而且它看起来很棒)。在这种“混合风格”范式中是否有任何大的竞争者?

采纳答案by Igor Krivokon

The best known are OCamland F#(which can be vaguely described as OCaml for .NET).

最著名的是OCamlF#(可以模糊地描述为 OCaml for .NET)。

There are many other multi-paradigm languages, like Oz, but they have mostly pedagogical value. By contrast, OCaml is very practical. It's almost as fast as C and almost as beautiful as Haskell :)

还有许多其他多范式语言,例如Oz,但它们主要具有教学价值。相比之下,OCaml 非常实用。它几乎和 C 一样快,几乎和 Haskell 一样漂亮:)

The popular scripting languages like Python and Ruby let you program in functional style as well. However, they don't provide one of the strongest facilities that "classical" functional languages (as well as OCaml) have: pattern matching(don't mistake it for regexp).

流行的脚本语言(如 Python 和 Ruby)也允许您以函数式风格进行编程。但是,它们没有提供“经典”函数式语言(以及 OCaml)所具有的最强大的功能之一:模式匹配(不要将其误认为正则表达式)。

回答by Evan Meagher

Javascript: OOand functional.

Javascript:面向对象功能性

回答by Zifre

Also, many scripting languages such as Python, Ruby, Lua, etc. have this capability, but lack many of the nice features of functional languages such as algebraic data types and pattern matching.

此外,许多脚本语言,例如 Python、Ruby、Lua 等,都具有此功能,但缺乏函数式语言的许多优良特性,例如代数数据类型和模式匹配。

回答by Gordon Gustafson

Haskell: Pure functional ,pretty much no OO, but go ahead, take the dive. :D

Haskell:纯函数式,几乎没有面向对象,但是继续前进,深入了解。:D

Scala: Beautiful mix of OO and FP, could possibly overtake java as premier language on the JVM in a decade or 2. I like it because it brings functional programming to the java platform, something that's sorely need IMHO.

Scala:OO 和 FP 的完美结合,有可能在一两年内取代 Java 成为 JVM 上的主要语言。我喜欢它,因为它为 Java 平台带来了函数式编程,这是非常需要恕我直言的东西。

C#: Awesome support for OO, as well as getting more functional (first class functions already, we'll see what improvements .net 4 brings)

C#:对 OO 的出色支持,以及获得更多功能(已经是一流的功能,我们将看到 .net 4 带来了哪些改进)

F#: .net language Built to be functional specifically, as opposed to C#, which was originally conceived for OO stuff.

F#:.net 语言专为功能性而构建,与 C# 不同,C# 最初是为 OO 内容而设计的。

Python: Great for OO, but not at all suited to FP

Python:非常适合 OO,但根本不适合 FP

Javascript: Supports first-class functions, but not specifically designed for FP like Scala and F#. Still slightly better than python IMHO.

Javascript:支持一流的函数,但不是专门为 Scala 和 F# 等 FP 设计的。恕我直言,仍然比python略好。

Why do you want to mix OO and FP? As a stepping stone?

为什么要混合使用 OO 和 FP?作为垫脚石?

回答by Rayne

OCaml and F# are the most popular languages that mix OOP and FP, as far as I know.

据我所知,OCaml 和 F# 是最流行的混合 OOP 和 FP 的语言。

Most languages, like Ruby, mix functional programming in, but a lot of people don't even realize it. I find languages like that leave a lot to be desired on the syntax front and such.

大多数语言,比如 Ruby,都混合了函数式编程,但很多人甚至没有意识到这一点。我发现这样的语言在语法方面还有很多不足之处。

回答by Eugene Yokota

JavaScript, Python, and Ruby could be used that way, but Scalabumps up a notch by typing the function statically and working under JVM.

JavaScript、Python 和 Ruby 都可以这样使用,但Scala通过静态键入函数并在 JVM 下工作而提高了一个档次。

回答by Scott Wisniewski

C#. It's imperative, which can be handy, but also has a lot of functional features. Lambdas, iterators, and LINQ are all functional.

C#。它是命令式的,可以很方便,但也有很多功能特性。Lambda、迭代器和 LINQ 都是函数式的。

It probably won't appeal much to purists, but it works for me.

它可能不会对纯粹主义者有吸引力,但它对我有用。

回答by Apocalisp

You're really asking the wrong question. Your question is premised on there being a distinction between "OO" and "functional" programming. This distinction isn't interesting or relevant. In fact, according to the "supports first-class function" criteria, even Java is functional.

你真的问错了问题。您的问题的前提是“OO”和“函数式”编程之间存在区别。这种区别并不有趣或不相关。事实上,按照“支持一流功能”的标准,即使是Java也是功能性的

But you hit the nail on the head with "purely functional". That's the interesting bit. What languages offer you referential transparency and purity like that? Well, most of them, if you're very careful.But not many of them actually guarantee that your functions are pure. I can only think of a couple of languages that offer you that.

但是你用“纯粹的功能”击中了头上的钉子。这就是有趣的一点。什么语言为您提供了这样的参考透明度和纯度?好吧,他们中的大多数,如果你非常小心的话。但实际上并不能保证您的函数是纯函数。我只能想到几种可以为您提供的语言。

One of them is Haskell. In Haskell, you write programs that are purely functional from beginning to end. Side-effects are delegated to a data structure called IO, and state is handled by passing it through pure functions or encapsulating it in monads. So you have your "debugging heaven" where only a small portion of your code interacts with global state and the rest of your program is pure, and purity is enforced by the language.

其中之一是Haskell。在 Haskell 中,您编写的程序从头到尾都是纯函数式的。副作用被委托给称为 IO 的数据结构,状态通过将其传递给纯函数或将其封装在 monad 中来处理。所以你有你的“调试天堂”,其中只有一小部分代码与全局状态交互,其余程序是纯的,纯是由语言强制执行的。

回答by Svante

As long as you don't insist on "purity", Common Lisp supports all your needs.

只要您不坚持“纯粹”,Common Lisp 就可以支持您的所有需求。

回答by Toby

I'm wondering why you are looking for a language that specifically encourages mixing it up rather than just doing it anyway with a language that does functional programming and OO programming well? It could be easily brought into effect with Python, Ruby, Perl or similar interpreted languages. In addition C based OO languages tend to mix pure C with OO features, for example Objective C could easily be written in such a way should you choose.

我想知道您为什么要寻找一种特别鼓励将它混合在一起的语言,而不是仅仅使用一种能够很好地进行函数式编程和 OO 编程的语言来进行混合?它可以很容易地通过 Python、Ruby、Perl 或类似的解释语言实现。此外,基于 C 的 OO 语言倾向于将纯 C 与 OO 特性混合在一起,例如,如果您选择,Objective C 可以很容易地以这种方式编写。

EDIT: I have been made aware I'm incorrect, I've left this answer here incase someone can learn from my mistake - see comments.

编辑:我已经意识到我错了,我把这个答案留在这里以防有人可以从我的错误中吸取教训 - 请参阅评论。