Pluck Me!(panteravb.com)

submitted by chrcar01chrcar01(560) 4 years, 4 months ago

Fun with Generics and Arrays and Delegates

8 comments |category: |Views: 0

tags: another

new Add a live kick counter to your blog >> liveImage

You can even customize the image by choosing your own colors, and then clicking the button below to update the preview and the html code:

  • "Kick It" text
  • "Kick It" background
  • kick count text
  • kick count background
  • border

Simply copy and paste this HTML into your blog post.


Users who kicked this story:
Comments:

posted by atifazizatifaziz(1000) 4 years, 4 months ago 0

The blog entry of this story discusses the blogger's journey to arrive at the same solution as provided by Array.ConvertAll<TInput,TOutput>. Here's how the pluck sample could be done today without further ado:
int[] ages = Array.ConvertAll<int, Customer>(_customers, delegate(Customer c) { return c.Age; });

Reply

posted by simonebsimoneb(5450) 4 years, 4 months ago 0

Ahhhrgg, Gavin unkick this immediately!! :-)

Reply

posted by gavinjoycegavinjoyce(25.7k) 4 years, 4 months ago 0

:-)

Kick removed.

Reply

posted by chrcar01chrcar01(560) 4 years, 4 months ago 0

Oh shizzle, I'm dumb, sorry guys. Let me plug that above example in and then run this little console app against the code, it's called csc.exe, some of you may have heard of it. So without further ado:

C:\source\MemberShareData1614\trunk\Component\ArrayUtils.cs(78,17): error CS1502: The best overloaded method match for 'System.Array.ConvertAll<MemberShareData1614.Component.ArrayUtilsTests.Customer,int>(MemberShareData1614.Component.ArrayUtilsTests.Customer[], System.Converter<MemberShareData1614.Component.ArrayUtilsTests.Customer,int>)' has some invalid arguments
C:\source\MemberShareData1614\trunk\Component\ArrayUtils.cs(78,49): error CS1503: Argument '1': cannot convert from 'System.Collections.Generic.List<MemberShareData1614.Component.ArrayUtilsTests.Customer>' to 'MemberShareData1614.Component.ArrayUtilsTests.Customer[]'

That's weird....hmmm, maybe someone needs to take a journey and learn how csc.exe compiles code catches errors like the one posted above, etc, etc.

I had updated the blog post but just caught the, ahem, brilliant review above.

So without more adoodie doo doo: Mr Aziz, Pluck Off! :)

Reply

posted by atifazizatifaziz(1000) 4 years, 4 months ago 0

OK, Chris, so sue me for not checking with the compiler first, but as your examples were full of compilation mistakes anyhow, I didn't think that structural integrity and accuracy was the big picture here. But since you want to micro-pluck here, let's take an enlightened journey into csc.exe. Below is that code I started with where most snippets were copied verbatim off the blog entry in an attempt to construct a working example:

using System;
using System.Collections.Generic;

class Customer
{
public int Age;
public string Name;
public DateTime Birthday;

Customer(int age, string name, DateTime birthday)
{
this.Age = age;
this.Name = name;
this.Birthday = birthday;
}
}

class Program
{
static void Main()
{
List<Customer> customers = new List<Customer>();
customers.Add(new Customer(37, new DateTime(1970, 6, 18), "Chris"));
customers.Add(new Customer(36, new DateTime(1971, 3, 30), "Anja"));
customers.Add(new Customer(3, new DateTime(2004, 4, 11), "Riley"));
customers.Add(new Customer(1, new DateTime(2006, 10, 14), "Emmitt"));

int[] ages = Array.ConvertAll<int, Customer>(customers, delegate(Customer c) { return c.Age; });
}
}

Here's the result of first compilation:

PluckMe.cs(24,23): error CS0122: 'Customer.Customer(int, string, System.DateTime)' is inaccessible due to its protection level
PluckMe.cs(25,23): error CS0122: 'Customer.Customer(int, string, System.DateTime)' is inaccessible due to its protection level
PluckMe.cs(26,23): error CS0122: 'Customer.Customer(int, string, System.DateTime)' is inaccessible due to its protection level
PluckMe.cs(27,23): error CS0122: 'Customer.Customer(int, string, System.DateTime)' is inaccessible due to its protection level
PluckMe.cs(29,54): error CS0103: The name '_customers' does not exist in the current context

OK, never mind the Customer class doesn't have a public constructor. Let's fix it and move along. Obviously, compilation accuracy was not of utmost importance. You were only trying to illustrate a point, right? Last error is my bad, which was a copy of some magical _customers reference that I also copied off the original blog entry as well (assuming some reasonable context). Fixed...made constructor public and changed '_customers' to 'customers'. Moving on.

