- Notifications
You must be signed in to change notification settings - Fork 162
/
Copy pathDictionary.swift
88 lines (85 loc) · 3.78 KB
/
Dictionary.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Async Algorithms open source project
//
// Copyright (c) 2022 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
//
//===----------------------------------------------------------------------===//
@available(AsyncAlgorithms 1.0,*)
extensionDictionary{
/// Creates a new dictionary from the key-value pairs in the given asynchronous sequence.
///
/// You use this initializer to create a dictionary when you have an asynchronous sequence
/// of key-value tuples with unique keys. Passing an asynchronous sequence with duplicate
/// keys to this initializer results in a runtime error. If your
/// asynchronous sequence might have duplicate keys, use the
/// `Dictionary(_:uniquingKeysWith:)` initializer instead.
///
/// - Parameter keysAndValues: An asynchronous sequence of key-value pairs to use for
/// the new dictionary. Every key in `keysAndValues` must be unique.
/// - Precondition: The sequence must not have duplicate keys.
@available(AsyncAlgorithms 1.0,*)
@inlinable
publicinit<S:AsyncSequence>(uniqueKeysWithValues keysAndValues:S)asyncrethrows
where S.Element ==(Key,Value){
self.init(uniqueKeysWithValues:tryawaitArray(keysAndValues))
}
/// Creates a new dictionary from the key-value pairs in the given asynchronous sequence,
/// using a combining closure to determine the value for any duplicate keys.
///
/// You use this initializer to create a dictionary when you have a sequence
/// of key-value tuples that might have duplicate keys. As the dictionary is
/// built, the initializer calls the `combine` closure with the current and
/// new values for any duplicate keys. Pass a closure as `combine` that
/// returns the value to use in the resulting dictionary: The closure can
/// choose between the two values, combine them to produce a new value, or
/// even throw an error.
///
/// - Parameters:
/// - keysAndValues: An asynchronous sequence of key-value pairs to use for the new
/// dictionary.
/// - combine: A closure that is called with the values for any duplicate
/// keys that are encountered. The closure returns the desired value for
/// the final dictionary, or throws an error if building the dictionary
/// can't proceed.
@available(AsyncAlgorithms 1.0,*)
@inlinable
publicinit<S:AsyncSequence>(
_ keysAndValues:S,
uniquingKeysWith combine:(Value,Value)asyncthrows->Value
)asyncrethrowswhere S.Element ==(Key,Value){
self.init()
fortryawait(key, value)in keysAndValues {
iflet existing =self[key]{
self[key]=tryawaitcombine(existing, value)
}else{
self[key]= value
}
}
}
/// Creates a new dictionary whose keys are the groupings returned by the
/// given closure and whose values are arrays of the elements that returned
/// each key.
///
/// The arrays in the "values" position of the new dictionary each contain at
/// least one element, with the elements in the same order as the source
/// asynchronous sequence.
///
/// - Parameters:
/// - values: An asynchronous sequence of values to group into a dictionary.
/// - keyForValue: A closure that returns a key for each element in
/// `values`.
@available(AsyncAlgorithms 1.0,*)
@inlinable
publicinit<S:AsyncSequence>(grouping values:S, by keyForValue:(S.Element)asyncthrows->Key)asyncrethrows
where Value ==[S.Element]{
self.init()
fortryawaitvaluein values {
letkey=tryawaitkeyForValue(value)
self[key, default:[]].append(value)
}
}
}