swiftlang/swift

[SR-3444] map, filter on RangeReplaceableCollection should return Self

Open

#46,032 opened on Dec 17, 2016

View on GitHub
 (3 comments) (0 reactions) (0 assignees)Swift (69,989 stars) (10,719 forks)batch import
good first issueimprovementstandard libraryswift evolution proposal needed

Description

Previous ID SR-3444
Radar None
Original Reporter @airspeedswift
Type Improvement
Votes 1
Component/s Standard Library
Labels Improvement, StarterProposal, swift-evolution-proposal-needed
Assignee None
Priority Medium

md5: 05669e9e4e1c642618be3a82e1ececb1

Issue Description:

Currently, map and filter on Sequence return an Array, even though the implementation use only capabilities of Array that are available on RangeReplaceableCollection* (an empty init, reserveCapacity, and append).

This means that RangeReplaceableCollection could implement a version of map that returned the same type. For example, a Deque could return another Deque. This is probably what most users would expect for these types.

This would only apply to RRCs that could hold the mapped-to element type. Also note that, in the case of map, this would only apply to transformations that return the same element type – to represent a transformation to another type would require higher-kinder types. And sequences and non-RRC collections, like Set would need to return arrays.

*(this isn't completely true: it creates a ContiguousArray and then turns it into an Array. But hopefully this optimization could be removed at some point)

Contributor guide