Lazy flatMap implementation algorithm in java 10

Viewed 596

I know java streams, and tried to implement the map, filter, fold (with custom function as argument), both the strict and lazy evaluation ways.

However i could not implement a lazy implementation of flatmap in java. Normal map,filter, fold are just composed functions which run on the main iterator (if its list) and apply of functions is discarded if the incoming value is null.

However flatMap input function produces another list( stream) which needs to be flattened,

How is the lazy flatMap implemented in java 10? is there any document on the algorithm?

Thanks.

1 Answers

If you want to implement lazy flatMap, the most important part is to provide a correct implementation of Iterator. This implementation can look like this:

final class FlatMappedIterator<A, B> implements Iterator<B> {
    private final Iterator<A> iterator;
    private final Function<A, Iterable<B>> f;
    private Iterator<B> targetIterator; // Iterator after applying `f` to element of type A

    FlatMappedIterator(Iterator<A> iterator, Function<A, Iterable<B>> f) {
        this.iterator = iterator;
        this.f = f;
    }

    @Override
    public boolean hasNext() {
        if (targetIterator != null && targetIterator.hasNext()) {
            return true;
        } else if (iterator.hasNext()) {
            A next = iterator.next();
            Iterable<B> targetIterable = f.apply(next);
            targetIterator = targetIterable.iterator();
            return targetIterator.hasNext();
        } else {
            return false;
        }
    }

    @Override
    public B next() {
        if (hasNext()) {
            return targetIterator.next();
        } else {
            throw new NoSuchElementException();
        }
    }
}

So the retrieval of the next element is postponed to the moment when hasNext or next is called.

Then you need to implement the flatMap function itself. But this is easy. I'm leaving it as an exercise for the reader :)

Related