RFR: JDK-8319123 : Implementation of JEP-461: Stream Gatherers (Preview)
Tagir F. Valeev
tvaleev at openjdk.org
Wed Nov 8 16:42:08 UTC 2023
On Mon, 6 Nov 2023 16:24:12 GMT, Viktor Klang <vklang at openjdk.org> wrote:
>> src/java.base/share/classes/java/util/stream/Gatherers.java line 450:
>>
>>> 448: * @throws NullPointerException if any of the parameters are null
>>> 449: */
>>> 450: public static <T, R> Gatherer<T, ?, R> fold(
>>
>> Should it be explicitly named as `foldLeft`? Because `foldRight` is also possible (though will require complete stream buffering). Also see [JDK-8133680](https://bugs.openjdk.org/browse/JDK-8133680) and [JDK-8292845](https://bugs.openjdk.org/browse/JDK-8292845) (probably should be linked to gatherer's issue, and read the comment about naming)
>
> TBH I don't think `foldRight` makes much sense for potentially unbounded structures such as Stream. In the case you need it, it might be better to export it to a List and then reversing it.
Well, the unboundness argument does not hold, as it can be equally applied to any operation except the short-circuiting ones. Yes, this includes your `fold()`. The only difference is that `foldRight()` will fail with OutOfMemoryError while `fold()` will be stuck in the endless loop. Or probably also fail depending on the folder function (for example, if it's a string concatenation, like in the Javadoc example). You may not like the fact the `foldRight()` buffers the whole stream content, but we already have operations like this, namely `sorted()` and parallel ordered `distinct()`.
I'm not convincing you to add `foldRight()` to the JDK. But if we don't have it, somebody will surely write it in third-party libraries. After all, this is what third-party libraries are for. In this case, simple name `fold()` can be confusing.
Note that Haskell has separate foldl, foldr, scanl, scanr, so the order is always clear. Scala has `fold()` (which is like `reduce()` and can fold in any order), `foldLeft()` and `foldRight()`. Naming this method simply `fold()` sounds quite confusing. It should be either `foldLeft()` or `reduceLeft()` (even if we don't provide the -right versions)
>> src/java.base/share/classes/java/util/stream/Gatherers.java line 482:
>>
>>> 480: * @throws NullPointerException if any of the parameters are null
>>> 481: */
>>> 482: public static <T, R> Gatherer<T, ?, R> scan(
>>
>> Similarly, probably should be named `scanLeft`?
>
> That's a good question, and here's my thinking—`scanRight` doesn't make any sense for a construct which supports unboundedness, so if we were discussing *Collections* I'd be more inclined to agree (but it is more likely that reversing the collection and then calling scanLeft would be better).
See the previous comment. Even if we don't provide `scanRight` for symmetry, the name `scanLeft` for this method would be clearer. As an alternative, consider the `prefix` name, as we already have `Arrays.parallelPrefix`, which performs the same operation on arrays.
-------------
PR Review Comment: https://git.openjdk.org/jdk/pull/16420#discussion_r1386910684
PR Review Comment: https://git.openjdk.org/jdk/pull/16420#discussion_r1386913105
More information about the core-libs-dev
mailing list