Java 8提供了功能样式但不是功能编程,这种说法是正确的吗? [英] Is this statement true that Java 8 provides functional style but is not functional programming?

查看:61
本文介绍了Java 8提供了功能样式但不是功能编程,这种说法是正确的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这句话是对的吗?Java 8提供了功能样式,但不是功能编程,因为它使用的语法也是对象?

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?

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

如果是,那为什么我们到处都可以看到用Java 8进行函数式编程的文章?

If yes, then why we get articles everywhere Functional Programming with Java 8?

推荐答案

以下是(非详尽的)抽象FP概念的列表:

Here is a (non-exhaustive) list of abstract FP concepts:

  1. 关注不变性
  2. 功能的参照透明性
  3. 对副作用的限制(遵循1和2)
  4. 基于表达式,无语句.语句不是一流的.
  5. 用作一等值.
  6. 状态变化是一等值(例如Clojure原子).
  7. 以代数数据类型为基本单位.
  8. 通过类型系统实施上述部分或全部操作.

我可以继续.一种语言不必在列表上的每个方框中打勾就可以成为功能编程语言"(我想不出任何符合这些要点的 all 的语言),但是更多的方框它会打勾您必须给它的标签的较少资格.这就是为什么Java在许多函数式程序员看来都不合格的原因:它只是没有核对上述项目中的很多(可以说只有一个).

And I could go on. 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. 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进行功能编程,但这很难.该语言为您提供了所需的工具,这意味着您必须编写大量样板文件才能将必要的原语编码为类,这意味着您的代码将变得越来越慢且难于遵循(这是在获得之前).这些原语的15种不同的竞争性临时库实现问题).

Which is not to say that you cannot do functional programming in Java, but it's hard. 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).

您可以将您的对象视为不可变的,在语句中使用表达式(例如,使用三元运算符而不是if/else),使大多数方法成为纯函数,对lambdas进行疯狂处理,等等,但是在末尾那天,在语言和社区期望方面仍然存在很多摩擦.

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.

这篇关于Java 8提供了功能样式但不是功能编程,这种说法是正确的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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