std.algorithm - notes

Jarrod qwerty at ytre.wq
Fri Mar 7 06:40:36 PST 2008


Perhaps I wasn't too clear about what I meant. I recall you saying D 
should have python list comps, and anything else is not worth it. I 
wasn't saying D should go without any list generation at all, I just 
think the python method of going about it may not exactly 'gel' well with 
a language like D. The first alternative that comes to mind for me, is 
map. Clearly there are other methods to go about it, but map and filter 
are already in std2.


Oh and I couldn't help myself, I had to throw together this quick perl 
version of your program:
> sub inverse{
> 	my @n2txt = qw(zero one two three four five six seven eight nine);
> 	my $c;
> 	my %txt2n = map {($_,$c++)} @n2txt;
> 
> 	my $n = shift;
> 	if ($n =~ /^\d+$/){
> 		return join ' ', map {$n2txt[$_]} split '', $n;
> 	} 
> 	else{
> 		return join '', map {$txt2n{$_}} split ' ', $n;
> 	}
> }
> print inverse(inverse(752))."\n";

Makes you wonder if python style list comps in D would end up any better 
than map did.



More information about the Digitalmars-d mailing list