Behavior of Array.TakeWhile

  • Thread starter Thread starter Robert Scheer
  • Start date Start date
R

Robert Scheer

Hi.

I am trying to understand how to use the Array.TakeWhile operator. In
my tests below, the first Array is filled but the second one is not:

Dim arTemp() As Integer = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}

Dim first() As Integer = arTemp.TakeWhile(Function(i) i < 5).ToArray
Dim second() As Integer = arTemp.TakeWhile(Function(i) i > 5).ToArray

Am I missing something?

Regards,
Robert
 
In the second is the first item already not true


"Robert Scheer" wrote in message

Hi.

I am trying to understand how to use the Array.TakeWhile operator. In
my tests below, the first Array is filled but the second one is not:

Dim arTemp() As Integer = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}

Dim first() As Integer = arTemp.TakeWhile(Function(i) i < 5).ToArray
Dim second() As Integer = arTemp.TakeWhile(Function(i) i > 5).ToArray

Am I missing something?

Regards,
Robert
 
* Robert Scheer said:
I am trying to understand how to use the Array.TakeWhile operator. In
my tests below, the first Array is filled but the second one is not:

Btw, this is not an operator but an extension function of Linq and it's
not defined for Array but for Enumerables of all kinds.
Dim arTemp() As Integer = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}

Dim first() As Integer = arTemp.TakeWhile(Function(i) i < 5).ToArray
Dim second() As Integer = arTemp.TakeWhile(Function(i) i > 5).ToArray

That's exactly the specified behavior, TakeWhile starts at the first
element and stops when it finds one that doesn't fulfill your predicate.
0 clearly is NOT bigger than 5 :)

I /guess/ you are actually looking for the Where() function

Regards, Felix
 
* Robert Scheer said:
I am trying to understand how to use the Array.TakeWhile operator. In
my tests below, the first Array is filled but the second one is not:

Btw, this is not an operator but an extension function of Linq and it's
not defined for Array but for Enumerables of all kinds.
Dim arTemp() As Integer = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
Dim first() As Integer = arTemp.TakeWhile(Function(i) i < 5).ToArray
Dim second() As Integer = arTemp.TakeWhile(Function(i) i > 5).ToArray

That's exactly the specified behavior, TakeWhile starts at the first
element and stops when it finds one that doesn't fulfill your predicate.
0 clearly is NOT bigger than 5 :)

I /guess/ you are actually looking for the Where() function

Regards, Felix

--
 Felix Palmen       (Zirias)  + [PGP] Felix Palmen <[email protected]>
 web:  http://palmen-it.de/ |            http://palmen-it.de/pub.txt
 my open source projects:     |   Fingerprint: ED9B 62D0 BE39 32F9 2488
 http://palmen-it.de/?pg=pro +                5D0C 8177 9D80 5ECF F683

Hi Felix,

thanks for the detailed explanation. I tried Where as you suggested me
and got what I want.

Can I assume that only Linq has these extension functions? How they
differ from a common method?

Regards,
Robert Scheer
 
* Robert Scheer said:
Can I assume that only Linq has these extension functions? How they
differ from a common method?

No, but Markus already explained that. Of course these extension methods
on enumerables you are using /are/ part of Linq, they are defined in the
System.Linq namespace.

The difference is these functions aren't really methods (they are not
defined inside the class they are operating on), but they behave as if
they were. So it is possible to extend the functionality of a class
without creating a derived class.

Regards, Felix
 
Back
Top