Protocol

Strideable

A type representing continuous, one-dimensional values that can be offset and measured. オフセットできて計測できる、連続的な、一次元の値を表している型。

Declaration 宣言

protocol Strideable

Overview 概要

You can use a type that conforms to the Strideable protocol with the stride(from:to:by:) and stride(from:through:by:) functions. For example, you can use stride(from:to:by:) to iterate over an interval of floating-point values: あなたは、Strideableプロトコルに準拠する型をstride(from:to:by:)およびstride(from:through:by:)関数で使うことができます。例えば、あなたはstride(from:to:by:)を使って浮動小数点値のある間隔のすべてにわたって反復できます。


for radians in stride(from: 0.0, to: .pi * 2, by: .pi / 2) {
    let degrees = Int(radians * 180 / .pi)
    print("Degrees: \(degrees), radians: \(radians)")
}
// Degrees: 0, radians: 0.0
// Degrees: 90, radians: 1.5707963267949
// Degrees: 180, radians: 3.14159265358979
// Degrees: 270, radians: 4.71238898038469

The last parameter of these functions is of the associated Stride type—the type that represents the distance between any two instances of the Strideable type. これらの関数の最後のパラメータは、Stride型 — Strideable型の何らかの2つのインスタンスの間の隔たりを表す型の関連型です。

Types that have an integer Stride can be used as the boundaries of a countable range or as the lower bound of an iterable one-sided range. For example, you can iterate over a range of Int and use sequence and collection methods. ある整数Strideを持つこれら型は、可付番範囲の境界としてまたは反復可能な片側範囲の下方境界として使われることができます。例えば、あなたはある範囲のIntのすべてに反復してシーケンスおよびコレクションのメソッドを使うことができます。


var sum = 0
for x in 1...100 {
    sum += x
}
// sum == 5050


let digits = (0..<10).map(String.init)
// ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]

Conforming to the Strideable Protocol Strideableプロトコルに準拠する

To add Strideable conformance to a custom type, choose a Stride type that can represent the distance between two instances and implement the advanced(by:) and distance(to:) methods. For example, this hypothetical Date type stores its value as the number of days before or after January 1, 2000: Strideable準拠をあつらえの型に加えるには、2つのインスタンスの間の隔たりを表現可能であるStride型のひとつを選んで、advanced(by:)distance(to:)メソッドを実装してください。例えば、この仮説上のDate型は、それの値を2000年1月1日の前または後の日数として格納します:


struct Date: Equatable, CustomStringConvertible {
    var daysAfterY2K: Int


    var description: String {
        // ...
    }
}

The Stride type for Date is Int, inferred from the parameter and return types of advanced(by:) and distance(to:): DateのためのStride型はIntです、パラメータと戻り値のadvanced(by:)distance(to:)から推論されます:


extension Date: Strideable {
    func advanced(by n: Int) -> Date {
        var result = self
        result.daysAfterY2K += n
        return result
    }


    func distance(to other: Date) -> Int {
        return other.daysAfterY2K - self.daysAfterY2K
    }
}

The Date type can now be used with the stride(from:to:by:) and stride(from:through:by:) functions and as the bounds of an iterable range. Date型は、今ではstride(from:to:by:)およびstride(from:through:by:)関数でそして反復可能な範囲の境界として使用できます。


let startDate = Date(daysAfterY2K: 0)   // January 1, 2000
let endDate = Date(daysAfterY2K: 15)    // January 16, 2000


for date in stride(from: startDate, to: endDate, by: 7) {
    print(date)
}
// January 1, 2000
// January 8, 2000
// January 15, 2000

Topics 話題

Getting an Offset Value オフセット値の取得

Comparing Values 値の比較

Associated Types さまざまな関連型

Operator Functions 演算子関数

See Also 参照

Basic Arithmetic 基本算術