TIL different kinds of ranges in Swift
Watched first video from missed Vapor meetup. 0...
- that’s new for me. It seems I have read about this kinds of ranges but have forgotten. TIL this range is called PartialRangeFrom
. Beside regular half-open range Range
swift also has PartialRangeThrough
, PartialRangeUpTo
, and UnboundedRange_
, yes, with undersscore at the end. There’re also CountableRange
, CountablePartialRangeFrom
.
TIL Swift has a thing called DefaultIndices
, a collection of indices for an arbitrary collection.
TIL Swift’s RangeExpression
protocol has relative(to:)
function which allows to convert range expression which might be missing one of one or both of its endpoints, into a concrete range that is bounded on both sides.
let numbers = [10, 20, 30, 40, 50, 60, 70]
let upToFour: PartialRangeUpTo<Int> = ..<4
print(upToFour) // PartialRangeUpTo<Int>(upperBound: 4)
let r1: Range<Int> = upToFour.relative(to: numbers)
print(r1) // 0..<4