Skip to content

Instantly share code, notes, and snippets.

@ushpizin
ushpizin / Permutations.swift
Last active September 8, 2017 08:36 — forked from proxpero/Permutations.swift
Generate the permutations of a Swift array.
//: Permutations
// based on https://www.objc.io/blog/2014/12/08/functional-snippet-10-permutations/
// but updated for Swift 3.0 (Xcode 8.3)
extension Array {
func decompose() -> (Generator.Element, [Generator.Element])? {
guard let x = first else { return nil }
return (x, Array(self[1..<count]))
}
}