Code Monkey home page Code Monkey logo

Comments (5)

vxgmichel avatar vxgmichel commented on September 16, 2024

Just thought I'd share some ideas for streams that would be useful to me.

Great, thanks!

A ziplast stream

That would be the equivalent of ReactiveX combineLatest right?

Here's a possible implementation:

@operator(pipable=True)
def ziplatest(*sources):
    n = len(sources)

    # Add source index to the items
    new_sources = [
        stream.map(source, lambda x, i=i: {i: x})
        for i, source in enumerate(sources)]

    # Merge the sources
    merged = stream.merge.raw(*new_sources)

    # Accumulate the current state in a dict
    accumulated = stream.accumulate.raw(
        merged, lambda x, e: {**x, **e})

    # Convert the state dict to a tuple
    return stream.map.raw(
        accumulated, lambda x: tuple(map(x.get, range(n))))

And an example:

async def main():
    xs = stream.count(interval=2) | pipe.delay(2)
    ys = stream.count(interval=5) | pipe.delay(5)
    zs = xs | ziplatest.pipe(ys)
    ps = zs | pipe.print()
    await ps

With the corresponding output:

(0, None)
(1, None)
(1, 0)
(2, 0)
(3, 0)
(3, 1)
(4, 1)
(5, 1)
(6, 1)
(6, 2)
(7, 2)
[...]

I guess extra keywords arguments could also be provided, either to disable the producing of incomplete items, or to define a custom default value.

What do you think?

from aiostream.

andersea avatar andersea commented on September 16, 2024

Awesome! That is exactly what I need.

from aiostream.

vxgmichel avatar vxgmichel commented on September 16, 2024

Great! I don't really have the time to write the docs and tests for this feature at the moment, so feel free to submit a PR if you're interested. Otherwise, I'll take care of it later.

from aiostream.

andersea avatar andersea commented on September 16, 2024

Np, thanks for the effort. I really like this library and async generator functions is a pretty nice new python feature. It really cleans up a lot of my previous async context manager code in the project I am currently working at. You get a feeling that async/await is still a pretty young feature in python and there are definitely a few pitfalls to dodge here and there.

from aiostream.

vxgmichel avatar vxgmichel commented on September 16, 2024

Hi @andersea,

I just added the ziplatest operator (see PR #22), it will become available with version 0.3.1.

Thanks for the suggestion :)

from aiostream.

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.