|
| 1 | +# Ranges |
| 2 | + |
| 3 | +[Ranges][range] represent an interval between two values. |
| 4 | +The most common types that support ranges are `Int`, `String`, and `Date`. |
| 5 | +They can be used for many things, such as quickly creating a collection, slicing an array, checking if a value is in a range, and iteration. |
| 6 | +They are created using the range operator `...` or `..<` (inclusive and exclusive, respectively). |
| 7 | + |
| 8 | +```swift |
| 9 | +1...5 // A range containing 1, 2, 3, 4, 5 |
| 10 | +1..<5 // A range containing 1, 2, 3, 4 |
| 11 | +``` |
| 12 | + |
| 13 | +The reason for having two range operators is to create ranges that are inclusive or exclusive of the end value, which can be useful when, for example, working with zero-based indexes. |
| 14 | + |
| 15 | +~~~~exercism/note |
| 16 | +When creating a range in Swift using the range operators `...` or `..<`, and wanting to call a method on the range, you need to wrap the range in parentheses. |
| 17 | +This is because the otherwise will the method be called on the 2nd argument of the range operator. |
| 18 | +
|
| 19 | +```swift |
| 20 | +(1...5).contains(3) // Returns true |
| 21 | +1...5.contains(3) // => Error: value of type 'Int' has no member 'contains' |
| 22 | +``` |
| 23 | +~~~~ |
| 24 | + |
| 25 | +## Convert a range to an array |
| 26 | + |
| 27 | +To convert a range to an array, you can use the `Array` initializer. |
| 28 | +This can be useful when you want to create a collection of values, without having to write them out. |
| 29 | + |
| 30 | +```swift |
| 31 | +let range = 1...5 |
| 32 | +let array = Array(range) // Returns [1, 2, 3, 4, 5] |
| 33 | +``` |
| 34 | + |
| 35 | +## Slice an array |
| 36 | + |
| 37 | +Ranges can be used to slice an array. |
| 38 | + |
| 39 | +```swift |
| 40 | +let array = [1, 2, 3, 4, 5] |
| 41 | +let slice = array[1...3] // Returns [2, 3, 4] |
| 42 | +``` |
| 43 | + |
| 44 | +## Range methods |
| 45 | + |
| 46 | +Ranges do have a set of methods that can be used to work with them. |
| 47 | +For example, these methods can be used to get the sum of all the values in the range or check if the range includes a value. |
| 48 | + |
| 49 | +| Method | Description | Example | |
| 50 | +| ----------------------- | ----------------------------------------------------------------------- | ------------------------------------- | |
| 51 | +| `count` | Returns the size of the range | `(1...5).count // returns 5` | |
| 52 | +| [`contains`][contains] | Returns `true` if the range includes the given value, otherwise `false` | `(1...5).contains(3) // Returns true` | |
| 53 | + |
| 54 | +## Endless & Beginningless ranges |
| 55 | + |
| 56 | +A range can be endless and beginless. |
| 57 | + |
| 58 | +Using beginless and endless ranges is useful when you want to, for example, slice an array from the beginning or to the end. |
| 59 | + |
| 60 | +~~~~exercism/caution |
| 61 | +If not used on a collection, the endless range can cause an endless sequence, if not used with caution. |
| 62 | +~~~~ |
| 63 | + |
| 64 | +## String ranges |
| 65 | + |
| 66 | +String can be used in ranges and allow you to get an interval of Strings between two Strings. |
| 67 | +For example, this can be handy when you want to get the alphabet. |
| 68 | + |
| 69 | +```swift |
| 70 | +"a"..."z" // A range containing ["a", "b", "c", ..., "z"] |
| 71 | +``` |
| 72 | + |
| 73 | +[range]: https://developer.apple.com/documentation/swift/range |
| 74 | +[contains]: https://developer.apple.com/documentation/swift/range/contains(_:) |
0 commit comments