Last active
May 24, 2016 07:24
-
-
Save satoshin2071/d75af352933aafc851088896134c7a51 to your computer and use it in GitHub Desktop.
Swift Pattern Matching, Part 3: Custom pattern matching & syntactic sugar. Shakyo practice.
This file contains 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
/* | |
From http://alisoftware.github.io/swift/pattern-matching/2016/04/24/pattern-matching-3/ | |
Shakyo practice | |
Pattern Matching, Part 3: Custom pattern matching & syntactic sugar | |
*/ | |
import XCPlayground | |
import Foundation | |
import UIKit | |
// MARK: - Simply Range Pattern Match | |
0...100 ~= 15 | |
// MARK: - Use Operator Overloading 1 | |
struct Affine { | |
var a: Int | |
var b: Int | |
} | |
func ~= (leftSide: Affine, rightSide: Int) -> Bool { | |
return rightSide % leftSide.a == leftSide.b | |
} | |
switch 5 { | |
case Affine(a: 2, b: 0): print("Even") | |
case Affine(a: 3, b: 1): print("3x+1") | |
case Affine(a: 3, b: 2): print("3x+2") | |
default: print("default") | |
} | |
// MARK: - Use Operator Overloading 2 | |
struct Book { | |
let title: String | |
let author: String | |
let publishYear: Int | |
} | |
func ~= (leftSide: Range<Int>, rightSide: Book) -> Bool { | |
return leftSide ~= rightSide.publishYear | |
} | |
let book = Book(title: "Roadside Picnic", author: "Arkady and Boris Strugatsky", publishYear: 1972) | |
switch book { | |
case 1900..<1950: print("1900...<1950") | |
case 1950..<2000: print("1950..<2000") | |
default: print("default") | |
} | |
// MARK: - Use Operator Overloading 3 | |
struct Answer { | |
let text: String | |
let compareOptions: NSStringCompareOptions = [ | |
.CaseInsensitiveSearch, // 大文字小文字区別しない | |
.DiacriticInsensitiveSearch, // 記号を無視 | |
.WidthInsensitiveSearch // 半角,全角の幅の違いを無視 | |
] | |
} | |
func ~= (leftSide: Answer, rightSide: String) -> Bool { | |
return leftSide.text.compare(rightSide, options: leftSide.compareOptions, range: nil, locale: nil) == NSComparisonResult.OrderedSame | |
} | |
let userAnswer = "Tete a Tete" | |
switch userAnswer { | |
case Answer(text: "tête-à-tête"): print("Good answer!") | |
case Answer(text: "tête à tête"): print("Almost… don't forget dashes!") | |
default: print("Sorry, wrong answer!") | |
} | |
// MARK: - indexPath and case ….rawValue | |
let indexPath : NSIndexPath = NSIndexPath(forRow: 2, inSection: 0) | |
indexPath.row | |
enum MenuItem: Int { | |
case Home | |
case Account | |
case Setting | |
} | |
switch indexPath.row { | |
case MenuItem.Home.rawValue: print("Home") | |
case MenuItem.Account.rawValue: print("Accopunt") | |
case MenuItem.Setting.rawValue: print("Setting") | |
default: print("default") | |
} | |
// ↓ enum from the rawValue first. | |
switch MenuItem(rawValue: indexPath.row) { | |
case .Home?:print("Home") | |
case .Account?:print("Account") | |
case .Setting?:print("Setting") | |
default: print("default") | |
} | |
// ↓ one more !! | |
guard let menuItem = MenuItem(rawValue: indexPath.row) else { | |
fatalError("indexPath invalid") | |
} | |
switch menuItem { | |
case .Home:print("Home") | |
case .Account:print("Account") | |
case .Setting:print("Setting") | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment