sum across an array of objects

Philip Daniels Philip.Daniels1971 at gmail.com
Sat Jul 28 01:53:05 PDT 2012


On Saturday, 28 July 2012 at 08:32:57 UTC, Jonathan M Davis wrote:
> void main()
> {
>     auto stuff = ["hello world",
>                   "Goodbye, Shirley",
>                   "Babylon 5 rocks"];
>
>     auto result = reduce!"a + b.length"(0L, stuff);
>     assert(result == join(stuff).length);
> }
>
> The first parameter is the sum, and the second is the item. So, 
> I expect that
>
> auto total = reduce!((a, b) => a + b.size())(0, _word_tables);
>
> would work for you.
>
>
> - Jonathan M Davis

Jonathan, I tried the above suggestion, and also this equivalent 
based on your code:

   auto total = reduce!"a + b.size()"(0, _word_tables);

in both cases I get


/usr/include/i386-linux-gnu/dmd/phobos/std/conv.d(3330): Error: 
cannot implicitly convert expression (_param_1) of type 
WordDatabase.WordTable to int
/usr/include/i386-linux-gnu/dmd/phobos/std/algorithm.d(743): 
Error: template instance std.conv.emplace!(int,WordTable) error 
instantiating
WordDatabase.d(150):        instantiated from here: 
reduce!(int,WordTable[30u])
WordDatabase.d(150): Error: template instance 
std.algorithm.reduce!("a + b.size()").reduce!(int,WordTable[30u]) 
error instantiating




More information about the Digitalmars-d-learn mailing list