map operation transforms elements that are going from the output port. Since both
Source and a
Flow has an output port, map operation is available on both of these types.
You might find the Akka Stream documentation page on the “Modularity, Composition and Hierarchy” interesting.
When it comes to organizing code, both of the definitions of
source2 define the same
val source1 = Source.single(1).map(_.toString)
val flow = Flow[Int].map(_.toString)
val source2 = Source.single(1).via(flow)
If the transformation of the elements is to be reused in other stream definitions, then it makes sense to have it as a separate reusable
Flow. Otherwise defining transformation directly on the
Source is fine.