PC Review


Reply
Thread Tools Rate Thread

Behavior of Array.TakeWhile

 
 
Robert Scheer
Guest
Posts: n/a
 
      31st Aug 2010
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

 
Reply With Quote
 
 
 
 
Cor
Guest
Posts: n/a
 
      31st Aug 2010
In the second is the first item already not true


"Robert Scheer" wrote in message
news:27be86ed-8ae7-4269-89a0-(E-Mail Removed)...

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

 
Reply With Quote
 
Felix Palmen
Guest
Posts: n/a
 
      31st Aug 2010
* Robert Scheer <(E-Mail Removed)>:
> 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 <(E-Mail Removed)>
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
 
Reply With Quote
 
Robert Scheer
Guest
Posts: n/a
 
      31st Aug 2010
On 31 ago, 12:46, fe...@palmen-it.de (Felix Palmen) wrote:
> * Robert Scheer <rbsch...@my-deja.com>:
>
> > 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 <fe...@palmen-it.de>
> *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
 
Reply With Quote
 
MarkusSchaber
Guest
Posts: n/a
 
      1st Sep 2010
Hi, Robert,

On 31 Aug., 19:20, Robert Scheer <rbsch...@my-deja.com> wrote:
> On 31 ago, 12:46, fe...@palmen-it.de (Felix Palmen) wrote:
> Can I assume that only Linq has these extension functions? How they
> differ from a common method?


No, Linq just makes intense use of those functions. They were
introduced in c# 3.0.

http://msdn.microsoft.com/en-us/library/bb383977.aspx has more
information about them.

HTH,
Markus
 
Reply With Quote
 
Felix Palmen
Guest
Posts: n/a
 
      1st Sep 2010
* Robert Scheer <(E-Mail Removed)>:
> 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

--
Felix Palmen (Zirias) + [PGP] Felix Palmen <(E-Mail Removed)>
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
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Strange pasting array to cells behavior CompleteNewb Microsoft Excel Programming 2 13th Oct 2010 03:51 AM
Behavior of Array.TakeWhile Robert Scheer Microsoft Dot NET 5 1st Sep 2010 08:28 AM
PERCENTRANK in array formula: strange behavior vezerid Microsoft Excel Misc 4 9th Mar 2006 04:11 PM
Strange behavior in INDEX(..., MIN(...)) - array formula vezerid Microsoft Excel Misc 2 21st Feb 2006 11:56 AM
Strange behavior of modulo in array index =?Utf-8?B?QmVuIEJsYW5r?= Microsoft C# .NET 11 25th Oct 2004 10:29 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:40 AM.