views:

356

answers:

5

I love list comprehensions in Python, because they concisely represent a transformation of a list.

However, in other languages, I frequently find myself writing something along the lines of:

foreach (int x in intArray)
  if (x > 3) //generic condition on x
    x++ 
    //do other processing

This example is in C#, where I'm under the impression LINQ can help with this, but is there some common programming construct which can replace this slightly less-than-elegant solution? Perhaps a data structure I'm not considering?

A: 

Depends on the language and what you need to do, a "map" as it's called in many languages could be what you're looking for. I don't know C#, but according to this page, .NET 2.0 calls map "ConvertAll".

The meaning of "map" is pretty simple - take a list, and apply a function to each element of it, returning a new list. You may also be looking for "filter", which would give you a list of items that satisfy a predicate in another list.

jeremiahd
A: 

in Ruby:

intArray.select { |x| x > 3 }.each do |x|
  # do other processing
end

or if "other processing" is a short one-liner:

intArray.select { |x| x > 3 }.each { |x| something_that_uses x }

lastly, if you want to return a new array containing the results of the processing of those elements greater than 3:

intArray.select { |x| x > 3 }.map { |x| do_something_to x }
James A. Rosen
+1  A: 

In Python, you have filter and map, which can so what you want:

map(lambda x: foo(x + 1) filter(lambda x: x > 3, intArray))

There's also list comprehensions which can do both in one easy statement:

[f(x + 1) for x in intArray if x > 3]
Patrick
+1  A: 

In C# you can apply selective processing on anything that lives inside an IEnumerable like this:

intArray.Where(i => i > 3).ConvertAll();
DoStuff(intArray.Where(i => i 3));

Etc..

Lars Mæhlum
+2  A: 

The increment in the original foreach loop will not affect the contents of the array, the only way to do this remains a for loop:

for(int i = 0; i < intArray.Length; ++i)
{
    if(intArray[i] > 3) ++intArray[i];
}

Linq is not intended to modify existing collections or sequences. It creates new sequences based on existing ones. It is possible to achieve the above code using Linq, though it is slightly against its purposes:

var newArray1 = from i in intArray select ((i > 3) ? (i + 1) : (i));
var newArray2 = intArray.Select(i => (i > 3) ? (i + 1) : (i));

Using where (or equivalent), as shown in some of the other answers, will exclude any values less than or equal to 3 from the resulting sequence.

var intArray = new int[] { 10, 1, 20, 2 };
var newArray = from i in intArray where i > 3 select i + 1;
// newArray == { 11, 21 }

There is a ForEach method on arrays that will allow you to use a lambda function instead of a foreach block, though for anything more than a method call I would stick with foreach.

intArray.ForEach(i => DoSomething(i));
Zooba