how to count number of letters with std.algorithm.count / std.algorithm.reduce / std.algorithm.map ?

Simen Kjaeraas simen.kjaras at gmail.com
Fri Nov 16 09:33:51 PST 2012


On 2012-11-16, 17:37, bearophile wrote:

> Simen Kjaeraas:
>
>> There. Now it works, and returns a Tuple!(ulong,ulong)(8, 8).
>>
>> One thing I think is ugly in my implementation is acc + (seq == 'G').  
>> This
>> adds a bool and a ulong together. For more points, replace that with
>> acc + (seq == 'G' ? 1 : 0).
>
> I use a pragmatic approach: I use such higher order functions when they  
> give me some advantage, like more compact code, or less-bug-prone code,  
> etc (because they usually don't give me faster code). In this case a  
> normal loop that increments two counters is faster and far more easy to  
> read and understand :-)

Absolutely. But after trying to make sense of the mess presented in the
OP, I wanted to document the voyage.

-- 
Simen


More information about the Digitalmars-d-learn mailing list