Skip to content

Instantly share code, notes, and snippets.

@pedantix
Created March 10, 2018 21:10
Show Gist options
  • Save pedantix/82898fc65e2b77f1d04d2b7d77ecf1d7 to your computer and use it in GitHub Desktop.
Save pedantix/82898fc65e2b77f1d04d2b7d77ecf1d7 to your computer and use it in GitHub Desktop.
descendingElementalPairs mocked this extension up to transform [1,2,3] -> [[1, 2], [1, 3] , [2, 3]]. Please suggest names
import Foundation
let anArray = [1, 2, 3, 4, 5]
extension Array where Element: Equatable {
func descendingElementalPairs() -> Array<Array<Element>> {
return self.enumerated()
.map { pair -> Array<Array<Element>> in
self[(pair.offset + 1)...].map {
[pair.element, $0]
}
}.reduce([[Element]]()) { $0 + $1 }
}
}
anArray.descendingElementalPairs().forEach {
print("\($0)")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment