<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">We had this same discussion before with Jesper (when I wrote this blog post), but I still think these versions clearer:<div class=""><br class=""></div><div class=""><div class="">remove_args(#state { contents = C } = State) -></div><div class=""> frequency(</div><div class=""> case C of</div><div class=""> [] -> [];</div><div class=""> C -> [{5, ?LET(Pair, elements(C), [element(1, Pair)])}]</div><div class=""> end ++ [{1, ?SUCHTHAT([K], [map_key(State)], find(K, 1, C) == false)}]).</div><div class=""><br class=""></div><div class=""><div class=""><br class=""></div><div class="">f(X) -> [1,2,3] ++ e(X) ++ [4,5].</div><div class="">e(X) when X > 5 -> [7];</div><div class="">e(_) -> [].</div></div><div class=""><br class=""></div><div class="">My two cents, ¯\_(ツ)_/¯ </div><div class="">
<div class=""><table border="0" cellpadding="0" cellspacing="0" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 14px; font-family: proxima-nova-1, proxima-nova-2, Tahoma, Helvetica, Verdana, sans-serif; vertical-align: baseline; border-spacing: 0px; color: rgb(51, 51, 51); line-height: 18.2000007629395px; background-color: rgb(255, 255, 255);" class=""><tbody style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline;" class=""><tr style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline;" class=""><td style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-style: inherit; font-size: 0px; font-family: inherit; vertical-align: baseline; width: auto; height: 30px;" class=""> </td></tr><tr style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline;" class=""><td style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; width: auto;" class=""><div style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; line-height: 0;" class=""><a href="http://about.me/elbrujohalcon?promo=email_sig" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(43, 130, 173); text-decoration: none; display: inline-block;" class=""><table border="0" cellpadding="0" cellspacing="0" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-style: inherit; font-size: 14px; font-family: inherit; vertical-align: baseline; border-spacing: 0px;" class=""><tbody style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline;" class=""><tr style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline;" class=""><td align="left" valign="top" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-style: inherit; font-family: inherit; vertical-align: top; width: auto; line-height: 1;" class=""><img alt="--" width="0" height="0" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; display: block; width: 0px; height: 0px; overflow: hidden;" class=""><div style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-weight: bold; font-style: inherit; font-size: 18px; font-family: proxima-nova-1, Proxima-Nova, Helvetica, Arial, sans-serif; vertical-align: baseline; line-height: 1; color: rgb(51, 51, 51);" class="">Fernando "Brujo" Benavides</div><div style="margin: 3px 0px 0px; padding: 0px; border: 0px; outline: 0px; font-style: inherit; font-size: 12px; font-family: proxima-nova-1, Proxima-Nova, Helvetica, Arial, sans-serif; vertical-align: baseline; color: rgb(43, 130, 173);" class=""><img alt="http://" width="0" height="0" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; display: block; width: 0px; height: 0px; overflow: hidden;" class="">about.me/elbrujohalcon</div></td></tr><tr style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline;" class=""><td align="left" valign="top" style="margin: 0px; padding: 8px 0px 0px; border: 0px; outline: 0px; font-style: inherit; font-family: inherit; vertical-align: top; width: auto; line-height: 1;" class=""><div style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; text-align: right; height: 4px; background-color: rgb(197, 208, 224);" class=""><img src="http://d13pix9kaak6wt.cloudfront.net/signature/colorbar.png" alt="" width="88" height="4" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; float: right; display: block;" class=""></div></td></tr></tbody></table></a> </div></td></tr><tr style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline;" class=""><td style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-style: inherit; font-size: 0px; font-family: inherit; vertical-align: baseline; width: auto; height: 20px;" class=""> </td></tr></tbody></table><div class=""><br class=""></div></div><br class="Apple-interchange-newline">
</div>
<br class=""><div><blockquote type="cite" class=""><div class="">On Sep 1, 2015, at 15:01, Jesper Louis Andersen <<a href="mailto:jesper.louis.andersen@gmail.com" class="">jesper.louis.andersen@gmail.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class=""><div class="gmail_extra"><br class=""><div class="gmail_quote">On Tue, Sep 1, 2015 at 6:43 PM, zxq9 <span dir="ltr" class=""><<a href="mailto:zxq9@zxq9.com" target="_blank" class="">zxq9@zxq9.com</a>></span> wrote:<br class=""><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div id=":9r" class="" style="overflow:hidden">Honestly, I don't really think this is the best use of list comprehension syntax. I can easily imagine people being confused at that, or at least requiring a few moments thought to figure out wtf is going on in that line. The fact this confounded you (and the example was unclear in the blog post, imo) is a good reason why you shouldn't do this in your code.<br class=""></div></blockquote></div><br class="">I must admit I sometimes do this. Consider:</div><div class="gmail_extra"><br class=""></div><div class="gmail_extra">f(X) -></div><div class="gmail_extra"> E = case X > 5 of</div><div class="gmail_extra"> true -> [7];</div><div class="gmail_extra"> false -> []</div><div class="gmail_extra"> end,</div><div class="gmail_extra"> [1,2,3] ++ E ++ [4,5].</div><div class="gmail_extra"><br class=""></div><div class="gmail_extra">This is easier written as</div><div class="gmail_extra"><br class=""></div><div class="gmail_extra">f(X) -></div><div class="gmail_extra"> [1,2,3] ++ [7 || X > 5] ++ [4,5].</div><div class="gmail_extra"><br class=""></div><div class="gmail_extra">but as a way to get the list comprehension to append like this is the only way I tend to use the construction. For real-world use, consider the following snippet from my Maps R18 tests:</div><div class="gmail_extra"><br class=""></div><div class="gmail_extra"><a href="https://github.com/jlouis/maps_eqc/blob/96d06da56053e87dd33c830b293dface525be17d/src/maps_eqc.erl#L693-L696" class="">https://github.com/jlouis/maps_eqc/blob/96d06da56053e87dd33c830b293dface525be17d/src/maps_eqc.erl#L693-L696</a><br class=""></div><div class="gmail_extra"><br class=""></div><div class="gmail_extra"><div class="gmail_extra">remove_args(#state { contents = C } = State) -></div><div class="gmail_extra"> frequency(</div><div class="gmail_extra"> [{5, ?LET(Pair, elements(C), [element(1, Pair)])} || C /= [] ] ++</div><div class="gmail_extra"> [{1, ?SUCHTHAT([K], [map_key(State)], find(K, 1, C) == false)}]).</div><div class="gmail_extra"><br class=""></div><div class="gmail_extra">The idea here is that we want to generate arguments for removing an element from a map, and C contains the current contents of the map. If the map is non-empty, C /= [], and we can pick elements from C. Otherwise, we generate a map key such that it is really not an element of the map in question (which is trivially true if C = []). The neat part is that the first variant with frequency 5 is never generated for the empty map.</div><div class="gmail_extra"><br class=""></div><div class="gmail_extra">A case analysis in this case would tend to repeat code, so I find this somewhat more nimble. But these situations are probably the only situations on which I use this way of writing.</div></div><div class="gmail_extra"><br clear="all" class=""><div class=""><br class=""></div>-- <br class=""><div class="gmail_signature">J.</div>
</div></div>
_______________________________________________<br class="">erlang-questions mailing list<br class=""><a href="mailto:erlang-questions@erlang.org" class="">erlang-questions@erlang.org</a><br class="">http://erlang.org/mailman/listinfo/erlang-questions<br class=""></div></blockquote></div><br class=""></div></body></html>