custom generators?

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

custom generators?

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 




_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: custom generators?

Fred Hebert-2
On 06/29, Karlo Kuna wrote:
>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

List comprehensions operate only on lists and binaries (using the
'<<BytePattern>> <= Bin' generator instead of `Pattern <- List').  The
closest thing you could get is likely QLC:
http://erlang.org/doc/man/qlc.html
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: custom generators?

Karlo Kuna
qlc table seems like answer i was looking for 
thank you fred

On Fri, Jun 29, 2018 at 1:31 AM Fred Hebert <[hidden email]> wrote:
On 06/29, Karlo Kuna wrote:
>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

List comprehensions operate only on lists and binaries (using the
'<<BytePattern>> <= Bin' generator instead of `Pattern <- List').  The
closest thing you could get is likely QLC:
http://erlang.org/doc/man/qlc.html

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: custom generators?

dieter
In reply to this post by Karlo Kuna
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<generator.0.70999052>
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 <0.67.0> terminating 
** Last message in was {next,1}
** When Server state == {state,12,#Fun<erl_eval.12.99386804>}
** Reason for termination == 
** stopped
** Client <0.65.0> 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 <[hidden email]>:
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 




_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions