Question: Is there an idiomatic way to apply a function to all items in a list ?

For example, in Python, say we wish to capitalize all strings in a list, we can use a loop :

```
regimentNames = ['Night Riflemen', 'Jungle Scouts', 'The Dragoons', 'Midnight Revengence', 'Wily Warriors']
# create a variable for the for loop results
regimentNamesCapitalized_f = []

# for every item in regimentNames
for i in regimentNames:
    # capitalize the item and add it to regimentNamesCapitalized_f
    regimentNamesCapitalized_f.append(i.upper())

```

But a more concise way is:

```
capitalizer = lambda x: x.upper()
regimentNamesCapitalized_m = list(map(capitalizer, regimentNames)); regimentNamesCapitalized_m

```

What is an equivalent way to call a function on all items in a list in Dart ?

Answer: 
The answer seems to be to use anonymous functions, or pass a function to a lists `forEach` method.

Passing a function:

```
void capitalise(var string) {
  var foo = string.toUpperCase();
  print(foo);
}

var list = ['apples', 'bananas', 'oranges'];
list.forEach(capitalise);

```

Using an anonymous function:

```
list.forEach((item){
  print(item.toUpperCase());
});

```

If the function is going to be used only in one place, I think its better to use the anonymous function, as it is easy to read what is happening in the list.

If the function is going to be used in multiple places, then its better to pass the function instead of using an anonymous function.
If you want to apply a function to all items in a `List` (or `Iterable`) *and* collect the results, Dart provides an `Iterable.map` function that is equivalent to Python's `map`:

```dart
// Dart
regimentNamesCapitalized_m = regimentNames.map((x) => x.toUpperCase()).toList();

```

Python also provides *list comprehensions*, which usually are considered more Pythonic and often are preferred to the functional approach:

```py
# Python
regimentNamesCapitalized_m = [x.upper() for x in regimentNames]

```

Dart's equivalent of Python's list comprehensions is *collection-for*:

```dart
// Dart
regimentNamesCapitalized_m = [for (var x in regimentNames) x.toUpperCase()];

```

If you're calling a function for its side-effect and don't care about its return value, you could use `Iterable.forEach` instead of `Iterable.map`. In such cases, however, [I personally prefer explicit loops](https://stackoverflow.com/a/65420010/):

* I think they're more readable by virtue of being more common.
* They're more flexible. You can use `break` or `continue` to control iteration.
* They might be more efficient. `.forEach` involves an extra function call per iteration to invoke the supplied callback.