Akka Streams - Source.map vs Source.via(flow)

1,191 views
Skip to first unread message

Ori Popowski

unread,
Sep 5, 2016, 3:32:37 AM9/5/16
to Akka User List
Hi,

It may seem like a trivial question, but I'm a bit confused about use cases of Source.map vs. Source.via(flow).

For example, these two flows produce the exact same output:

    val flow1 = Source(1 to 10).map(_ * 2).to(Sink.foreach(println))
    val flow2
= Source(1 to 10).via(Flow[Int].map(_ * 2)).to(Sink.foreach(println))

so why would anyone express this functionality with flow2?

Also, in the example given in this blog post, the first way (not using Flows) is used exclusively.

Thanks.

Endre Varga

unread,
Sep 5, 2016, 3:35:10 AM9/5/16
to akka...@googlegroups.com
Hi Ori,

The reason is if you have longer, reusable chains of computations then you can factor them out into a Flow. For example if you have a more complex chain handling some deserialization and domain object mapping then you can express it once and then just include it multiple times into other streams. For a simple map this is an overkill of course (btw .map() is converted to a .via() without you seeing it ;) )

-Endre

--
>>>>>>>>>> Read the docs: http://akka.io/docs/
>>>>>>>>>> Check the FAQ: http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user
---
You received this message because you are subscribed to the Google Groups "Akka User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to akka-user+unsubscribe@googlegroups.com.
To post to this group, send email to akka...@googlegroups.com.
Visit this group at https://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.

Ori Popowski

unread,
Sep 17, 2016, 7:29:47 AM9/17/16
to Akka User List
Hi,

I didn't realize that maps underlying implementation is using via. Should have definitely dug into the code more :)

Thanks, it cleared that issue for me.



On Monday, September 5, 2016 at 10:35:10 AM UTC+3, drewhk wrote:
Hi Ori,

The reason is if you have longer, reusable chains of computations then you can factor them out into a Flow. For example if you have a more complex chain handling some deserialization and domain object mapping then you can express it once and then just include it multiple times into other streams. For a simple map this is an overkill of course (btw .map() is converted to a .via() without you seeing it ;) )

-Endre
On Mon, Sep 5, 2016 at 9:30 AM, Ori Popowski <ori...@gmail.com> wrote:
Hi,

It may seem like a trivial question, but I'm a bit confused about use cases of Source.map vs. Source.via(flow).

For example, these two flows produce the exact same output:

    val flow1 = Source(1 to 10).map(_ * 2).to(Sink.foreach(println))
    val flow2
= Source(1 to 10).via(Flow[Int].map(_ * 2)).to(Sink.foreach(println))

so why would anyone express this functionality with flow2?

Also, in the example given in this blog post, the first way (not using Flows) is used exclusively.

Thanks.

--
>>>>>>>>>> Read the docs: http://akka.io/docs/
>>>>>>>>>> Check the FAQ: http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user
---
You received this message because you are subscribed to the Google Groups "Akka User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to akka-user+...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages