This workshop will be retired on May 31, 2020.
One Sided Ranges6:04 with Pasan Premaratne
Swift 4 introduces syntax to easily create a "one sided range". In this video, let's go over the changes.
Swift 4 introduces the concept of a one-sided range that is created 0:00 using prefix or postfix operators and the existing range syntax. 0:04 Let's look at how we would have done things in Swift 3. 0:10 Suppose we had a string. 0:13 We'll assign this to a constant called workshop. 0:15 And we'll say this is what's New in Swift 4 One Sided Ranges. 0:19 Let's say we had this string, and 0:28 we wanted to split this up into a workshop title and a video title. 0:30 To do this, 0:36 we'll need a bit of boiler plate code to get to the index values that we want. 0:36 So we'd have to do something like if let 0:40 indexOfPunctuation = workshop.indexof and 0:45 we'll look for the colon character. 0:50 Once we have this we'll say let indexAfter. 0:56 So the index right after where the colon occurs = 1:00 workshop.index after indexOfPunctuation. 1:05 So once we have this index, 1:11 we can use the standard range operators to get a slice of the string. 1:12 Let series = workshop, workshop is our string. 1:16 And then we'll start with the start index which is the beginning of the string. 1:22 And then we'll go all the way using the half open range operator, 1:29 all the way to the indexOfPunctuation. 1:35 So to get the series, we start at the beginning of the string which again 1:38 we have accessed using the start index property. 1:42 And then we've used the half open range operator along with the index where 1:44 the colon is located to return a slice of the string. 1:48 Now for the title, we'll start with the indexAfter. 1:52 And, again, 1:56 use the half-open range operator to return a slice right up to the end. 1:57 So we'll say let title = workshop. 2:00 We'll start with indexAfter, and 2:05 we'll go all the way up to workshop.endIndex. 2:08 And if we look in the results area, you can see that 2:14 we have those slices of the string that we want. 2:19 So this is a workshop title, this is the video title. 2:22 In Swift 4, you can use a one-sided range for these kinds of operation. 2:25 So let's refractor this. 2:30 For the series string here, we can remove the reference to the start index, 2:32 leaving the lower bound unspecified. 2:37 Let's get rid of that. 2:40 This is now inferred and we have a one-sided range. 2:42 The resulting value is exactly the same. 2:46 Now, similarly, we can also remove the reference to the end index, so 2:49 let's get rid of this. 2:54 But here in doing so, 2:55 we'll get an error because we're using the half open range operator. 2:56 When defining one-sided ranges, the half open range operator is a valid 3:01 prefix operator as seen in our usage to get the series substring. 3:05 But it is not a valid postfix unary operator. 3:10 For them, you can only use the close range operator. 3:14 Let's change this, and now we have the substring that we want. 3:17 What's cool about this is we can also use a one-sided range to create 3:24 an infinite sequence. 3:29 We can say let evenValues, and here we'll do 1... 3:30 And this creates an infinite sequence that starts at 1. 3:37 And then let's say we only want the first 100, so we'll say .prefix max length 100. 3:41 And now we've taken the infinite sequence and 3:48 we've just grabbed the first 100 elements of the sequence. 3:51 And now we can do whatever we want. 3:55 So we'll say, filter the odd ones out. 3:56 Or rather, we'll get the even ones. 4:01 Now, there's one more neat thing that we can do with one-sided ranges, and 4:06 that's pattern matching. 4:10 This isn't fundamentally different from what we could do earlier with range 4:12 operators, it just makes matching on certain ranges easier. 4:16 So let's define a constant here, let value = -2. 4:20 Let's assume we have this simple value, and 4:24 we want to match it against a range of values. 4:26 In this case, we want to check if a value is positive or negative. 4:28 So we can switch on this value. 4:33 And using the one-sided range, 4:36 we can define an infinite range that covers our use cases. 4:38 So here you'll say anything that goes from 1 all the way to the end of our number 4:41 range, print value is a positive number. 4:46 Now here the end of the range is assumed and 4:56 we've obtained a range of all positive numbers. 4:58 Similarly, we can define one for 5:02 all negative numbers using the unary prefix version. 5:04 So here we'll say case..half open range all the way to 0. 5:07 Then we'll print, ("\(value) 5:12 is a negative number"). 5:19 To finish this off, let's include a 0 case. 5:24 Print ("Zero). 5:26 And you'd think that this is all we need, 5:31 but we need to add a default case. 5:36 Because here we're using a range of values, and the switch statement can't 5:40 infer that we've covered all our cases, so we'll say default fatalError. 5:44 There we go, -2 is a negative number. 5:51 Now one-sided ranges aren't a profound change, but 5:54 it does clean things up nicely. 5:56 To read up more on the proposal, which is SE-0176, check the notes section. 5:59
You need to sign up for Treehouse in order to download course files.Sign up