Re: [elixir-core:4345] Retrieving accumulator from Stream.resource after execution

27 views
Skip to first unread message

José Valim

unread,
Aug 30, 2015, 4:04:41 AM8/30/15
to elixir-l...@googlegroups.com
Sean, I think you noticed your mistake, that's why you posted to elixir-lang-talk later, but as a reminder to everyone, please don't use the core mailing list for questions. :)



José Valim
Skype: jv.ptec
Founder and Director of R&D

On Sun, Aug 30, 2015 at 8:50 AM, Sean Stavropoulos <sean....@gmail.com> wrote:
I am attempting to use Stream.resource with an accumulator, and I cannot find a way to retrieve the accumulator after execution has finished.

defmodule DummyExternalResource do
  defstruct range
: nil,
           
done: false,
            something_important
: nil

 
def stream(external_resource) do
   
Stream.resource(
      fn
-> external_resource end,
      fn  
(resource=%__MODULE__{done: true}) -> {:halt, resource}
         
(resource) ->
            resource
= %__MODULE__{resource|done: true, something_important: 1234}
           
{Enum.to_list(resource.range), resource}
         
end,
      fn
(resource) -> resource end
   
)
 
end
end

%DummyExternalResource{range: 1..25}
|> DummyExternalResource.stream
|> Stream.each(fn(a) -> IO.puts("Do something with a side effect...") end)
|> Stream.run #=> :ok

# Goal: Use lazy stream behavior, but retrieve the :something_important key after the stream is over.

From the example above, I would like to execute a function on each emitted record, which is working fine.. But how can I get the "something_important" key back out after the stream has finished?

Thanks,

Sean

--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/ae9f8680-a15b-480c-91bb-bd259dfa5360%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

eksperimental

unread,
Aug 30, 2015, 9:34:51 AM8/30/15
to elixir-l...@googlegroups.com
Jose, one advice that I can give you is to set the list as
premoderated,
There's in an option that new users message need to be aproved.
Once the user has been trusted (usually after their first message is
aproved), you don't need to aprove any more message from this user.

I think most of the people who send off-topic messages fall into this
category.



On Sun, 30 Aug 2015 10:04:20 +0200
José Valim <jose....@plataformatec.com.br> wrote:

> Sean, I think you noticed your mistake, that's why you posted to
> elixir-lang-talk later, but as a reminder to everyone, please don't
> use the core mailing list for questions. :)
>
>
>
> *José Valim*
> > <https://groups.google.com/d/msgid/elixir-lang-core/ae9f8680-a15b-480c-91bb-bd259dfa5360%40googlegroups.com?utm_medium=email&utm_source=footer>
> > .
Reply all
Reply to author
Forward
0 new messages