<div dir="ltr"><div><div><div><div><div><div><div><div><div>I usually do something like:<br><br></div>Txn = fun() -> ... end,<br></div>case mnesia:transaction(Txn) of<br></div>    {atomic, []} -><br></div>       not_found;<br></div>    {atomic, [V]} -><br></div>       <One_Value_Case><br></div>    {atomic, L = [_|_]} -><br></div>        <Multi_Value_Case></div><div>end.</div><div><br></div><div>Some times, if there are a bunch of cases, I tend to use a function such as your confirm/1 function, passing the result on and writing down the case split at the top level, but it depends a bit on wether I like that code flow or not in that particular case. In general I prefer matching and binding values over projection functions such as hd/1, because hd/1 doesn't work on all lists, only the non-empty ones.</div><div><br></div><div>Another common trick is that if your Results are lists, then you can often avoid using a not_found atom, but handle the [] case naturally. Consider that</div><div><br></div><div>[] = lists:map(F, [])</div><div><br></div><div>which is true for a lot of functions working on lists. This suggests you can avoid having multiple data flows in this case, and use [] as a degenerate case which "skips" computations in a natural way. This can sometimes turn a code flow which case-splits its control flow all the time into a flow that just has one path. Flow with a single path tend to be far easier to handle in the long run in my experience.</div><div><br></div><div>Also, if you really do expect there to be a value, then I use</div><div><br></div><div>{atomic, [Value]} = mnesia:transaction(Txn),</div><div><br></div><div>and crash the code if it is violated. Defensive code is often a mistake in Erlang because we can just use the standard crash semantics to recover. Many other languages doesn't fare so well here.</div><div><br></div></div><br><div class="gmail_quote"><div dir="ltr">On Wed, Nov 15, 2017 at 9:04 PM <<a href="mailto:lloyd@writersglen.com">lloyd@writersglen.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hello,<br>
<br>
The result of a mnesia read transaction looks like this:<br>
<br>
   {atomic, Results} where result is either a populated list [value1, ... valueN] or, an empty list [].<br>
<br>
If the table is initialized as set, then the returned list will contain at most one value, e.g:<br>
<br>
  [value].<br>
<br>
If I want to use of the value in a function or to populate another record, I need to extract the value from the list.<br>
<br>
I've tried various ways to do this, but they all seem clumsy:<br>
<br>
E.g.<br>
<br>
[MyValue]<br>
<br>
hd[vlaue]<br>
<br>
confirm([]) -><br>
    not_found;<br>
confirm([Value]) -><br>
    Value;<br>
confirm(Value) -><br>
    Value.<br>
<br>
Does there happen to be a preferred/conventional/best-practices way to do this?<br>
<br>
Many thanks,<br>
<br>
LRP<br>
<br>
<br>
<br>
<br>
<br>
<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" rel="noreferrer" target="_blank">http://erlang.org/mailman/listinfo/erlang-questions</a><br>
</blockquote></div>