<html><head><meta http-equiv="Content-Type" content="text/html charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div>Hi,</div><div><br></div><div><div style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: rgb(34, 34, 34); font-family: Arial, Helvetica, sans-serif; font-size: 13px; background-color: rgb(255, 255, 255); ">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="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: rgb(34, 34, 34); font-family: Arial, Helvetica, sans-serif; font-size: 13px; background-color: rgb(255, 255, 255); "><br></div><div style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: rgb(34, 34, 34); font-family: Arial, Helvetica, sans-serif; font-size: 13px; background-color: rgb(255, 255, 255); ">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="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: rgb(34, 34, 34); font-family: Arial, Helvetica, sans-serif; font-size: 13px; background-color: rgb(255, 255, 255); "><br></div><div style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: rgb(34, 34, 34); font-family: Arial, Helvetica, sans-serif; font-size: 13px; background-color: rgb(255, 255, 255); ">I'm sure the first codec pattern below is familiar, and for sure is efficient. </div><div style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: rgb(34, 34, 34); font-family: Arial, Helvetica, sans-serif; font-size: 13px; background-color: rgb(255, 255, 255); ">However, there are multiple places where updating is required to add new message types.</div><div style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: rgb(34, 34, 34); font-family: Arial, Helvetica, sans-serif; font-size: 13px; background-color: rgb(255, 255, 255); "><br></div><div style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: rgb(34, 34, 34); font-family: Arial, Helvetica, sans-serif; font-size: 13px; background-color: rgb(255, 255, 255); ">The second pattern is much less usual, but handy as one line achieves the addition of a new message type.</div><div style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: rgb(34, 34, 34); font-family: Arial, Helvetica, sans-serif; font-size: 13px; background-color: rgb(255, 255, 255); ">It helps particularly when there is more than one pairing involved (e.g. {atom, code, status_message}). </div><div style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: rgb(34, 34, 34); font-family: Arial, Helvetica, sans-serif; font-size: 13px; background-color: rgb(255, 255, 255); "><br></div><div style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: rgb(34, 34, 34); font-family: Arial, Helvetica, sans-serif; font-size: 13px; background-color: rgb(255, 255, 255); ">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="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: rgb(34, 34, 34); font-family: Arial, Helvetica, sans-serif; font-size: 13px; background-color: rgb(255, 255, 255); "><br></div><div style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: rgb(34, 34, 34); font-family: Arial, Helvetica, sans-serif; font-size: 13px; background-color: rgb(255, 255, 255); ">/s</div><div style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: rgb(34, 34, 34); font-family: Arial, Helvetica, sans-serif; font-size: 13px; background-color: rgb(255, 255, 255); ">-------</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 class="Apple-tab-span" style="white-space:pre"> </span>{hello, 100},</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>{help, 101},</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>{done, 102}</div><div>]).</div><div><br></div><div>encode0(X) -></div><div><span class="Apple-tab-span" style="white-space:pre"> </span>{X, Y} = lists:keyfind(X, 1, ?MAP),</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>Y.</div><div><br></div><div>decode0(X) -></div><div><span class="Apple-tab-span" style="white-space:pre"> </span>{Y, X} = lists:keyfind(X, 2, ?MAP),</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>Y.</div><div><br></div></body></html>