Skip to content

Instantly share code, notes, and snippets.

@alexisakers
Created November 17, 2016 11:50
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexisakers/488ba0a8bcb3c29b23fa3b6a41ed4461 to your computer and use it in GitHub Desktop.
Save alexisakers/488ba0a8bcb3c29b23fa3b6a41ed4461 to your computer and use it in GitHub Desktop.
Remove trailing elements in a Swift array

Array+Suffix

This extension allows you to strip repeating trailing elements in a Swift array.

Examples

Removing trailing zeros

let array = [0,1,2,3,4,5,0,0,0,0,0,0,0,0,0,0]
let stripped = array.removing(suffix: 0)
// stripped = [0,1,2,3,4,5]

Removing trailing empty strings

let array = ["","a","b","c","","",""]
let stripped = array.removing(suffix: "")
// stripped = ["","a","b","c"]

License

The MIT License (MIT)

Copyright (c) 2016 ALEXIS AUBRY RADANOVIC

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

/*
* ==---------------------------------------------------------------------------------==
*
* File : Array+Suffix.swift
* Project : Array+Suffix
* Author : ALEXIS AUBRY RADANOVIC
*
* License : The MIT License (MIT)
*
* ==---------------------------------------------------------------------------------==
*
* The MIT License (MIT)
* Copyright (c) 2016 ALEXIS AUBRY RADANOVIC
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* ==---------------------------------------------------------------------------------==
*/
extension Array where Element: Equatable {
///
/// Removes the trailing elements that match the specified suffix.
///
/// - parameter suffix: The suffix to remove.
///
/// - returns: The initial array without the specified suffix.
///
public func removing(suffix: Element) -> [Element] {
var array = self
var previousValue = suffix
for i in (0..<array.endIndex).reversed() {
let value = array[i]
guard value == previousValue else {
break
}
array.remove(at: i)
previousValue = value
}
return array
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment