<div class="gmail_quote">On Sat, Nov 7, 2009 at 17:33, Pelle Månsson <span dir="ltr"><<a href="mailto:pelle.mansson@gmail.com">pelle.mansson@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div><div></div>I am all in favor of adding convenience functions sum and product to phobos. I use them both often enough.<br></div></blockquote><div><br>vote++<br><br>And also min (on a range), max (on a range). Those are simple one-liners, though they can create some name-shadowing problems and are dubious with empty ranges.<br>
<br>ElementType!R min(R)(R range) {<br> enforce(!range.empty, "Don't use min on an empty range.");<br> return reduce!(std.algorithm.min)(ElementType!R.max, range);<br>}<br><br>Maybe R doesn't have elements with a .max property, but then the same remark can be used on sum/product (0 or 1 are not necessarily the neutral elements for + and * on your struct/class). For specific structures, use your own sum function.<br>
<br><br>Some other languages also have and( on a boolean range) and or( on a boolean range), though I never use them. They are slightly different from reduce!"a&&b"(range) as they can exit early on a 'false' or 'true'. Has anyone ever used this?<br>
</div></div><br><br> Philippe<br>