Earlier quoted context omitted.
If you want more than a one-line lambda, however, you're in for a rough time, and need to go the route of def foo(): # ... four lines here ... modified = map(foo, items)
What's rough about giving a function a name?
For example, `f` is a refactoring of an anonymous function:
function f (item) {
return {
// foo properties
}
}
const fooItems = values.map(item => f)
Imagine that you have to map over several different collections of items, and generating the lists `foo`, `bar`, and `baz`. You have to either name the functions we pass to map `f`, `g`, and `h` (which I bet our reviewers would hate), or `fooMapFunc`, `barMapFunc`, `bazMapFunc`. It just pollutes the namespace that I have to keep in my head, because I have to wonder "is this used somewhere else?".Moreover, is it _more readable_ to define the helpers first, and then the collections that are made by using them, or to define the pairs (fooMapFunc, fooItems) in sequence? This could easily be felt one way or the other, and both are valid, but leads to code review holy wars. "I feel this is more readable / I feel exactly opposite".
For comparison, the anonymous version of this similar operation:
const fooItems = values.map((item) => {
return {
// foo properties
}
})
const barItems = values.map((item) => {
return {
// bar properties
}
})
const bazItems = values.map((item) => {
return {
// baz properties
}
})
In this case, the anonymous function is clearly not usable anywhere else, so it's easier to be sure that it's something one can change, and completely removes the chance of holy war over readability of whether to define helper functions together or with their collection.