Skip to content

Instantly share code, notes, and snippets.

@devxoul
Created October 5, 2016 12:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devxoul/70b83cd7bac5ba1cf0280aec18993ffb to your computer and use it in GitHub Desktop.
Save devxoul/70b83cd7bac5ba1cf0280aec18993ffb to your computer and use it in GitHub Desktop.
Arraying
// The MIT License (MIT)
//
// Copyright (c) 2016 Suyeol Jeon (xoul.kr)
//
// 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 NONINFRINGEMENT. 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.
public extension Array {
public func replacingSubrange<C: Collection>(
_ subrange: Range<Int>,
with newElements: C
) -> Array where C.Iterator.Element == Element {
var newArray = self
newArray.replaceSubrange(subrange, with: newElements)
return newArray
}
public func replacingSubrange<C: Collection>(
_ subrange: ClosedRange<Int>,
with newElements: C
) -> Array where C.Iterator.Element == Element {
var newArray = self
newArray.replaceSubrange(subrange, with: newElements)
return newArray
}
public func replacingSubrange<C: Collection>(
_ subrange: CountableRange<Int>,
with newElements: C
) -> Array where C.Iterator.Element == Element {
var newArray = self
newArray.replaceSubrange(subrange, with: newElements)
return newArray
}
public func replacingSubrange<C: Collection>(
_ subrange: CountableClosedRange<Int>,
with newElements: C
) -> Array where C.Iterator.Element == Element {
var newArray = self
newArray.replaceSubrange(subrange, with: newElements)
return newArray
}
}
public extension Array {
public func appending(_ newElement: Element) -> Array {
var newArray = self
newArray.append(newElement)
return newArray
}
public func appending<S: Sequence>(
contentsOf newElements: S
) -> Array where S.Iterator.Element == Element {
var newArray = self
newArray.append(contentsOf: newElements)
return newArray
}
public func appending<C: Collection>(
contentsOf newElements: C
) -> Array where C.Iterator.Element == Element {
var newArray = self
newArray.append(contentsOf: newElements)
return newArray
}
public func inserting(_ newElement: Element, at i: Int) -> Array {
var newArray = self
newArray.insert(newElement, at: i)
return newArray
}
public func removing(at: Int) -> Array {
var newArray = self
newArray.remove(at: at)
return newArray
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment