Skip to content

Commit 6ac5f95

Browse files
shimibjbgi
authored andcommitted
Add NEL.minimum/maximum and safe versions of minimum/maximum for List
* added minimum/maximum on NEL && changed impl of minimumOption/maximumOption in List to use it * spaces for tabs
1 parent e95182a commit 6ac5f95

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-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 NonEmptyList.fromList(this).map(nel -> nel.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 NonEmptyList.fromList(this).map(nel -> nel.minimum(o));
1496+
}
14771497

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

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,28 @@ public NonEmptyList<A> sort(final Ord<A> o) {
226226
return nel(list.head(), list.tail());
227227
}
228228

229+
230+
/**
231+
* Returns the minimum element in this non empty list according to the given ordering.
232+
*
233+
* @param o An ordering for the elements of this non empty list.
234+
* @return The minimum element in this list according to the given ordering.
235+
*/
236+
public final A minimum(final Ord<A> o) {
237+
return foldLeft1(o::min);
238+
}
239+
240+
/**
241+
* Returns the maximum element in this non empty list according to the given ordering.
242+
*
243+
* @param o An ordering for the elements of this non empty list.
244+
* @return The maximum element in this list according to the given ordering.
245+
*/
246+
public final A maximum(final Ord<A> o) {
247+
return foldLeft1(o::max);
248+
}
249+
250+
229251
/**
230252
* Zips this non empty list with the given non empty list to produce a list of pairs. If this list and the given list
231253
* have different lengths, then the longer list is normalised so this function never fails.

0 commit comments

Comments
 (0)