Closed gzsombor closed 3 years ago
I'm not actually sure we need something like this. Indexes play not very nice with streams, as they should be counted through the whole stream (imagine you have upstream flatMap or filter), so this creates the global stream state, and it cannot be parallelized nicely. If you need indexes for List or array, you can use EntryStream.of(arrayOrList).mapKeyValue((index, element) -> ...)
, and this should be more parallel-friendly. Otherwisely, I would prefer having zip operation explicit.
Thanks, I haven't noticed this EntryStream.of(arrayOrList) trick, and it's nearly good for my use case. Unfortunately, I have a couple of filter/flatMap between my list and the indexing function. The original code was something like this:
int lineNo = 1;
for (MyObj obj: list) {
var lines = myObj.getChildren().stream().filter(...).toList();
for (Line line : lines) {
....
line.setNumber(lineNo++);
}
}
Not the best, and certainly not the most common problem, and in the end, my solution with the zipWith(IntStreamEx.ints()...) didn't look too nice as well. It hided the intent, that we wanted to count the lines, and put these line numbers in the objects itself - so I've created a static helper function, which made it a little bit better, but still ehh. So that's why I forked this repo, to put on the original class.
It's not a very complex function, but I think, this could be a nice addition, I definitely missed from Kotlin's collections