Created
March 10, 2018 21:10
-
-
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
This file contains 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
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