[erlang-questions] custom generators?

dieter@REDACTED dieter@REDACTED
Fri Jun 29 12:41:36 CEST 2018


Hi Karlo, list,
I am just learning erlang and found this an interesting topic to explore.
I wrote a little server, which creates generator functions. 
Right now it is not usable for list comprehensions, I think.
And the termination is not very nice yet..
But maybe someone finds it useful.
Kind regards,
dieter
------------------------------------------------------------------------
-module(generator).

-behaviour(gen_server).
%% API
-export([generate/2]).
%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
         terminate/2, code_change/3]).
-define(SERVER, ?MODULE).
-record(state, {client_state, client_fun}).
%%%===================================================================
%%% API
%%%===================================================================
generate(Function,State) ->
    {ok, Pid} = gen_server:start_link(?MODULE, [Function,State], []),
    Generator = fun(Params) ->
                        {ok, Value} = gen_server:call(Pid, {next, Params}),
                        Value
                end,
    Generator.
init([Function, State]) ->

    {ok, #state{client_fun=Function, client_state=State}}.
handle_call({next, Params}, _From, State = #state{client_fun=Function, client_state=ClientState}) ->

    case Function(Params, ClientState) of
        {ok, Value, NewClientState} ->
            {reply, {ok, Value}, State#state{client_state=NewClientState}};
        stop ->
            {stop, stopped, State}
    end.
[..]
%% Client callback function definition
%% @spec func(Params, State) -> {ok, Value, State} | stop
%% Test run
1> G = generator:generate(fun(P,S) -> 

                              V=P+S, 
                              NS=S+1, 
                              case NS of 
                                   13 -> stop; 
                                   _ -> {ok, V, NS} 
                              end 
                        end, 
                        10).
#Fun

2> 
2> 
2> 
2> G13(1).                                                                                              11  
3> G13(1).
12
4> G13(1).
** exception exit: stopped
5> 
=ERROR REPORT==== 29-Jun-2018::10:32:54 ===
** Generic server  terminating 
** Last message in was {next,1}
** When Server state == {state,12,#Fun}
** Reason for termination == 
** stopped
** Client  stacktrace
** [{gen,do_call,4,[{file,"gen.erl"},{line,169}]},
    {gen_server,call,2,[{file,"gen_server.erl"},{line,202}]},
    {generator,'-generate/2-fun-0-',2,[{file,"generator.erl"},{line,30}]},
    {erl_eval,do_apply,5,[{file,"erl_eval.erl"},{line,661}]},
    {shell,exprs,7,[{file,"shell.erl"},{line,687}]},
    {shell,eval_exprs,7,[{file,"shell.erl"},{line,642}]},
    {shell,eval_loop,3,[{file,"shell.erl"},{line,627}]}]
Am Fr., Jun. 29, 2018 00:27 schrieb Karlo Kuna : i was wandering is there protocol or "interface" for making custom generators in erlang? here i asume generator can be used in list comprehensions 
something similar to I/O protocol
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20180629/485276b7/attachment.htm>


More information about the erlang-questions mailing list