Files
swift-algorithms/Guides/Stride.md
T

1.9 KiB

Stride

[Source | Tests]

A type that steps over sequence elements by the specified amount.

This is available through the striding(by:) method on any Sequence.

(0...10).striding(by: 2) // == [0, 2, 4, 6, 8, 10]

If the stride is larger than the count, the resulting wrapper only contains the first element.

The stride amount must be a positive value.

Detailed Design

The striding(by:) method is declared as a Sequence extension, and returns a Stride type:

extension Sequence {
  public func striding(by step: Int) -> Stride<Self>
}

A custom Index type is defined so that it's not possible to get confused when trying to access an index of the stride collection.

[0, 1, 2, 3, 4].striding(by: 2)[1] // == 1
[0, 1, 2, 3, 4].striding(by: 2).map { $0 }[1] // == 2

A careful thought was given to the composition of these strides by giving a custom implementation to index(_:offsetBy:limitedBy) which multiplies the offset by the stride amount.

base.index(i.base, offsetBy: distance * stride, limitedBy: base.endIndex)

The following two lines of code are equivalent, including performance:

(0...10).striding(by: 6)
(0...10).striding(by: 2).stride(by: 3)

Complexity

The call to striding(by: k) is always O(1) and access to the next value in the stride is O(1) if the collection conforms to RandomAccessCollection, otherwise O(k).

Comparison with other languages

rust has Strided available in a crate. c++ has std::slice::stride

The semantics of striding described in this documentation are equivalent.