sum across an array of objects

Philip Daniels Philip.Daniels1971 at gmail.com
Sat Jul 28 00:58:32 PDT 2012


I have an array of objects that have a size() property. Code to 
sum them manually works fine, as expected:

   auto total = 0;
   foreach (wt; _word_tables)
     total += wt.size();
   return total;


How can I do this with reduce? (BTW I second the old comments and 
bugs about not having sum() built into the library, it's 
annoying.)

I tried

   auto total = reduce!(
     (int a, WordTable b) { return a + b.size(); })
     (0, _word_tables);

but it seems that reduce expects a and b to be of type WordTable, 
so it won't instantiate the template. It's also as long as the 
manual version :-(

I really want to do

   auto total = sum(a => a.size(), _word_tables)

if that's possible.





More information about the Digitalmars-d-learn mailing list