[IronPython] Extension methods...

Thane thane at magna-capital.com
Tue Sep 20 17:23:02 CEST 2005


LINQ is pretty cool -- thanks for the update Keith.

I do find it amusing that many of these "queries" are available in standard
Python through list comprehensions, lambda, etc.

Here's a simple LINQ example:

[Category("Restriction Operators")]
[Title("Where - Simple 1")]
[Description("This sample uses where to find all elements of an array less
than 5.")]
public void Linq1() {
	int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };

	var lowNums =
		from n in numbers
		where n < 5
		select n;
        
	Console.WriteLine("Numbers < 5:");
	foreach (var x in lowNums) {
		Console.WriteLine(x);
	}
}

Here's the Python equivalent:

>>> numbers = [5, 4, 1, 3, 9, 8, 6, 7, 2, 0]
>>> lownums = [n for n in numbers if n < 5]
>>> lownums
[4, 1, 3, 2, 0]
>>>

For most simple extractions of this sort, I prefer the Python list
comprehension.  IMO, the power of LINQ comes from integrating to databases
using the same syntax as you would for other IEnumerable objects.


> -----Original Message-----
> From: users-ironpython.com-bounces at lists.ironpython.com [mailto:users-
> ironpython.com-bounces at lists.ironpython.com] On Behalf Of Keith J. Farmer
> Sent: Monday, September 19, 2005 3:43 PM
> To: users-ironpython.com at lists.ironpython.com
> Subject: [IronPython] Extension methods...
> 
> All the fun Linq bits run on beta 2.  All that's needed is a way to make
> use of extension method attributes, which is remarkably Pythonic:
> foo.Bar(baz) -> Gleep.Bar(foo, baz).  Python already has lambdas, but
> could use a means to generate expression trees rather than functions.
> 
> 
> _______________________________________________
> users-ironpython.com mailing list
> users-ironpython.com at lists.ironpython.com
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com




More information about the Ironpython-users mailing list