Expression Pattern Operator in Swift

Expression Pattern Operator in Swift

Hello people, Leo here. I’m cheerful to say that today we’ll study the Expression Pattern Operator in Swift.

We’ll explore one kind of hidden operator called pattern matching operator or expression pattern operator. This is behind the scene and used as a *switch* statement operator by Swift. So let’s see how can we use it directly!

The image is because Van Gogh was an expressionist artist. Got it? 😉 No more talking! Let’s code!

 

The Problem – Expression Pattern Operator in Swift

You have some credit card number that must be inside a range.

So we can start with range operators. There are some ways you can write that.

Let’s start with the basics:

let creditCardNumber = "123456789012"

let rangeTest = 0...12

rangeTest.contains(creditCardNumber.count) // true

But let’s explore a little more about Swift operators before we enter to improve this code above.

 

One Sided Ranges and other Ranges Flavors

In Swift, we can also use the range operator to do ranges that continue as far as possible in one direction. So we can rewrite the code above like this:

let creditCardNumber = "123456789012" // 12 digits
let creditCardMinSize = 13

let range = ...creditCardMinSize // 1

range.contains(creditCardNumber.count) // true

range.contains(-23) // true as well

On the (1) mark we can see the one-sided range syntax. It means that “I want to create a range from a most negative number to the creditCardMinSize this case is 13”. So when we ask if it contains the -23 integer it will return true.

You can do this in the other direction too. From the number we are starting to the max positive number, so we can write things like this:

let creditCardNumber = "123456789012" // 12 digits
let creditCardMinSize = 13

let range = creditCardMinSize...

range.contains(creditCardNumber.count) // false
range.contains(2000) // true

Actually, we don’t need the *range variable*, writing finally this:

let creditCardNumber = "123456789012" // 12 digits
let creditCardMinSize = 12

(creditCardMinSize...).contains(creditCardNumber.count) // true
(creditCardMinSize...).contains(2000) // true

But this isn’t very accurate because if the credit card number is like “13241341341341344” it will be true too. So would be better if we close the range. Let’s explore the pattern matching operator in the next section to do that.

 

Expression Pattern Operator in Swift

The pattern matching operator is used behind the scenes by the *switch* statement. To solve our credit card problem we can use exactly that to do what we want. The ~= operator is used to accomplish that, we can compare ranges without having the call any methods like *contains* or do any switch statement. The code below is an example of how to use it:

let creditCardNumber = "123456789012" // 12 digits
let creditCardMinSize = 12
let creditCardMaxSize = 14

if creditCardMinSize...creditCardMaxSize ~= creditCardNumber.count { // 1
    print("Yep, is a credit card")
} else {
    print("Nope, not a credit card here")
}

The (1) shows how: to create a range with the closed range operator, and use the pattern matching operator *~=* to compare if the credit card number is within the range. Now we don’t need to make our logic complicated or use any methods or more variables to make range comparisons.

One last thing the code above is analogous to the code with the *switch statement*, example:

let creditCardNumber = "123456789012" // 12 digits
let creditCardMinSize = 12
let creditCardMaxSize = 14

switch creditCardNumber.count {
case creditCardMinSize...creditCardMaxSize:
    print("Yep, is a credit card")
default:
    print("Nope, not a credit card")
}

And we are done!

 

Summary

That’s it. Today we explore open and closed ranged operators, and the pattern matching operator solving the problem of checking if it’s a valid credit card. Hope you have enjoyed the content as I enjoyed writing it.

That’s all my people, I hope you liked reading this article as much as I enjoyed writing it. If you want to support this blog you can Buy Me a Coffee or leave a comment saying hello. You can also sponsor posts and I’m open to freelance writing! You can reach me on LinkedIn or Twitter and send me an e-mail through the contact page.

Thanks for the reading and… That’s all folks.

Credits – image

Share this post:

Related posts

Sponsor