<br><br><div class="gmail_quote">On Sat, May 29, 2010 at 03:27, bearophile <span dir="ltr">&lt;<a href="mailto:bearophileHUGS@lycos.com">bearophileHUGS@lycos.com</a>&gt;</span> wrote:<br><br>&gt;Now and then it&#39;s very useful to compare how to write in D2 a small 
interesting program <br>&gt;written in another language, the comparison can help
 find problems, possible improvements, <br>&gt;performance problems, and so on.<br>
&gt;<br>
&gt;Time ago I have tried to convert to D2 a small Python program that finds
 the Huffman encoding<br>&gt; of a given string, but Phobos2 was not mature 
enough, and I didn&#39;t produce a good enough D2 program. <br>&gt;I have tried 
again and this time things are good enough.<br>
<br>(snip)<br><br>Hey, cool. And you made it generic to boot. I&#39;d vote that to be put in std.algorithm.<br><br><br><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
Simen kjaeraas:<br>
<div class="im"><br>
&gt; I&#39;m now so tired of hearing this, I started writing something that<br>
&gt; does it:</div></blockquote><div>&gt;Now, granted, I have little to no experience with list comprehension,<br>
&gt;and as such this might not be what is needed. Still, it was fun to<br>
&gt;play around with, and I kinda like what it turned out as.<br> 
<br>That&#39;s fun to see, the syntax is cool. I never thought of using operator overloading for this.<br></div><div><br>I did a range comprehension also, a few months ago. It&#39;s called comp in <br><a href="http://svn.dsource.org/projects/dranges/trunk/dranges/docs/algorithm2.html">http://svn.dsource.org/projects/dranges/trunk/dranges/docs/algorithm2.html</a><br>
<br>Like yours, it takes any range and is lazy, the main difference it that it&#39;s multirange: you can give it any number of range as input, it will create the combinations of elements (aka product of ranges), filter it with the predicate and map the generative function on it.<br>
<br>Syntax is     comp!(mappingFunction, predicate)(ranges...)<br><br>The classical example for Haskell list comprehension is generating the pythagorean triplets:<br><br><pre>pyth n = [ ( a, b, c ) | a &lt;- [1..n],          -- Pythagorean Triples<br>
                         b &lt;- [1..n], <br>                         c &lt;- [1..n], <br>                         a + b + c &lt;= n,<br>                         a^2 + b^2 == c^2 ]<br></pre><br>In my case, it would be:<br>
<br>auto pyth(int n) <br>{<br>    return comp ! (&quot;tuple(a,b,c)&quot;, &quot;a*a+b*b==c*c &amp;&amp; a&lt;b&quot;) (iota(1,n),iota(1,n), iota(1,n));<br>}<br><br>pyth(11) -&gt; (3,4,5), (6,8,10)<br><br><br>   Philippe<br>
</div></div>