-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathExtensions.swift
37 lines (28 loc) · 939 Bytes
/
Extensions.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
//
// Extensions.swift
// Geosphere
//
// Created by Jacob Martin on 4/17/17.
// Copyright © 2017 Jacob Martin. All rights reserved.
//
import Foundation
extension Array {
var powerset: [[Element]] {
guard count > 0 else {
return [[]]
}
// tail contains the whole array BUT the first element
let tail = Array(self[1..<endIndex])
// head contains only the first element
let head = self[0]
// computing the tail's powerset
let withoutHead = tail.powerset
// mergin the head with the tail's powerset
let withHead = withoutHead.map { $0 + [head] }
// returning the tail's powerset and the just computed withHead array
return withHead + withoutHead
}
func combinations(_ size: Int) -> [[Element]]{
return powerset.filter { $0.count == size }
}
}