Skip to content

Instantly share code, notes, and snippets.

@paultopia
Created February 10, 2019 04:57
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 paultopia/e9b4803ce500396d38ee787c11d585f5 to your computer and use it in GitHub Desktop.
Save paultopia/e9b4803ce500396d38ee787c11d585f5 to your computer and use it in GitHub Desktop.
just holding onto this for myself: reshuffling an array with generics
let arr = ["a", "b", "c"]
let arr2 = [1, 2, 3]
func moveOver<Item>(_ arr: [Item], start: Int, dest: Int) -> [Item] {
var out = arr
let rem = out.remove(at: start)
out.insert(rem, at: dest)
return out
}
moveOver(arr, start: 0, dest: 2)
moveOver(arr, start: 2, dest: 0)
moveOver(arr, start: 1, dest: 0)
moveOver(arr, start: 1, dest: 2)
moveOver(arr, start: 0, dest: 1)
moveOver(arr, start: 2, dest: 1)
moveOver(arr2, start: 0, dest: 2)
extension Array {
func move(start: Int, dest: Int) -> [Element] {
var out = self
let rem = out.remove(at: start)
out.insert(rem, at: dest)
return out
}
}
arr.move(start: 0, dest: 2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment