Coder Social home page Coder Social logo

Comments (8)

aNNiMON avatar aNNiMON commented on September 26, 2024

I think, it is impossible in Stream API. In rxjava we can compare items in two Observables according to the emit time, but in Stream API there is no time. If two Streams has items, we can take them at any time.

from lightweight-stream-api.

dalewking avatar dalewking commented on September 26, 2024

Sorry, linked to the wrong takeUntil method. This is more a variation on takeWhile:

http://reactivex.io/RxJava/javadoc/rx/Observable.html#takeUntil(rx.functions.Func1)

from lightweight-stream-api.

dalewking avatar dalewking commented on September 26, 2024

Haven't tested it but I think this would be a working implementation.

public Stream<T> takeUntil(final Predicate<? super T> predicate) {
    return new Stream<T>(new LsaExtIterator<T>() {

        @Override
        protected void nextIteration() {
            hasNext = iterator.hasNext() && !(isInit && predicate.test(next));
            if(hasNext)
            {
                next = iterator.next();
            }
        }
    });
}

from lightweight-stream-api.

dalewking avatar dalewking commented on September 26, 2024

So as an example:

IntStream.range(0, 10).takeUntil(x -> x > 3)

Would be a stream with 0, 1, 2, 3, 4

from lightweight-stream-api.

aNNiMON avatar aNNiMON commented on September 26, 2024

@dalewking ok, this possible. Thanks!

from lightweight-stream-api.

dalewking avatar dalewking commented on September 26, 2024

Refactored version:

public Stream<T> takeUntil(final Predicate<? super T> predicate) {
    return new Stream<T>(new LsaExtIterator<T>() {

        @Override
        protected void nextIteration() {
            hasNext = iterator.hasNext() && !(isInit && predicate.test(next));
            next = hasNext ? iterator.next() : null;
        }
    });
}

Setting next to null is so we don't keep a reference to the last item which we no longer need.

from lightweight-stream-api.

dalewking avatar dalewking commented on September 26, 2024

Your fix does not release the reference to the last item emitted when it is no longer needed.

The null part of this:

next = hasNext ? iterator.next() : null;

from lightweight-stream-api.

aNNiMON avatar aNNiMON commented on September 26, 2024

That's my fault. Thank you so much!

from lightweight-stream-api.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.