weixin_39885166 2020-11-29 20:19
浏览 0

Add indexed methods to Stream to simplify using indexes

I have these calls in a stream because I need to map a value based on the index and the value:

        .indexed()
        .map(p -> getPredecessorForRow(p.getFirst()).pathTo(p.getFirst(), p.getSecond()))

which is very cumbersome having to deal with IntPair and not very readable.

If you look at Sequence in Kotlin (which is the equivalent of a Stream) it has Indexed versions for filter, map, forEach, and reduce (aka fold). It would be nice to have these in this library.

These would depend on some new interfaces that take the index as a parameter along with the value.

With a mapIndexed function the above becomes:

        .mapIndexed((i, value) -> getPredecessorForRow(i).pathTo(i, value))

FYI, while looking at Sequence in Kotlin feel free to look for other functions that would be good candidates there for inclusion.

该提问来源于开源项目:aNNiMON/Lightweight-Stream-API

  • 写回答

0条回答 默认 最新

    报告相同问题?