Replacement for ... if iteration of the array

I like comprehension of lists in Python because they briefly represent list conversion.

However, in other languages ​​I often write something like:

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

This example is in C #, where I, inspired by LINQ, can help with this, but is there some general programming construct that can replace this slightly less elegant solution? Perhaps a data structure that I am not considering?

+6
python arrays loops iteration
source share
6 answers

An increment of the source foreach does not affect the contents of the array, the only way to do this is with 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. You can achieve the above code with Linq, although it is slightly contrary to its objectives:

 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 the equivalent), as shown in some other answers, excludes from the resulting sequence any values ​​less than or equal to 3.

 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, although for something more than a method call, I would stick with foreach .

 intArray.ForEach(i => DoSomething(i)); 
+5
source share

In C #, you can apply selective processing to everything that lives inside IEnumerable, like this:

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

Etc ..

+2
source share

In Python, you have a filter and a map that may so you want:

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

There is also a list of concepts that can be implemented as in one simple statement:

 [f(x + 1) for x in intArray if x > 3] 
+1
source share

in Ruby:

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

or if the "other processing" is a short single-line layer:

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

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

 intArray.select { |x| x > 3 }.map { |x| do_something_to x } 
0
source share
 map(lambda x: test(x + 1) filter(lambda x: x > 3, arr)) 
0
source share

Depending on the language and what you need to do, a “map,” as it is called in many languages, may be what you are looking for. I do not know C #, but according to this page , .NET 2.0 calls the "ConvertAll" card.

The value of "map" is pretty simple - take a list and apply a function to each element, returning a new list. You can also look for a “filter” that will provide you with a list of elements that match a predicate in another list.

-one
source share

All Articles