<div dir="ltr"><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Sep 1, 2015 at 6:43 PM, zxq9 <span dir="ltr"><<a href="mailto:zxq9@zxq9.com" target="_blank">zxq9@zxq9.com</a>></span> wrote:<br><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></div></blockquote></div><br>I must admit I sometimes do this. Consider:</div><div class="gmail_extra"><br></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></div><div class="gmail_extra">This is easier written as</div><div class="gmail_extra"><br></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></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></div><div class="gmail_extra"><a href="https://github.com/jlouis/maps_eqc/blob/96d06da56053e87dd33c830b293dface525be17d/src/maps_eqc.erl#L693-L696">https://github.com/jlouis/maps_eqc/blob/96d06da56053e87dd33c830b293dface525be17d/src/maps_eqc.erl#L693-L696</a><br></div><div class="gmail_extra"><br></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></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></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"><div><br></div>-- <br><div class="gmail_signature">J.</div>
</div></div>