(Cont'd...)

Reply

posted by atifazizatifaziz(1000) 4 years, 4 months ago 0

(...Cont'd)

Next run of compilation and o'my, the console window is having helluva time just scrolling away at:

PluckMe.cs(23,23): error CS1502: The best overloaded method match for 'Customer.Customer(int, string, System.DateTime)' has some invalid arguments
PluckMe.cs(23,40): error CS1503: Argument '2': cannot convert from 'System.DateTime' to 'string'
PluckMe.cs(23,67): error CS1503: Argument '3': cannot convert from 'string' to 'System.DateTime'
PluckMe.cs(24,23): error CS1502: The best overloaded method match for 'Customer.Customer(int, string, System.DateTime)' has some invalid arguments
PluckMe.cs(24,40): error CS1503: Argument '2': cannot convert from 'System.DateTime' to 'string'
PluckMe.cs(24,67): error CS1503: Argument '3': cannot convert from 'string' to 'System.DateTime'
PluckMe.cs(25,23): error CS1502: The best overloaded method match for 'Customer.Customer(int, string, System.DateTime)' has some invalid arguments
PluckMe.cs(25,39): error CS1503: Argument '2': cannot convert from 'System.DateTime' to 'string'
PluckMe.cs(25,66): error CS1503: Argument '3': cannot convert from 'string' to 'System.DateTime'
PluckMe.cs(26,23): error CS1502: The best overloaded method match for 'Customer.Customer(int, string, System.DateTime)' has some invalid arguments
PluckMe.cs(26,39): error CS1503: Argument '2': cannot convert from 'System.DateTime' to 'string'
PluckMe.cs(26,67): error CS1503: Argument '3': cannot convert from 'string' to 'System.DateTime'
PluckMe.cs(28,22): error CS1502: The best overloaded method match for 'System.Array.ConvertAll<int,Customer>(int[], System.Converter<int,Customer>)' has some invalid arguments
PluckMe.cs(28,54): error CS1503: Argument '1': cannot convert from 'System.Collections.Generic.List<Customer>' to 'int[]'
PluckMe.cs(28,65): error CS1503: Argument '2': cannot convert from 'anonymous method' to 'System.Converter<int,Customer>'

OK, so it looks like that the Customer class constructor has the parameters in the 2nd and 3rd position swapped. Again, never mind accuracy, let's just try to get this baby compiling. We're trying keep our eye on the big plucking picture here, right? Swapped! Move along...Constructor now reads:

public Customer(int age, DateTime birthday, string name)

Last 3 errors seem to be the crux of this fanfare. I'll take it for now that I was just plain sloppy and return back to the rationale for it in a moment. What's the quick fix here? Ah, I see customers is typed as List<Customer> so let's just call List<T>.CovnertAll<TOutput> instead. The output needs to be an array of integers, you say? Let's call ToArray on the result. Bottom line, corrected version becomes:

int[] ages = customers.ConvertAll<int>(delegate(Customer c) { return c.Age; }).ToArray();

Compiles and runs! Phew! Never mind plucking, this was an awesome journey into csc.exe! So why did I even bother posting the original comment as the solution? Well, it was the shortest adaptation of your bottom line that I could think of:

int[] ages = ArrayUtils.Pluck<int, Customer>(_customers, delegate(Customer c) { return c.Age; });

If the class is called ArrayUtils, I would say the first parameter should be naturally typed as T[]. Otherwise, you might as well have called the containing class ListUtils. And if the source is a list yet the output is an array then that causes unnecessary surprises (don't want to get into API design here too much). Consequently, the rationale behind the following line was that it's the closest adaptation of your concluding line to what the BCL already offers (yes, given an array):

int[] ages = Array.ConvertAll<int, Customer>(_customers, delegate(Customer c) { return c.Age; });

(Cont'd...)

Reply

posted by atifazizatifaziz(1000) 4 years, 4 months ago 0

(...Cont'd)

Anyway, I see you've taken my original comment to heart. I had no intentions of making a public assault and I'm sorry if it came across that way to you. Rather, I was trying to comment on why the submitted story (from a learning DNK community perspective) is not getting my kick. Perhaps I should focus on the stories I'd kick rather than giving justification for those I wouldn't. That's the lesson I'm taking away here. I also think, though, that if people are going to submit their own blog entries as stories then they need to grow a thicker skin and take comments as positive criticism or otherwise prompt clarification.

Reply

posted by chrcar01chrcar01(560) 4 years, 4 months ago 0

Thicker skin, yep, I prolly need more of that.

After today's gentle use of it in action it's a little annoying. we have domain classes with names like AgreementParticipant and it was lame to have to do this:

int[] ccids = ArrayUtils.Pluck<int, AgreementParticipant>(participants, delegate(AgreementParticipant p){ return p.CoreCustomerIdentifer; })

so as of now, i'm going to stop Plucking myself. ..I just like saying Pluck...

Reply

information Login or create an account to comment on this story