Last active
February 15, 2024 15:53
-
-
Save lucianoschillagi/3f77f829a5b6d192c33c4516692617ae to your computer and use it in GitHub Desktop.
Tuples with Pattern Matching in Swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Cocoa | |
| func processCoordinates(coordinates: (Double, Double)) { | |
| switch coordinates { | |
| case (0, 0): | |
| print("You're at the origin!") | |
| case (let x, 0): | |
| print("You're on the x-axis at \(x)") | |
| case (0, let y): | |
| print("You're on the y-axis at \(y)") | |
| case (let x, let y): | |
| print("You're at coordinates (\(x), \(y))") | |
| } | |
| } | |
| // Call the function with different tuples | |
| processCoordinates(coordinates: (0, 0)) // Output: You're at the origin! | |
| processCoordinates(coordinates: (3, 0)) // Output: You're on the x-axis at 3 | |
| processCoordinates(coordinates: (0, 5)) // Output: You're on the y-axis at 5 | |
| processCoordinates(coordinates: (2.5, 3.5)) // Output: You're at coordinates (2.5, 3.5) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment