<p>I love list comprehensions in Python, because they concisely represent a transformation of a list.</p>

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

<pre><code>foreach (int x in intArray)
  if (x &gt; 3) //generic condition on x
    x++ 
    //do other processing
</code></pre>

<p>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?</p>

[{'answer_id': 13399, 'author': 'jdd', 'author_id': 242853, 'author_profile': 'https://Stackoverflow.com/users/242853', 'pm_score': -1, 'selected': False, 'text': '<p>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 <a href="http://blogs.msdn.com/devdev/archive/2006/06/30/652802.aspx" rel="nofollow noreferrer">this</a> page, .NET 2.0 calls map "ConvertAll".</p>\n\n<p>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.</p>\n'}, {'answer_id': 13403, 'author': 'James A. Rosen', 'author_id': 1190, 'author_profile': 'https://Stackoverflow.com/users/1190', 'pm_score': 0, 'selected': False, 'text': '<p>in Ruby:</p>\n\n<pre><code>intArray.select { |x| x &gt; 3 }.each do |x|\n  # do other processing\nend\n</code></pre>\n\n<p>or if "other processing" is a short one-liner:</p>\n\n<pre><code>intArray.select { |x| x &gt; 3 }.each { |x| something_that_uses x }\n</code></pre>\n\n<p>lastly, if you want to return a new array containing the results of the processing of those elements greater than 3:</p>\n\n<pre><code>intArray.select { |x| x &gt; 3 }.map { |x| do_something_to x }\n</code></pre>\n'}, {'answer_id': 13405, 'author': 'Patrick', 'author_id': 429, 'author_profile': 'https://Stackoverflow.com/users/429', 'pm_score': 1, 'selected': False, 'text': '<p>In Python, you have <a href="http://docs.python.org/tut/node7.html#SECTION007130000000000000000" rel="nofollow noreferrer">filter and map</a>, which can so what you want:</p>\n\n<pre><code>map(lambda x: foo(x + 1) filter(lambda x: x &gt; 3, intArray))\n</code></pre>\n\n<p>There\'s also <a href="http://docs.python.org/tut/node7.html#SECTION007140000000000000000" rel="nofollow noreferrer">list comprehensions</a> which can do both in one easy statement:</p>\n\n<pre><code>[f(x + 1) for x in intArray if x &gt; 3]\n</code></pre>\n'}, {'answer_id': 13406, 'author': 'Lars Mæhlum', 'author_id': 960, 'author_profile': 'https://Stackoverflow.com/users/960', 'pm_score': 2, 'selected': False, 'text': '<p>In C# you can apply selective processing on anything that lives inside an IEnumerable like this:  </p>\n\n<pre><code>intArray.Where(i =&gt; i &gt; 3).ConvertAll();\nDoStuff(intArray.Where(i =&gt; i 3));\n</code></pre>\n\n<p>Etc..</p>\n'}, {'answer_id': 13435, 'author': 'Zooba', 'author_id': 891, 'author_profile': 'https://Stackoverflow.com/users/891', 'pm_score': 4, 'selected': True, 'text': '<p>The increment in the original <code>foreach</code> loop will not affect the contents of the array, the only way to do this remains a <code>for</code> loop:</p>\n\n<pre><code>for(int i = 0; i &lt; intArray.Length; ++i)\n{\n    if(intArray[i] &gt; 3) ++intArray[i];\n}\n</code></pre>\n\n<p>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:</p>\n\n<pre><code>var newArray1 = from i in intArray select ((i &gt; 3) ? (i + 1) : (i));\nvar newArray2 = intArray.Select(i =&gt; (i &gt; 3) ? (i + 1) : (i));\n</code></pre>\n\n<p>Using <code>where</code> (or equivalent), as shown in some of the other answers, will exclude any values less than or equal to 3 from the resulting sequence.</p>\n\n<pre><code>var intArray = new int[] { 10, 1, 20, 2 };\nvar newArray = from i in intArray where i &gt; 3 select i + 1;\n// newArray == { 11, 21 }\n</code></pre>\n\n<p>There is a <code>ForEach</code> method on arrays that will allow you to use a lambda function instead of a <code>foreach</code> block, though for anything more than a method call I would stick with <code>foreach</code>.</p>\n\n<pre><code>intArray.ForEach(i =&gt; DoSomething(i));\n</code></pre>\n'}, {'answer_id': 6477444, 'author': 'sam', 'author_id': 778942, 'author_profile': 'https://Stackoverflow.com/users/778942', 'pm_score': 0, 'selected': False, 'text': '<pre><code>map(lambda x: test(x + 1) filter(lambda x: x &gt; 3, arr))\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/13396', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1266/']