Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-8792] Add drop(at:) #51300

Open
swift-ci opened this issue Sep 18, 2018 · 1 comment
Open

[SR-8792] Add drop(at:) #51300

swift-ci opened this issue Sep 18, 2018 · 1 comment
Labels
feature A feature request or implementation standard library Area: Standard library umbrella swift evolution proposal needed Flag → feature: A feature that warrants a Swift evolution proposal

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-8792
Radar None
Original Reporter mattdipasquale (JIRA User)
Type New Feature
Additional Detail from JIRA
Votes 0
Component/s Standard Library
Labels New Feature, LanguageFeatureRequest
Assignee None
Priority Medium

md5: 4ff633c75fae5bc9c26a9302548c4a42

Issue Description:

You might consider adding a function named drop(at:) to Array, ArraySlice, etc.

Note: I'd use remove(at:), but the array I'm working with is a let constant.

func drop(at index: Int) -> ArraySlice<Element> {
  precondition(indices.contains(index), "Index out of range")
  return self[..<index] + self[(index+1)...]
}

It returns a slice of the original array without the element at the specified index.

let numbers = [1, 2, 3, 4, 5]
print(numbers.drop(at: 2))
// Prints "[1, 2, 4, 5]"

See "Swift Array instance method drop(at: Int)" for more discussion.

Note: Maybe dropSubrange(_:) should also be added. This way, every "remove" instance method for removing elements from an array, for which it makes sense, has a corresponding "drop" instance method for excluding elements from an array, for when you're working with an array that's a let constant or an array that you don't want to modify.

@belkadan
Copy link
Contributor

That's not a slice in that it's not a sub-sequence of the original Array. If it's going to make a copy, it might as well return a plain old Array.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@AnthonyLatsis AnthonyLatsis added swift evolution proposal needed Flag → feature: A feature that warrants a Swift evolution proposal and removed new feature labels Nov 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature A feature request or implementation standard library Area: Standard library umbrella swift evolution proposal needed Flag → feature: A feature that warrants a Swift evolution proposal
Projects
None yet
Development

No branches or pull requests

3 participants