[erlang-questions] Extracting pure functions
Judson Lester
nyarly@REDACTED
Fri Feb 20 20:46:49 CET 2015
I'm thinking about how to separate pure functions from those that cause
side effects, and one of the places I'm trying to find a decent pattern for
is message passing.
I'm primarily concerned with being able to easily test the functional
components as units, and then use integration testing for the parts that
involve a side effect.
For instance, I have a gen_server that handles a message and emits an event
sometimes.
handle_event_message(Arg,
State=#state{collected_args=Args,event_server=Ev}) ->
gen_event:notify(Ev, {some_event, Arg}),
State#state{collected_args[Arg |Args]}.
(obviously, I'm simplifying a lot here)
What I'm wondering about is extracting something like:
handle_event_message(Arg,
State=#state{collected_args=Args,event_server=Ev}) ->
{Effects, State} = pure_handle(Arg, State),
[ apply(Mod, Fun, Args) || {Mod, Fun, Args} <- Effects],
State.
pure_handle(Arg, State) ->
{ [{gen_event, notify, [Ev, {some_event, Arg}]}],
State#state{collected_args[Arg |Args]} }.
Which would mean I could test the effects that pure_handle/2 intends in a
unit test.
Is this a pattern others use? A terrible delusion? Not interesting?
Judson
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20150220/1432f821/attachment.htm>
More information about the erlang-questions
mailing list