Created
April 13, 2023 07:42
-
-
Save hira22/8f881178eeba7be12c1bd6ae8c2acf6a to your computer and use it in GitHub Desktop.
map と reduce の説明
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
import Foundation | |
// MARK: 1. 1〜10の配列を用意する | |
print( | |
(1...10).map{ $0 } | |
) | |
// MARK: 2. 1〜10の合計を求める forEach | |
var result: Int = 0 | |
(1...10).map{ $0 }.forEach { (value: Int) in | |
result = result + value | |
} | |
print( | |
result | |
) | |
// MARK: 3. 1〜10の合計を求める reduce ver.1 | |
print( | |
(1...10).map{ $0 } | |
.reduce(.zero) { (partialResult: Int, value: Int) in | |
return partialResult + value | |
} | |
) | |
// MARK: 4. 1〜10の合計を求める reduce ver.2 | |
print( | |
(1...10).map{ $0 } | |
.reduce(into: .zero) { (partialResult: inout Int, value: Int) in | |
partialResult += value | |
// partialResult = partialResult + value // 1行上と同じ | |
} | |
) | |
// MARK: 5. 1〜10の合計を求める reduce ver.3 | |
print( | |
(1...10).map{ $0 } | |
.reduce(.zero, +) | |
) | |
// MARK: 6. 1〜10の文字列の配列を用意する (各要素の Int -> String 型変換) | |
// String に直したい時には "\()" でもいいけど、.description を推奨 | |
// "\(cost)万円" みたいな埋め込みをしたい時は "\()" が便利 | |
var result2: [String] = [] | |
(1...10).map{ $0 }.forEach { (value: Int) in | |
result2 = result2 + [value.description] | |
} | |
print( | |
result2 | |
) | |
// MARK: 7. 1〜10の文字列の配列を用意する (各要素の Int -> String 型変換) reduce版 ver.1 | |
// reduce(_ initialResult, ...) は毎回新しい結果(partialResult: String の配列)を return する必要がある | |
print( | |
(1...10).map{ $0 } | |
.reduce([String]()) { (partialResult: [String], value: Int) in | |
return partialResult + [value.description] | |
} | |
) | |
// MARK: 8. 1〜10の文字列の配列を用意する (各要素の Int -> String 型変換) reduce版 ver.2 | |
// reduce(into: , ...) は前回の結果(partialResult: String の配列)の参照を受け取って変更する | |
print( | |
(1...10).map{ $0 } | |
.reduce(into: [String]()) { (partialResult: inout [String], value: Int) in | |
partialResult += [value.description] | |
} | |
) | |
// MARK: 9. 1〜10の文字列の配列を用意する (各要素の Int -> String 型変換) っていうか map で行けるよね版 | |
print( | |
(1...10).map{ $0.description } | |
) | |
// MARK: 10. 1〜10の文字列の配列を用意する (各要素の Int -> String 型変換) いつ reduce 使うの?版 | |
struct User { | |
let id: Int | |
let name: String | |
} | |
let names = ["たなか","なかがわ","ひらおか"] | |
print( | |
(1...10).map{ $0 }.reduce([User]()) { (partialResult: [User], value: Int) in | |
let index = value % 3 | |
let user = User(id: value, name: names[index]) | |
return partialResult + [user] | |
} | |
) | |
print( | |
(1...10).map{ $0 }.reduce(into: [User]()) { (partialResult: inout [User], value: Int) in | |
let index = value % 3 | |
let user = User(id: value, name: names[index]) | |
partialResult += [user] | |
} | |
) | |
// ある配列から全く別の型の配列を作りたい時がよく使うと思います。 | |
let param: [String: String?] = ["a": "aaa", "b": nil, "c": "ccc"] | |
print(param.filter({ element in | |
element.value != nil | |
})) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment