array of randomly generated names

Simen kjaeraas simen.kjaras at gmail.com
Sat Oct 16 03:50:09 PDT 2010


spir <denis.spir at gmail.com> wrote:

> ===================
> alias char[] Text ;
>
> Text letters = ['a','b','c',...] ;
>
> Text[] nameSet (uint count , uint size) {
> 	/* set of count random names of size size */
> 	Text[] names ; names.length = count ;
> 	Text name ; name.length = size ;
> 	for (int i=0 ; i<count ; i++) {
> 		for (int j=0 ; j<size ; j++)
> 		    name[j] = letters[uniform(0u,26u)] ;
> 		names[i].length = size ;
> 		names[i][] = name ;
> 	}
> 	return names ;
> }
> ===================

Here's my version of the same, in highly unidiomatic D:

auto nameSet( uint count, uint size ) {
     auto randomWordsRandomLength = map!"a()"(
         repeat({
             return repeat({
                     return cast(char)iota(65,91)[ uniform( 0, 26 ) ];
             });
         })
     );

     return array( take( map!(
         ( randomWord ){
             return array( take( map!"a()"( randomWord ), size ) );
         })( randomWordsRandomLength ), count ) );
}


-- 
Simen


More information about the Digitalmars-d-learn mailing list