Skip to content

Commit d353400

Browse files
committed
added safe versions of minimum and maximum for List
1 parent e95182a commit d353400

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

core/src/main/java/fj/data/List.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1465,6 +1465,16 @@ public final A maximum(final Ord<A> o) {
14651465
return foldLeft1(o::max);
14661466
}
14671467

1468+
/**
1469+
* Returns the maximum element in this list according to the given ordering.
1470+
*
1471+
* @param o An ordering for the elements of the list.
1472+
* @return The optional maximum element in this list according to the given ordering.
1473+
*/
1474+
public final Option<A> maximumOption(final Ord<A> o) {
1475+
return headOption().map(x -> maximum(o));
1476+
}
1477+
14681478
/**
14691479
* Returns the minimum element in this list according to the given ordering.
14701480
*
@@ -1474,6 +1484,16 @@ public final A maximum(final Ord<A> o) {
14741484
public final A minimum(final Ord<A> o) {
14751485
return foldLeft1(o::min);
14761486
}
1487+
1488+
/**
1489+
* Returns the minimum element in this list according to the given ordering.
1490+
*
1491+
* @param o An ordering for the elements of the list.
1492+
* @return The optional minimum element in this list according to the given ordering.
1493+
*/
1494+
public final Option<A> minimumOption(final Ord<A> o) {
1495+
return headOption().map(x -> minimum(o));
1496+
}
14771497

14781498
public final java.util.List<A> toJavaList() {
14791499
return new java.util.LinkedList<>(toCollection());

0 commit comments

Comments
 (0)