我已经用Java进行了一段时间的函数式编程实验,并注意到我开始更喜欢使用来自java.util.function包的java.util.function函数,例如函数、BiFunctions、UnaryOperators、谓词、BiPredicates等等,而不是在我的类中使用简单的私有方法。我知道,它们的应用程序更适合作为参数传递给另一个函数,这也是我通常倾向于使用它们的方式,但现在我发现它们是即时的,而且在某种程度上更好。
实际上,我现在倾向于将其中一些变量声明为变量,以便在需要时在类中使用。
我似乎没有找到任何关于使用这些方法的指南或缺点,而不是简单的方法。
那么:这样做有缺点吗?
为什么更喜欢:
private boolean foo(final int a, final int b){
return a < b;
}而不是:
private final BiPredicate<Integer,Integer> foo = (a,b) -> a < b;我倾向于从最近的项目中使用它们的一个例子:
private final BiFunction<BoardPosition, Pair<Integer, Integer>, BoardPosition> sumBoardPosWithPair = (pos,
pair) -> new BoardPositionImpl(pos.getX() + pair.getX(), pos.getY() + pair.getY());
private final Function<Pair<Integer, Integer>, UnaryOperator<BoardPosition>> unaryCreator = (
axis) -> (p) -> this.sumBoardPosWithPair.apply(p, axis);
/**
* If you need to call the fromFunction method twice for specular directions use
* this TriFunction specularNoLimitDirection instead.
*/
private final TriFunction<Piece, Vectors, Board, Set<BoardPosition>> specularNoLimitDirection = (piece, axis,
board) -> Stream.concat(
this.fromFunction(this.unaryCreator.apply(axis.getAxis()), piece, board,
board.getColumns() + board.getRows()).stream(),
this.fromFunction(this.unaryCreator.apply(axis.getOpposite()), piece, board,
board.getColumns() + board.getRows()).stream())
.collect(Collectors.toSet());
protected final Set<BoardPosition> fromFunction(final UnaryOperator<BoardPosition> function, final Piece piece,
final Board board, final int limit) {
/*
* The "function.apply" at the seed of the Stream.Iterate is used to skip the
* first element, that's itself, in fact a piece can't have as a possible move
* it's original position.
*/
final List<BoardPosition> positions = Stream.iterate(function.apply(piece.getPiecePosition()), function)
.takeWhile(board::contains)
.takeWhile(x -> board.getPieceAtPosition(x).isEmpty()
|| !board.getPieceAtPosition(x).get().getPlayer().equals(piece.getPlayer()))
.limit(limit).collect(Collectors.toList());
final Optional<BoardPosition> pos = positions.stream().filter(i -> board.getPieceAtPosition(i).isPresent()
&& !board.getPieceAtPosition(i).get().getPlayer().equals(piece.getPlayer())).findFirst();
/*
* The sublist excludes the last n-th element of the high-endpoint, for this
* reason we need to add 1.
*/
return pos.isEmpty() ? new HashSet<>(positions)
: new HashSet<>(positions.subList(0, positions.indexOf(pos.get()) + SINGLE_INCREMENT));
}发布于 2021-03-24 23:58:21
您应该做最易读和最可维护的事情。如果将这些函数放入带有描述性名称的变量中,感觉是解决问题的一种可读性和可维护性的方法,那就好了。像这样的编程没有什么问题。
您可能也喜欢的一个中间点是将逻辑放在普通的静态方法中:
private boolean foo(final int a, final int b){
return a < b;
}然后在需要时使用方法引用:MyClass::foo来引用它。这在行为上等同于您定义的lambda。
编写这段代码有很多方法。每个人都对“正确”的方法有自己的看法,但在现实中有很多“正确”的方法。(还有一些不太合适的方式。)
发布于 2021-03-24 23:53:59
正如我们所知道的,Java是一种相当古老的编程语言(在某种程度上),当涉及到可供选择的编程语言时,它具有定期更新的功能。尽管如此,您可能已经阅读了解释接口的旧文档。
private boolean foo(final int a, final int b){
return a < b;}
是一个有效的函数,在文档和学习接口的基本原理方面,您可以在其中看到很多。
现在,您正在将“旧”方法与处理方法/接口的“较新”方法进行比较。随着Java 8的发布,他们增加了lambdas,我认为这是让程序员能够用“较少的”行和更快的方式(除其他外)编写方法。您希望您的编程语言在安全性方面是最新的,但也与其他语言不相上下。与Python相比,Java是一种笨拙的编程语言。
private final BiPredicate<Integer,Integer> foo = (a,b) -> a < b;这也是一个可以接受的方法/函数,可以用Java 8和任何更新的方法/函数编写。用这种方式编写函数是没有问题的,它可能会导致更快地编写代码。
用这种“更新”的方式编写方法的唯一问题是
没有任何问题,“旧”的方式是首选的,只有当你是一个团队说:“不,你不能使用兰巴达”。没有什么不同。
最后,您可能会发现与匿名类相比,lambda速度的基准测试很有趣,这可以找到这里。
编辑我想添加到只写lambda的简单函数,我知道它们非常强大,但是编写lambda来搜索一个列表,那么常规的for循环是可以的,但是复杂的方法应该以一种清晰的方式编写。这只是为了避免调试程序时的头痛。
https://stackoverflow.com/questions/66790741
复制相似问题