www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - C# code sample

reply pragma <the_ignorator hotmail.com> writes:
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
Jan 24 2011
next sibling parent reply "Simen kjaeraas" <simen.kjaras gmail.com> writes:
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
Jan 24 2011
next sibling parent reply Jesse Phillips <jessekphillips+D gmail.com> writes:
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[] ) )
Jan 24 2011
parent "Simen kjaeraas" <simen.kjaras gmail.com> writes:
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
Jan 24 2011
prev sibling parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
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
Jan 24 2011
parent reply Mandeep Singh Brar <mandeep brars.co.in> writes:
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
Jan 25 2011
parent Jesse Phillips <jessekphillips+D gmail.com> writes:
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.
Jan 25 2011
prev sibling parent reply Kagamin <spam here.lot> writes:
pragma 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?
Usually there's a little need for a range in such case. IEnumerable is usually used because there's a high need for the List collection so it's used even more often than arrays. Current druntime heap implementation already gives D arrays C# List performance.
Jan 26 2011
next sibling parent bearophile <bearophileHUGS lycos.com> writes:
Kagamin:

 Current druntime heap implementation already gives D arrays C# List
performance.
What do you mean? Do you mean regarding appends, iteration, or what? Bye, bearophile
Jan 26 2011
prev sibling parent Jesse Phillips <jessekphillips+D gmail.com> writes:
Kagamin Wrote:

 pragma Wrote:
 
 i guess the D equivalent to IEnumerable is Range? how would it look like in D?
Usually there's a little need for a range in such case. IEnumerable is usually used because there's a high need for the List collection so it's used even more often than arrays. Current druntime heap implementation already gives D arrays C# List performance.
What? Of course there is a reason to use a Range/Iterable and has nothing to do with performance. He could be getting his double[] from any number of functions found in std.algorithm, most of whom return their own Range type.
Jan 26 2011