10
10
//
11
11
//===----------------------------------------------------------------------===//
12
12
13
- /// Returns the lesser of `x` and `y` .
13
+ /// Returns the lesser of two comparable values .
14
14
///
15
- /// If `x == y`, returns `x`.
15
+ /// - Parameters:
16
+ /// - x: A value to compare.
17
+ /// - y: Another value to compare.
18
+ /// - Returns: The lesser of `x` and `y`. If `x` is equal to `y`, returns `x`.
16
19
public func min< T : Comparable > ( _ x: T , _ y: T ) -> T {
17
20
// In case `x == y` we pick `x`.
18
21
// This preserves any pre-existing order in case `T` has identity,
@@ -23,7 +26,13 @@ public func min<T : Comparable>(_ x: T, _ y: T) -> T {
23
26
24
27
/// Returns the least argument passed.
25
28
///
26
- /// If there are multiple equal least arguments, returns the first one.
29
+ /// - Parameters:
30
+ /// - x: A value to compare.
31
+ /// - y: Another value to compare.
32
+ /// - z: A third value to compare.
33
+ /// - rest: Zero or more additional values.
34
+ /// - Returns: The least of all the arguments. If there are multiple equal
35
+ /// least arguments, the result is the first one.
27
36
public func min< T : Comparable > ( _ x: T , _ y: T , _ z: T , _ rest: T ... ) -> T {
28
37
var minValue = min ( min ( x, y) , z)
29
38
// In case `value == minValue`, we pick `minValue`. See min(_:_:).
@@ -33,17 +42,26 @@ public func min<T : Comparable>(_ x: T, _ y: T, _ z: T, _ rest: T...) -> T {
33
42
return minValue
34
43
}
35
44
36
- /// Returns the greater of `x` and `y` .
45
+ /// Returns the greater of two comparable values .
37
46
///
38
- /// If `x == y`, returns `y`.
47
+ /// - Parameters:
48
+ /// - x: A value to compare.
49
+ /// - y: Another value to compare.
50
+ /// - Returns: The greater of `x` and `y`. If `x` is equal to `y`, returns `y`.
39
51
public func max< T : Comparable > ( _ x: T , _ y: T ) -> T {
40
52
// In case `x == y`, we pick `y`. See min(_:_:).
41
53
return y >= x ? y : x
42
54
}
43
55
44
56
/// Returns the greatest argument passed.
45
57
///
46
- /// If there are multiple equal greatest arguments, returns the last one.
58
+ /// - Parameters:
59
+ /// - x: A value to compare.
60
+ /// - y: Another value to compare.
61
+ /// - z: A third value to compare.
62
+ /// - rest: Zero or more additional values.
63
+ /// - Returns: The greatest of all the arguments. If there are multiple equal
64
+ /// greatest arguments, the result is the last one.
47
65
public func max< T : Comparable > ( _ x: T , _ y: T , _ z: T , _ rest: T ... ) -> T {
48
66
var maxValue = max ( max ( x, y) , z)
49
67
// In case `value == maxValue`, we pick `value`. See min(_:_:).
@@ -53,18 +71,20 @@ public func max<T : Comparable>(_ x: T, _ y: T, _ z: T, _ rest: T...) -> T {
53
71
return maxValue
54
72
}
55
73
56
- /// The iterator for `EnumeratedSequence`. `EnumeratedIterator`
57
- /// wraps a `Base` iterator and yields successive `Int` values,
58
- /// starting at zero, along with the elements of the underlying
59
- /// `Base`:
74
+ /// The iterator for `EnumeratedSequence`.
75
+ ///
76
+ /// An instance of `EnumeratedIterator` wraps a base iterator and yields
77
+ /// successive `Int` values, starting at zero, along with the elements of the
78
+ /// underlying base iterator. The following example enumerates the elements of
79
+ /// an array:
60
80
///
61
81
/// var iterator = ["foo", "bar"].enumerated().makeIterator()
62
82
/// iterator.next() // (0, "foo")
63
83
/// iterator.next() // (1, "bar")
64
84
/// iterator.next() // nil
65
85
///
66
- /// - Note: Idiomatic usage is to call `enumerate` instead of
67
- /// constructing an `EnumerateIterator` directly .
86
+ /// To create an instance of `EnumeratedIterator`, call
87
+ /// `enumerated().makeIterator()` on a sequence or collection .
68
88
public struct EnumeratedIterator <
69
89
Base : IteratorProtocol
70
90
> : IteratorProtocol , Sequence {
@@ -91,14 +111,22 @@ public struct EnumeratedIterator<
91
111
}
92
112
}
93
113
94
- /// The type of the `enumerated()` property.
114
+ /// An enumeration of the elements of a sequence or collection.
115
+ ///
116
+ /// `EnumeratedSequence` is a sequence of pairs (*n*, *x*), where *n*s are
117
+ /// consecutive `Int` values starting at zero, and *x*s are the elements of a
118
+ /// base sequence.
95
119
///
96
- /// `EnumeratedSequence` is a sequence of pairs (*n*, *x*), where *n*s
97
- /// are consecutive `Int`s starting at zero, and *x*s are the elements
98
- /// of a `Base` `Sequence`:
120
+ /// To create an instance of `EnumeratedSequence`, call `enumerated()` on a
121
+ /// sequence or collection. The following example enumerates the elements of
122
+ /// an array.
99
123
///
100
124
/// var s = ["foo", "bar"].enumerated()
101
- /// Array(s) // [(0, "foo"), (1, "bar")]
125
+ /// for (n, x) in s {
126
+ /// print("\(n): \(x)")
127
+ /// }
128
+ /// // Prints "0: foo"
129
+ /// // Prints "1: bar"
102
130
public struct EnumeratedSequence < Base : Sequence > : Sequence {
103
131
internal var _base : Base
104
132
@@ -108,8 +136,6 @@ public struct EnumeratedSequence<Base : Sequence> : Sequence {
108
136
}
109
137
110
138
/// Returns an iterator over the elements of this sequence.
111
- ///
112
- /// - Complexity: O(1).
113
139
public func makeIterator( ) -> EnumeratedIterator < Base . Iterator > {
114
140
return EnumeratedIterator ( _base: _base. makeIterator ( ) )
115
141
}
0 commit comments