Created
February 10, 2019 04:57
-
-
Save paultopia/e9b4803ce500396d38ee787c11d585f5 to your computer and use it in GitHub Desktop.
just holding onto this for myself: reshuffling an array with generics
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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