Seeking advice on port "pattern" (long)

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

Seeking advice on port "pattern" (long)

Sean Hinde-2
>  2. You could write your decoder in a continuation
>     based style. So instead of having to do the
>     append (AllData = Buffered++Data) your decoder
>     returns either { Id, Req, Result } , { parseError, Details } ,
>     or {needmore, Cont}. When new data arrives, you
>     feed it into your continuation ( Cont(Data) ), which
>     again will return any of the three possibilities above.

Can you provide an example of what you mean? I know Joe posted one some time
ago but I can't find it and haven't quite managed to figure it out for
myself.

Thanks,

- Sean



NOTICE AND DISCLAIMER:
This email (including attachments) is confidential.  If you have received
this email in error please notify the sender immediately and delete this
email from your system without copying or disseminating it or placing any
reliance upon its contents.  We cannot accept liability for any breaches of
confidence arising through use of email.  Any opinions expressed in this
email (including attachments) are those of the author and do not necessarily
reflect our opinions.  We will not accept responsibility for any commitments
made by our employees outside the scope of our business.  We do not warrant
the accuracy or completeness of such information.



Reply | Threaded
Open this post in threaded view
|

Seeking advice on port "pattern" (long)

tobbe

> Can you provide an example of what you mean? I know Joe posted one some time
> ago but I can't find it and haven't quite managed to figure it out for
> myself.

Ok, below I've whipped up something to show what I meant.

Cheers /Tobbe
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-module(abc).
-export([abc/1]).

%%%
%%% Parse strings looking like: "abc"
%%% Example:
%%%
%%%   2> abc:abc("abcdefg").
%%%   {ok,"abc","defg"}
%%%   3> {_,C} = abc:abc("a").
%%%   {needmore,#Fun<abc.1.119051369>}
%%%   4> {_,C2} = C("b").
%%%   {needmore,#Fun<abc.2.1309902>}
%%%   5> C("cdef").      
%%%   {parse_error,{"a",'<here>',"cdef"}}
%%%   6> C2("cdef").
%%%   {ok,"abc","def"}
%%%

abc(In) ->
    a(In,[]).

a([$a|T],Acc) -> b(T, [$a|Acc]);
a([],Acc)     -> {needmore, fun(More) -> a(More, Acc) end};
a(In,Acc)     -> parse_error(In,Acc).

b([$b|T],Acc) -> c(T, [$b|Acc]);
b([],Acc)     -> {needmore, fun(More) -> b(More,Acc) end};
b(In,Acc)     -> parse_error(In,Acc).

c([$c|T],Acc) -> {ok, lists:reverse([$c|Acc]), T};
c([],Acc)     -> {needmore, fun(More) -> c(More, Acc) end};
c(In,Acc)     -> parse_error(In,Acc).

parse_error(In,Acc) -> {parse_error, {lists:reverse(Acc), '<here>', In}}.



Reply | Threaded
Open this post in threaded view
|

Seeking advice on port "pattern" (long)

Garry Hodgson-3
Torbjorn Tornkvist wrote:

>
> > Can you provide an example of what you mean? I know Joe posted one some time
> > ago but I can't find it and haven't quite managed to figure it out for
> > myself.
>
> Ok, below I've whipped up something to show what I meant.
>
> Cheers /Tobbe
> %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
> -module(abc).
> -export([abc/1]).
>
> %%%
> %%% Parse strings looking like: "abc"
> %%% Example:
> %%%
> %%%   2> abc:abc("abcdefg").
> %%%   {ok,"abc","defg"}
> %%%   3> {_,C} = abc:abc("a").
> %%%   {needmore,#Fun<abc.1.119051369>}
> %%%   4> {_,C2} = C("b").
> %%%   {needmore,#Fun<abc.2.1309902>}
> %%%   5> C("cdef").
> %%%   {parse_error,{"a",'<here>',"cdef"}}
> %%%   6> C2("cdef").
> %%%   {ok,"abc","def"}
> %%%

very cool.  i reworked my code to use this notion, and it works
just fine.  thanks.

--
Garry Hodgson                   sometimes we ride on your horses
Senior Hacker                   sometimes we walk alone
Software Innovation Services    sometimes the songs that we hear
AT&T Labs                       are just songs of our own
garry