Swift standard library methods naming

In Swift naming guidline Swift.org - API Design Guidelines

"When the operation is naturally described by a verb , use the verb’s imperative for the mutating method and apply the “ed” or “ing” suffix to name its nonmutating counterpart."
x.sort() = x.sorted()|
x.append(y) = x.appending(y)

Why Array - non mutating functions filter/map and not filtered/mapped?

There’s a competing guideline below:

Embrace precedent. Don’t optimize terms for the total beginner at the expense of conformance to existing culture.

map and filter and reduce have a long history (older than C, I believe!) and so in these specific cases we decided to stick with the standard names rather than modify them. We did discuss it, though, because you’re right: either they’re inconsistent with other programming environments, or they’re inconsistent with other stdlib methods.

(Personally I think that if we were to diverge from tradition for these particular functions, we’d use applying instead of mapped. I also have a soft spot for Ruby’s collect, select/reject, and inject, but that’s more because it’s cute that English has rhyming words for these operations.)

5 Likes

I don't think having "correct" naming and choosing "sort" / "filter" for mutating method and "sorted" / "filtered" for non mutating counterparts would divert us significantly far from the tradition. "If we were choosing the naming today what would we choose?". Swift language itself was about breaking some traditions..

We did “choose the name today”, in Swift 3. We had those discussions (at Apple, less so but still some in the community). We picked something, and it’s not everyone’s first choice, but it’s unlikely to change.

2 Likes

It would confuse me if filter were renamed to select, because in C# the Select method is equivalent to map (the equivalent to filter is Where). Similarly, collect reminds me of the Java method, which is more like reduce or init(_:) than map.

On the other hand, the existing methods have the exact same names as their counterparts in other languages (JS has all three, for example) and don't have any name conflicts that I'm aware of.

1 Like