简体   繁体   English

Java 8 提供函数式风格但不是函数式编程的说法是否属实?

[英]Is this statement true that Java 8 provides functional style but is not functional programming?

Is this statement true that Java 8 provides functional style but is not functional programming as the syntax which it uses is also an object? Java 8 提供函数式风格但不是函数式编程,因为它使用的语法也是 object,这句话是否属实?

 Calculator calc = (i, j) ->  i/j;

If yes, then why we get articles everywhere Functional Programming with Java 8?如果是的话,那为什么我们到处都能看到使用 Java 8 进行函数式编程的文章?

Here is a (non-exhaustive) list of abstract FP concepts:以下是抽象 FP 概念的(非详尽的)列表:

  1. Focus on immutability专注于不变性
  2. Referential transparency for functions函数的引用透明度
  3. Limitations on side effects (follows from 1 & 2)副作用的限制(遵循 1 和 2)
  4. Expression-based, no statements.基于表达式,无语句。 Statements are not first-class.陈述不是一流的。
  5. Functions as first-class values.函数作为一等值。
  6. State changes as first-class values (eg Clojure atoms). State 更改为一等值(例如 Clojure 原子)。
  7. Algebraic Data Types as fundamental units.代数数据类型作为基本单位。
  8. Enforcing some or all of the above via the type system.通过类型系统执行上述部分或全部。

And I could go on.我可以打开 go。 A language doesn't have to tick every box on the list to be a "Functional Programming Language" (I can't actually think of any that meet all of those bullet points), but the more boxes it ticks the less qualification you have to give to give it that label.一种语言不必勾选列表中的每个框才能成为“函数式编程语言”(我实际上想不出任何符合所有这些要点的语言),但是它勾选的框越多,您拥有的资格就越少给给它label。 And that's why Java doesn't qualify in the eyes of many functional programmers: it just doesn't check off very many (arguably only one) of the above items.这就是为什么 Java 在许多函数式程序员眼中不合格的原因:它只是没有检查上述项目中的很多(可以说只有一项)。

Which is not to say that you cannot do functional programming in Java, but it's hard.这并不是说你不能在 Java 中进行函数式编程,但这很难。 The language gives you too few of the tools you need to work with, meaning you'll have to write a lot of boilerplate to encode the necessary primitives as classes, meaning your code will be slower and harder to follow (and that's before you get into the issue of 15 different competing ad-hoc library implementations of those primitives).该语言为您提供的工具太少,这意味着您必须编写大量样板来将必要的原语编码为类,这意味着您的代码将变得更慢且更难遵循(这是在您获得这些原语的 15 个不同的竞争临时库实现的问题)。

You can treat your objects as immutable, favor expressions over statements (like using the ternary operator instead of if/else), make most of your methods pure functions, go hog-wild with lambdas, etc. but at the end of the day there's still a lot of friction, both with the language and the expectations of the community.您可以将您的对象视为不可变的,优先使用表达式而不是语句(例如使用三元运算符而不是 if/else),使您的大多数方法成为纯函数,go 带有 lambdas 等,但最终有仍然有很多摩擦,无论是语言还是社区的期望。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM