おそ松くんで学ぶSwift4.2での「うそ松」判別法

Swift4.2 でのAPIの変更点

Swift4.2 でのAPI変更点である、「New Sequence Methods」 によって、 うそ松の判別法の書き方が下記のように変わります。 (ネタです)

let osomatsuBrothers = ["おそ松", "カラ松", "チョロ松", "一松", "ウソ松", "十四松", "トド松"]

// Swift 4.1 first / index (where:) / index (of:)

if let usomatsu = osomatsuBrothers.first(where: {$0.hasPrefix("ウソ")}),
    let usomatsuIndex = osomatsuBrothers.index(where:{$0.hasPrefix("ウソ")}),
    let tyoromatsuIndex = osomatsuBrothers.index(of: "チョロ松") {
    print("osomatsuBrothersで \(usomatsuIndex + 1) 番目は \(usomatsu) くん.")
    print("チョロ松のindexは \(tyoromatsuIndex + 1)")
} else {
    print("ウソが付く兄弟がいなかった")
}

Swift4.1まではこうだったのが、Swift4.2からはこうなります。

let osomatsuBrothers = ["おそ松", "カラ松", "チョロ松", "一松", "ウソ松", "十四松", "トド松"]

// Swift 4.2 first / firstIndex (where:) / firstIndex(of:)

if let usomatsu = osomatsuBrothers.first(where: {$0.hasPrefix("ウソ")}),
    let usomatsuIndex = osomatsuBrothers.firstIndex(where: {$0.hasPrefix("ウソ")}),
    let tyoromatsuIndex = osomatsuBrothers.firstIndex(of: "チョロ松") {
    print("osomatsuBrothersで \(usomatsuIndex + 1) 番目は \(usomatsu) くん.")
    print("チョロ松のindexは \(tyoromatsuIndex + 1)")
} else {
    print("ウソが付く兄弟がいなかった")
}

ついでに lastIndexについても同じような操作で対応できます。

let osomatsuBrothers = ["おそ松", "カラ松", "チョロ松", "一松", "ウソ松", "十四松", "トド松"]

// Swift 4.2 last / lastIndex (where:) / lastIndex(of:)

if let usomatsu = osomatsuBrothers.last(where: {$0.hasPrefix("ウソ")}),
    let usomatsuIndex = osomatsuBrothers.lastIndex(where: {$0.hasPrefix("ウソ")}),
    let lastMajorIndex = osomatsuBrothers.lastIndex(of: "チョロ松") {
    print("osomatsuBrothersで \(usomatsuIndex + 1) 番目は \(usomatsu) くん.")
    print("チョロ松のindexは \(lastMajorIndex + 1)")
} else {
    print("ウソが付く兄弟がいなかった")
}

元ネタはこちらの記事です。

What's new in Swift 4.2 - New Sequence Methods

let ages = ["ten", "tweleve", "thirteen", "nineteen", "eighteen", "seventeen", "fourteen", "eighteen", "fifteen", "sixteen", "eleven"]

// Swift 4.1

if let firstTeen = ages.first(where: {$0.hasSuffix("teen")}),
    let firstIndex = ages.index(where:{$0.hasSuffix("teen")}),
    let firstMajorIndex = ages.index(of: "eighteen") {
    print("Teenager number \(firstIndex + 1) is \(firstTeen) years old.")
    print("Teenager number \(firstMajorIndex + 1) isn't a minor anymore")
} else {
    print("No Teenagers around here")
}

// Swift 4.2

if let firstTeen = ages.first(where: {$0.hasSuffix("teen")}),
    let firstIndex = ages.firstIndex(where: {$0.hasSuffix("teen")}),
    let firstMajorIndex = ages.firstIndex(of: "eightenn") {
    print("Teenager number \(firstIndex + 1) is \(firstTeen) years old.")
    print("Teenager number \(firstMajorIndex + 1) isn't a minor anymore")
} else {
    print("No Teenagers around here")
}

if let lastTeen = ages.last(where: {$0.hasSuffix("teen")}),
    let lastIndex = ages.lastIndex(where: {$0.hasSuffix("teen")}),
    let lastMajorIndex = ages.lastIndex(of: "eighteen") {
    print("Teenager number \(lastIndex + 1) is \(lastTeen) years old.")
    print("Teenager number \(lastMajorIndex + 1) isn't a minor anymore")
} else {
    print("No Teenagers around here")
}

また、 allSatisfyが新しく加わりました。 これによってシーケンスの全ての要素が条件を満たすかどうかを取得したい場合の書き方が下記のように書けるようになります。

let values = [10, 8 ,12, 20] // [10, 8, 12, 20]
let allEven = values.allSatisfy{ $0 % 2 == 0}
allEven // true