atifaziz


Comments:

Pluck Me!

posted by atifazizatifaziz(1000) 4 years, 7 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

One Language

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

Each year that I think I'm done learning languages or that I can rely on the current set for a few years, I find myself diving into another. The upside is, I'm still not bored after 20 years. :)

Reply

Pluck Me!

posted by atifazizatifaziz(1000) 4 years, 7 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

Pluck Me!

posted by atifazizatifaziz(1000) 4 years, 7 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

Pluck Me!

posted by atifazizatifaziz(1000) 4 years, 7 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

.NET Framework 3.5 Is Shared Source

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

Great move, indeed, that it deserves a kick of a lifetime!

Reply

F# to become 1st class .NET language

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

Wish they'd give IronPython a bit of the same love, which has been in release since a while now. It would be sweet to get fully-supported Visual Studio integration. It is there as an integration sample as part of the VS 2005 SDK, but then again, it's just a sample (albeit an impressive one).

Reply

Dangers of the new ASP.NET MVC framework

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

Yep, this kind of "great divide" has existed in the C/C++ world for a long time and it sorted itself out alright even if it was annoying at time. Anyone remember Windows through the lens of the straight C API versus MFC versus ATL and WTL? Hey, what do we call those programmers today anyhow? "Unmanaged" programmers? Sorry, couldn't resist. :)

Reply

List of countries, cities, languages

posted by atifazizatifaziz(1000) 3 years, 8 months ago 0

> Any idea how often these are updated?

@senfo: This is just sample data from MySQL dev docs and to play around with in MySQL. See “Setting Up the world Database” over at:

http://dev.mysql.com/doc/world-setup/en/world-setup.html

I wouldn't count on it being kept up to date. It's probably a snapshot from some point in time. The doc does, however, quote the source (“The sample data used in the world database is Copyright Statistics Finland”) so you may just want to go directly there for the most up to date versions:

http://www.stat.fi/worldinfigures

Reply