Here is a quick scalafiddle that runs the code from 2 years ago :)

https://scalafiddle.io/sf/xaGO6Yr/0

On Mon, Nov 5, 2018 at 8:18 AM Martynas Mickevičius <2...@lightbend.com>
wrote:

> Did you send any elements to the stream? internalCounter is incremented
> in the map operator for every incoming stream element.
>
> Trait is only used to hide the implementation details of the counter.
>
> On Thu, Nov 1, 2018 at 12:04 AM <jurgis.p...@dataqube.de> wrote:
>
>> Hi Rafal,
>>
>> I just stumbled upon your reply. Could you explain why you have to wrap
>> the AtomicLong into the Counter trait? I tried returning the naked
>> AtomicLong in the 'mapMaterializedValue' function, but the resulting value
>> was always 0. Why does wrapping in a trait produce a different result?
>>
>> Best,
>> Jurgis
>>
>>
>> On Sunday, March 6, 2016 at 1:46:12 AM UTC+1, Rafał Krzewski wrote:
>>>
>>> Hi,
>>> there are a few ways of doing that. Probably the simplest one is using
>>> Flow.mapMaterializedValue. Suppose you'd like to create a Flow that counts
>>> the elements that pass through it and makes the current count available
>>> through a "side channel":
>>>
>>>   trait Counter {
>>>     def get: Long
>>>   }
>>>
>>>   def counter[T]: Flow[T, T, Counter] = {
>>>     val internalCounter = new AtomicLong(0)
>>>     Flow[T].map{ elem ⇒
>>>       internalCounter.incrementAndGet()
>>>       elem
>>>      }.mapMaterializedValue(_ ⇒ new Counter{
>>>        override def get = internalCounter.get
>>>      })
>>>   }
>>>
>>> Another way is using a GraphStageWithMaterializedValue while building a
>>> custom Flow / Sink / Source. Instead of returning a GraphStageLogic,
>>> like an ordinary GraphStage, you return a pair of GraphStageLogic and
>>> the materialized value.
>>>
>>> Cheers,
>>> Rafał
>>>
>>> W dniu niedziela, 6 marca 2016 01:02:56 UTC+1 użytkownik Arun Sethia
>>> napisał:
>>>>
>>>> Hi,
>>>>
>>>> can some explain what does it mean of materialized value ? I have see
>>>> documentation at
>>>> http://doc.akka.io/docs/akka-stream-and-http-experimental/2.0.3/scala/stream-quickstart.html#transforming-and-consuming-simple-streams
>>>>
>>>> I am not sure how Flow can define materialize type, for example the
>>>> following code has Input - Tweet, output - Int but Mat is Unit. I would
>>>> like to see how someone can define Mat as Int or any example where Flow or
>>>> source is defining Mat other than Unit.
>>>>
>>>>
>>>>    - val count: Flow[Tweet, Int, Unit] = Flow[Tweet].map(_ => 1)
>>>>
>>>>
>>>>
>>>> It is quite confusing for me to understand difference between "out"
>>>> and "Mat".
>>>>
>>>>
>>>> Thanks
>>>>
>>>> As
>>>>
>>>> --
>>
>> *****************************************************************************************************
>> ** New discussion forum: https://discuss.akka.io/ replacing akka-user
>> google-group soon.
>> ** This group will soon be put into read-only mode, and replaced by
>> discuss.akka.io
>> ** More details:
>> https://akka.io/blog/news/2018/03/13/discuss.akka.io-announced
>>
>> *****************************************************************************************************
>> >>>>>>>>>>
>> >>>>>>>>>> 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+unsubscr...@googlegroups.com.
>> To post to this group, send email to akka-user@googlegroups.com.
>> Visit this group at https://groups.google.com/group/akka-user.
>> For more options, visit https://groups.google.com/d/optout.
>>
>

-- 
*****************************************************************************************************
** New discussion forum: https://discuss.akka.io/ replacing akka-user 
google-group soon.
** This group will soon be put into read-only mode, and replaced by 
discuss.akka.io
** More details: https://akka.io/blog/news/2018/03/13/discuss.akka.io-announced
*****************************************************************************************************
>>>>>>>>>> 
>>>>>>>>>>      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+unsubscr...@googlegroups.com.
To post to this group, send email to akka-user@googlegroups.com.
Visit this group at https://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.

Reply via email to