scanIndexed()

Perform a scan over every element in the input stream along with its index

Implementation Notes

Performs an ordered scan over the input stream, along with the index of the element being scanned/accumulated. This implementation attempts to behave like the non-indexing Gatherers::scan()in the JDK. For a folding version of this Gatherer, see foldIndexed().

Signature

scanIndexed(Supplier<OUTPUT> initialValue, IndexedAccumulatorFunction<OUTPUT, INPUT, OUTPUT> scanFunction)

  • initialValue - A non-null Supplier to provide the seed value for the scan (this implementation does not assume the first element is the seed)
  • scanFunction - IndexedAccumulatorFunction is a variation on a BiFunction which injects the long index of each element. This function does the actual scan/accumulation work.

Examples

Perform an indexed scan

This example joins the index and the input strings to show how this works.

Stream
    .of("A", "B", "C");
    .gather(
        Gatherers4j.scanIndexed(
            () -> "",
            (index, carry, next) -> carry + next + index
        )
    )
    .toList();

// [ "A0", "A0B1", "A0B1C2" ]