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

bearophile bearophileHUGS at lycos.com
Fri Nov 16 08:13:39 PST 2012


bioinfornatics:

> I would like to count number of one ore more letter into a 
> string or list of string (string[]) without use a for loop but 
> instead using std.algorithm to compute efficiently.

If it's speed you look for, then most times std.algorithm is not
the solution. Basic loops are usually faster. Iterating on two
chars at a time with a 2^16 table is probably fast enough for
most purposes.


> if you have:
>  string   seq1 = "ACGATCGATCGATCGCGCTAGCTAGCTAG";
>  string[] seq2 = ["ACGATCGATCGATCGCGCTAGCTAGCTAG", 
> "ACGATGACGATCGATGCTAGCTAG"];
>
> i try :
>
> reduce!( (seq) => seq.count("G"), 
> seq.count("C"))(tuple(0LU,0LU),seq1)

This code is a mess. You are not creating a tuple, you are
scanning the string more than once, and such counting is not a
valid reduction function. So you have to start over and think
better what you want to do, why and how.

What do you mean by counting those in a list of strings? Do you
want the total or an array/range with the partials?

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list