<div dir="ltr"><div>it is a very bad practice to have one function for  atom() -> int() and int() -> atom() that will detect whether it is encoding or decoding.<br><br></div>You will meet some unknown commands in protocol and decide to bypass them as an integer. Everything will break.<br>
<br></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Wed, Dec 19, 2012 at 7:47 PM, Daniel Goertzen <span dir="ltr"><<a href="mailto:daniel.goertzen@gmail.com" target="_blank">daniel.goertzen@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">This worked for me:<div><br></div><div><div><font face="courier new, monospace">-define(TRANSCODE(Atom,Int), transcode(Atom) -> Int; transcode(Int) -> Atom).</font></div>
<div><font face="courier new, monospace"><br></font></div>
<div><font face="courier new, monospace">?TRANSCODE(hello, 100);</font></div><div><font face="courier new, monospace">?TRANSCODE(help, 101);</font></div><div><font face="courier new, monospace">?TRANSCODE(done, 102).</font></div>

<div><br></div><div><br></div><div><br></div><div>Inspection of the expanded output with <span style="font-size:medium;font-family:Courier,monospace">compile:file(File, ['P'])</span><span style="font-size:medium;font-family:Verdana,Arial,Helvetica,sans-serif">. shows...</span></div>

<div><br></div><div><div><font face="courier new, monospace">transcode(hello) -></font></div><div><font face="courier new, monospace">    100;</font></div><div><font face="courier new, monospace">transcode(100) -></font></div>

<div><font face="courier new, monospace">    hello;</font></div><div><font face="courier new, monospace">transcode(help) -></font></div><div><font face="courier new, monospace">    101;</font></div><div><font face="courier new, monospace">transcode(101) -></font></div>

<div><font face="courier new, monospace">    help;</font></div><div><font face="courier new, monospace">transcode(done) -></font></div><div><font face="courier new, monospace">    102;</font></div><div><font face="courier new, monospace">transcode(102) -></font></div>

<div><font face="courier new, monospace">    done.</font></div></div><div><br></div><div><br></div><div>Dan.</div><div><br></div><div>On Wed, Dec 19, 2012 at 6:50 AM, Steve Davis <span dir="ltr"><<a href="mailto:steven.charles.davis@gmail.com" target="_blank">steven.charles.davis@gmail.com</a>></span> wrote:</div>

<div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div>Hi,</div><div><br></div><div><div style="vertical-align:baseline;color:rgb(34,34,34);font-size:13px;font-family:Arial,Helvetica,sans-serif;margin:0px;border:0px;padding:0px">

I'm frequently facing situations with binary protocol translations/transforms where I need to map binary codes to e.g. atoms and back. </div><div style="vertical-align:baseline;color:rgb(34,34,34);font-size:13px;font-family:Arial,Helvetica,sans-serif;margin:0px;border:0px;padding:0px">

<br></div><div style="vertical-align:baseline;color:rgb(34,34,34);font-size:13px;font-family:Arial,Helvetica,sans-serif;margin:0px;border:0px;padding:0px">This is of course "easy" in Erlang, but suffers some inconveniences. I've attached a code snippet distilled down to the simplest case to explain my issue.</div>

<div style="vertical-align:baseline;color:rgb(34,34,34);font-size:13px;font-family:Arial,Helvetica,sans-serif;margin:0px;border:0px;padding:0px"><br></div><div style="vertical-align:baseline;color:rgb(34,34,34);font-size:13px;font-family:Arial,Helvetica,sans-serif;margin:0px;border:0px;padding:0px">

I'm sure the first codec pattern below is familiar, and for sure is efficient. </div><div style="vertical-align:baseline;color:rgb(34,34,34);font-size:13px;font-family:Arial,Helvetica,sans-serif;margin:0px;border:0px;padding:0px">

However, there are multiple places where updating is required to add new message types.</div><div style="vertical-align:baseline;color:rgb(34,34,34);font-size:13px;font-family:Arial,Helvetica,sans-serif;margin:0px;border:0px;padding:0px">

<br></div><div style="vertical-align:baseline;color:rgb(34,34,34);font-size:13px;font-family:Arial,Helvetica,sans-serif;margin:0px;border:0px;padding:0px">The second pattern is much less usual, but handy as one line achieves the addition of a new message type.</div>

<div style="vertical-align:baseline;color:rgb(34,34,34);font-size:13px;font-family:Arial,Helvetica,sans-serif;margin:0px;border:0px;padding:0px">It helps particularly when there is more than one pairing involved (e.g. {atom, code, status_message}). </div>

<div style="vertical-align:baseline;color:rgb(34,34,34);font-size:13px;font-family:Arial,Helvetica,sans-serif;margin:0px;border:0px;padding:0px"><br></div><div style="vertical-align:baseline;color:rgb(34,34,34);font-size:13px;font-family:Arial,Helvetica,sans-serif;margin:0px;border:0px;padding:0px">

For sure this cannot be something nobody has seen/thought about. I'm wondering if anyone has comment on this, and maybe suggestions for approaches that I haven't thought of.</div><div style="vertical-align:baseline;color:rgb(34,34,34);font-size:13px;font-family:Arial,Helvetica,sans-serif;margin:0px;border:0px;padding:0px">

<br></div><div style="vertical-align:baseline;color:rgb(34,34,34);font-size:13px;font-family:Arial,Helvetica,sans-serif;margin:0px;border:0px;padding:0px">/s</div><div style="vertical-align:baseline;color:rgb(34,34,34);font-size:13px;font-family:Arial,Helvetica,sans-serif;margin:0px;border:0px;padding:0px">

-------</div></div><div>-module(mapping).</div><div><br></div><div>-compile(export_all).</div><div><br></div><div>%% traditional</div><div><br></div><div>-define(HELLO, 100).</div><div>-define(HELP, 101).</div><div>-define(DONE, 102).</div>

<div><br></div><div>encode(hello) -> ?HELLO;</div><div>encode(help) -> ?HELP;</div><div>encode(done) -> ?DONE.</div><div><br></div><div>decode(?HELLO) -> hello;</div><div>decode(?HELP) -> help;</div><div>decode(?DONE) -> done.</div>

<div><br></div><div>%% alternative</div><div><br></div><div>-define(MAP, [</div><div><span style="white-space:pre-wrap">      </span>{hello, 100},</div><div><span style="white-space:pre-wrap">    </span>{help, 101},</div><div><span style="white-space:pre-wrap">     </span>{done, 102}</div>

<div>]).</div><div><br></div><div>encode0(X) -></div><div><span style="white-space:pre-wrap">        </span>{X, Y} = lists:keyfind(X, 1, ?MAP),</div><div><span style="white-space:pre-wrap">      </span>Y.</div><div><br></div><div>

decode0(X) -></div><div><span style="white-space:pre-wrap">      </span>{Y, X} = lists:keyfind(X, 2, ?MAP),</div><div><span style="white-space:pre-wrap">      </span>Y.</div><div><br></div></div><br>_______________________________________________<br>


erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org" target="_blank">erlang-questions@erlang.org</a><br>
<a href="http://erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://erlang.org/mailman/listinfo/erlang-questions</a><br>
<br></blockquote></div><br></div></div>
<br>_______________________________________________<br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a><br>
<a href="http://erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://erlang.org/mailman/listinfo/erlang-questions</a><br>
<br></blockquote></div><br></div>