Using map instead of iteration

spir denis.spir at gmail.com
Sun Mar 6 07:25:33 PST 2011


On 03/06/2011 02:43 PM, Russel Winder wrote:
> I have a code fragment:
>
>    auto threads = new Thread[numberOfThreads] ;
>    foreach ( i ; 0 .. numberOfThreads ) {
>      void delegate ( ) closedPartialSum ( ) {
>        immutable id = i ;
>        return ( ) { partialSum ( id , sliceSize , delta ) ; } ;
>      }
>      threads[i] = new Thread ( closedPartialSum ) ;
>    }
>
> which clearly should be doable using map from std.algorithm.  So I
> tried:
>
>    auto threads = map ! ( function Thread ( int i ) {
>        void delegate ( ) closedPartialSum ( ) {
>          immutable id = i ;
>          return ( ) { partialSum ( id , sliceSize , delta ) ; } ;
>        }
>        return new Thread ( closedPartialSum ) ;
>      } ) ( 0 .. numberOfThreads )
>
> which fails:
>
> pi_d2_threadsGlobalState.d(41): found '..' when expecting ','
> pi_d2_threadsGlobalState.d(57): semicolon expected following auto
> declaration, not 'foreach'
>
> So clearly 0 .. numberOfThreads only means a range of integers in a
> foreach or array index only and not everywhere as it does in all
> sensible languages :-((
>
> I am clearly missing something, anyone any ideas?

Without trying to study the code's detainls: "( 0 .. numberOfThreads )" has 
very few chances to be accepted by the parser ;-)
Note: there is no interval literal in D. Instead uses of i..j in slicing and 
foreach both are pure syntactic honey.

Denis
-- 
_________________
vita es estrany
spir.wikidot.com



More information about the Digitalmars-d mailing list