-
Notifications
You must be signed in to change notification settings - Fork 731
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #872 from apollographql/add/extensions
Convenience extensions and reorganization
- Loading branch information
Showing
13 changed files
with
190 additions
and
154 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import Foundation | ||
|
||
// MARK: - Emptiness + Optionality | ||
|
||
extension Collection { | ||
|
||
/// Convenience helper to make `guard` statements more readable | ||
/// | ||
/// - returns: `true` if the collection has contents. | ||
var isNotEmpty: Bool { | ||
return !self.isEmpty | ||
} | ||
} | ||
|
||
extension Optional where Wrapped: Collection { | ||
|
||
/// - returns: `true` if the collection is empty or nil | ||
var isEmptyOrNil: Bool { | ||
switch self { | ||
case .none: | ||
return true | ||
case .some(let collection): | ||
return collection.isEmpty | ||
} | ||
} | ||
|
||
/// - returns: `true` if the collection is non-nil AND has contents. | ||
var isNotEmpty: Bool { | ||
switch self { | ||
case .none: | ||
return false | ||
case .some(let collection): | ||
return collection.isNotEmpty | ||
} | ||
} | ||
} | ||
|
||
// MARK: - Unzipping | ||
// MARK: Arrays of tuples to tuples of arrays | ||
|
||
public func unzip<Element1, Element2>(_ array: [(Element1, Element2)]) -> ([Element1], [Element2]) { | ||
var array1: [Element1] = [] | ||
var array2: [Element2] = [] | ||
|
||
for element in array { | ||
array1.append(element.0) | ||
array2.append(element.1) | ||
} | ||
|
||
return (array1, array2) | ||
} | ||
|
||
public func unzip<Element1, Element2, Element3>(_ array: [(Element1, Element2, Element3)]) -> ([Element1], [Element2], [Element3]) { | ||
var array1: [Element1] = [] | ||
var array2: [Element2] = [] | ||
var array3: [Element3] = [] | ||
|
||
for element in array { | ||
array1.append(element.0) | ||
array2.append(element.1) | ||
array3.append(element.2) | ||
} | ||
|
||
return (array1, array2, array3) | ||
} |
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
public extension Dictionary { | ||
static func += (lhs: inout Dictionary, rhs: Dictionary) { | ||
lhs.merge(rhs) { (_, new) in new } | ||
} | ||
} | ||
|
||
extension Dictionary { | ||
init<S: Sequence>(_ entries: S) where S.Iterator.Element == Element { | ||
self = Dictionary(minimumCapacity: entries.underestimatedCount) | ||
for (key, value) in entries { | ||
self[key] = value | ||
} | ||
} | ||
} |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
struct GroupedSequence<Key: Equatable, Value> { | ||
private(set) var keys: [Key] = [] | ||
fileprivate var groupsForKeys: [[Value]] = [] | ||
|
||
mutating func append(value: Value, forKey key: Key) -> (Int, Int) { | ||
if let index = keys.firstIndex(where: { $0 == key }) { | ||
groupsForKeys[index].append(value) | ||
return (index, groupsForKeys[index].endIndex - 1) | ||
} else { | ||
keys.append(key) | ||
groupsForKeys.append([value]) | ||
return (keys.endIndex - 1, 0) | ||
} | ||
} | ||
} | ||
|
||
extension GroupedSequence: Sequence { | ||
func makeIterator() -> GroupedSequenceIterator<Key, Value> { | ||
return GroupedSequenceIterator(base: self) | ||
} | ||
} | ||
|
||
struct GroupedSequenceIterator<Key: Equatable, Value>: IteratorProtocol { | ||
private var base: GroupedSequence<Key, Value> | ||
|
||
private var keyIterator: EnumeratedSequence<Array<Key>>.Iterator | ||
|
||
init(base: GroupedSequence<Key, Value>) { | ||
self.base = base | ||
keyIterator = base.keys.enumerated().makeIterator() | ||
} | ||
|
||
mutating func next() -> (Key, [Value])? { | ||
if let (index, key) = keyIterator.next() { | ||
let values = base.groupsForKeys[index] | ||
return (key, values) | ||
} else { | ||
return nil | ||
} | ||
} | ||
} |
Oops, something went wrong.