Skip to content

Instantly share code, notes, and snippets.

@lucianoschillagi
Last active February 15, 2024 15:53
Show Gist options
  • Save lucianoschillagi/3f77f829a5b6d192c33c4516692617ae to your computer and use it in GitHub Desktop.
Save lucianoschillagi/3f77f829a5b6d192c33c4516692617ae to your computer and use it in GitHub Desktop.
Tuples with Pattern Matching in Swift
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