Thread overview
C# code sample
Jan 24, 2011
pragma
Jan 24, 2011
Simen kjaeraas
Jan 24, 2011
Jesse Phillips
Jan 24, 2011
Simen kjaeraas
Jan 25, 2011
Mandeep Singh Brar
Jan 25, 2011
Jesse Phillips
January 24, 2011
Hi i come from a c# background

I would like to write the following code in the according D style but i'm not sure howto do it

c# code:
void foo(IEnumerable<double[]> data)
{
  foreach (var d in data)
  {
    do_some_stuff(d);
  }
}

i guess the D equivalent to IEnumerable is Range? how would it look like in D?

greetings
January 24, 2011
pragma <the_ignorator@hotmail.com> wrote:

> Hi i come from a c# background
>
> I would like to write the following code in the according D style but i'm not
> sure howto do it
>
> c# code:
> void foo(IEnumerable<double[]> data)
> {
>   foreach (var d in data)
>   {
>     do_some_stuff(d);
>   }
> }
>
> i guess the D equivalent to IEnumerable is Range? how would it look like in D?

void foo( R )( R data )
    if ( isInputRange!R && is( ElementType!R == double ) )
{
    foreach ( d; data ) {
        do_some_stuff( d );
    }
}

-- 
Simen
January 24, 2011
Simen kjaeraas Wrote:

> pragma <the_ignorator@hotmail.com> wrote:
> 
> > Hi i come from a c# background
> >
> > I would like to write the following code in the according D style but
> > i'm not
> > sure howto do it
> >
> > c# code:
> > void foo(IEnumerable<double[]> data)
> > {
> >   foreach (var d in data)
> >   {
> >     do_some_stuff(d);
> >   }
> > }
> >
> > i guess the D equivalent to IEnumerable is Range? how would it look like in D?
> 
> void foo( R )( R data )
>      if ( isInputRange!R && is( ElementType!R == double ) )
> {
>      foreach ( d; data ) {
>          do_some_stuff( d );
>      }
> }
> 
> -- 
> Simen

I thank it should be:  is( ElementType!R == double[] ) )

January 24, 2011
Jesse Phillips <jessekphillips+D@gmail.com> wrote:

> Simen kjaeraas Wrote:
>
>> pragma <the_ignorator@hotmail.com> wrote:
>>
>> > Hi i come from a c# background
>> >
>> > I would like to write the following code in the according D style but
>> > i'm not
>> > sure howto do it
>> >
>> > c# code:
>> > void foo(IEnumerable<double[]> data)
>> > {
>> >   foreach (var d in data)
>> >   {
>> >     do_some_stuff(d);
>> >   }
>> > }
>> >
>> > i guess the D equivalent to IEnumerable is Range? how would it look  
>> like
>> > in D?
>>
>> void foo( R )( R data )
>>      if ( isInputRange!R && is( ElementType!R == double ) )
>> {
>>      foreach ( d; data ) {
>>          do_some_stuff( d );
>>      }
>> }
>>
>> --
>> Simen
>
> I thank it should be:  is( ElementType!R == double[] ) )

True.

-- 
Simen
January 24, 2011
On Mon, 24 Jan 2011 14:39:33 -0500, Simen kjaeraas <simen.kjaras@gmail.com> wrote:

> pragma <the_ignorator@hotmail.com> wrote:
>
>> Hi i come from a c# background
>>
>> I would like to write the following code in the according D style but i'm not
>> sure howto do it
>>
>> c# code:
>> void foo(IEnumerable<double[]> data)
>> {
>>   foreach (var d in data)
>>   {
>>     do_some_stuff(d);
>>   }
>> }
>>
>> i guess the D equivalent to IEnumerable is Range? how would it look like in D?
>
> void foo( R )( R data )
>      if ( isInputRange!R && is( ElementType!R == double ) )
> {
>      foreach ( d; data ) {
>          do_some_stuff( d );
>      }
> }

Actually, isIterable would be more correct than isInputRange for this code example, not sure what the full code looks like.

http://www.digitalmars.com/d/2.0/phobos/std_traits.html#isIterable

-Steve
January 25, 2011
On Mon, 24 Jan 2011 14:39:33 -0500, Simen kjaeraas <simen.kjaras@gmail.com> wrote:

> pragma <the_ignorator@hotmail.com> wrote:
>
>> Hi i come from a c# background
>>
>> I would like to write the following code in the according D style but
>> i'm not
>> sure howto do it
>>
>> c# code:
>> void foo(IEnumerable<double[]> data)
>> {
>>   foreach (var d in data)
>>   {
>>     do_some_stuff(d);
>>   }
>> }
>>
>> i guess the D equivalent to IEnumerable is Range? how would it look like in D?
>
> void foo( R )( R data )
>      if ( isInputRange!R && is( ElementType!R == double ) )
> {
>      foreach ( d; data ) {
>          do_some_stuff( d );
>      }
> }

Actually, isIterable would be more correct than isInputRange for this code example, not sure what the full code looks like.

http://www.digitalmars.com/d/2.0/phobos/std_traits.html#isIterable

-Steve


How about simply saying:

void foo(double[] data)
{
   foreach (d; data)
   {
     do_some_stuff(d);
   }
}

all ranges are already foreachable.

Regards
Mandeep
January 25, 2011
Mandeep Singh Brar Wrote:

> How about simply saying:
> 
> void foo(double[] data)
> {
>    foreach (d; data)
>    {
>      do_some_stuff(d);
>    }
> }
> 
> all ranges are already foreachable.
> 
> Regards
> Mandeep

He is iterating over a range/iterable of double[], Simen got the type check wrong, which I pointed out.