mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
[stdlib] Revise stdlib documentation comments
- Expand pre-example explanations - Update documentation for SE-0118 - Removing remaining 'iff' usage - Revise Array discussion - Fix formIndex(_:offsetBy) parameter formatting - Improve index/formIndex(_:offsetBy:(limitedBy:)?) discussion - Update Quick Look discussions - Fixes grammar inconsistencies - Adds parameter / return documentation - Adds and expands on examples - Revises AnyObject discussion for new `id` bridging rules - Revise readLine, print, and assertion functions - Add missing docs to String index-moving methods
This commit is contained in:
@@ -10,9 +10,12 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
/// Returns the lesser of `x` and `y`.
|
||||
/// Returns the lesser of two comparable values.
|
||||
///
|
||||
/// If `x == y`, returns `x`.
|
||||
/// - Parameters:
|
||||
/// - x: A value to compare.
|
||||
/// - y: Another value to compare.
|
||||
/// - Returns: The lesser of `x` and `y`. If `x` is equal to `y`, returns `x`.
|
||||
public func min<T : Comparable>(_ x: T, _ y: T) -> T {
|
||||
// In case `x == y` we pick `x`.
|
||||
// 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 {
|
||||
|
||||
/// Returns the least argument passed.
|
||||
///
|
||||
/// If there are multiple equal least arguments, returns the first one.
|
||||
/// - Parameters:
|
||||
/// - x: A value to compare.
|
||||
/// - y: Another value to compare.
|
||||
/// - z: A third value to compare.
|
||||
/// - rest: Zero or more additional values.
|
||||
/// - Returns: The least of all the arguments. If there are multiple equal
|
||||
/// least arguments, the result is the first one.
|
||||
public func min<T : Comparable>(_ x: T, _ y: T, _ z: T, _ rest: T...) -> T {
|
||||
var minValue = min(min(x, y), z)
|
||||
// In case `value == minValue`, we pick `minValue`. See min(_:_:).
|
||||
@@ -33,9 +42,12 @@ public func min<T : Comparable>(_ x: T, _ y: T, _ z: T, _ rest: T...) -> T {
|
||||
return minValue
|
||||
}
|
||||
|
||||
/// Returns the greater of `x` and `y`.
|
||||
/// Returns the greater of two comparable values.
|
||||
///
|
||||
/// If `x == y`, returns `y`.
|
||||
/// - Parameters:
|
||||
/// - x: A value to compare.
|
||||
/// - y: Another value to compare.
|
||||
/// - Returns: The greater of `x` and `y`. If `x` is equal to `y`, returns `y`.
|
||||
public func max<T : Comparable>(_ x: T, _ y: T) -> T {
|
||||
// In case `x == y`, we pick `y`. See min(_:_:).
|
||||
return y >= x ? y : x
|
||||
@@ -43,7 +55,13 @@ public func max<T : Comparable>(_ x: T, _ y: T) -> T {
|
||||
|
||||
/// Returns the greatest argument passed.
|
||||
///
|
||||
/// If there are multiple equal greatest arguments, returns the last one.
|
||||
/// - Parameters:
|
||||
/// - x: A value to compare.
|
||||
/// - y: Another value to compare.
|
||||
/// - z: A third value to compare.
|
||||
/// - rest: Zero or more additional values.
|
||||
/// - Returns: The greatest of all the arguments. If there are multiple equal
|
||||
/// greatest arguments, the result is the last one.
|
||||
public func max<T : Comparable>(_ x: T, _ y: T, _ z: T, _ rest: T...) -> T {
|
||||
var maxValue = max(max(x, y), z)
|
||||
// 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 {
|
||||
return maxValue
|
||||
}
|
||||
|
||||
/// The iterator for `EnumeratedSequence`. `EnumeratedIterator`
|
||||
/// wraps a `Base` iterator and yields successive `Int` values,
|
||||
/// starting at zero, along with the elements of the underlying
|
||||
/// `Base`:
|
||||
/// The iterator for `EnumeratedSequence`.
|
||||
///
|
||||
/// An instance of `EnumeratedIterator` wraps a base iterator and yields
|
||||
/// successive `Int` values, starting at zero, along with the elements of the
|
||||
/// underlying base iterator. The following example enumerates the elements of
|
||||
/// an array:
|
||||
///
|
||||
/// var iterator = ["foo", "bar"].enumerated().makeIterator()
|
||||
/// iterator.next() // (0, "foo")
|
||||
/// iterator.next() // (1, "bar")
|
||||
/// iterator.next() // nil
|
||||
///
|
||||
/// - Note: Idiomatic usage is to call `enumerate` instead of
|
||||
/// constructing an `EnumerateIterator` directly.
|
||||
/// To create an instance of `EnumeratedIterator`, call
|
||||
/// `enumerated().makeIterator()` on a sequence or collection.
|
||||
public struct EnumeratedIterator<
|
||||
Base : IteratorProtocol
|
||||
> : IteratorProtocol, Sequence {
|
||||
@@ -91,14 +111,22 @@ public struct EnumeratedIterator<
|
||||
}
|
||||
}
|
||||
|
||||
/// The type of the `enumerated()` property.
|
||||
/// An enumeration of the elements of a sequence or collection.
|
||||
///
|
||||
/// `EnumeratedSequence` is a sequence of pairs (*n*, *x*), where *n*s
|
||||
/// are consecutive `Int`s starting at zero, and *x*s are the elements
|
||||
/// of a `Base` `Sequence`:
|
||||
/// `EnumeratedSequence` is a sequence of pairs (*n*, *x*), where *n*s are
|
||||
/// consecutive `Int` values starting at zero, and *x*s are the elements of a
|
||||
/// base sequence.
|
||||
///
|
||||
/// To create an instance of `EnumeratedSequence`, call `enumerated()` on a
|
||||
/// sequence or collection. The following example enumerates the elements of
|
||||
/// an array.
|
||||
///
|
||||
/// var s = ["foo", "bar"].enumerated()
|
||||
/// Array(s) // [(0, "foo"), (1, "bar")]
|
||||
/// for (n, x) in s {
|
||||
/// print("\(n): \(x)")
|
||||
/// }
|
||||
/// // Prints "0: foo"
|
||||
/// // Prints "1: bar"
|
||||
public struct EnumeratedSequence<Base : Sequence> : Sequence {
|
||||
internal var _base: Base
|
||||
|
||||
@@ -108,8 +136,6 @@ public struct EnumeratedSequence<Base : Sequence> : Sequence {
|
||||
}
|
||||
|
||||
/// Returns an iterator over the elements of this sequence.
|
||||
///
|
||||
/// - Complexity: O(1).
|
||||
public func makeIterator() -> EnumeratedIterator<Base.Iterator> {
|
||||
return EnumeratedIterator(_base: _base.makeIterator())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user