Last active
April 30, 2016 17:02
-
-
Save shoheiyokoyama/860dd04410a4220f3dd73b8e251abf3a to your computer and use it in GitHub Desktop.
Swiftらしいコーディングを学ぶ 「コレクションに用いる高階関数とClosure」 ref: http://qiita.com/shoheiyokoyama/items/ad884aeea2dc69baf422
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
| { | |
| (paramerters) -> return type in | |
| statements | |
| } | |
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
| @warn_unused_result | |
| public func map<T>(@noescape transform: (Self.Generator.Element) throws -> T) rethrows -> [T] |
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
| [x_1, x_2, .., x_n].map(f) = [f(x_1), f(x_2), .., f(n)] |
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
| let filteredDictionary = ["NewVersion": 3.0, "CurrentVersion": 2.2, "OldVersion": 1.0].filter { key, version in | |
| version >= 2.2 | |
| } | |
| //Keyの昇順になっている | |
| //["CurrentVersion": 2.2, "NewVersion": 3.0] | |
| //引数省略 | |
| let filteredDictionary = ["NewVersion": 3.0, "CurrentVersion": 2.2, "OldVersion": 1.0].filter { $0.1 >= 2.2 } | |
| //["CurrentVersion": 2.2, "NewVersion": 3.0] | |
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
| var result = "Math: {}, Science: {}, English: {}" | |
| let score = [65, 78, 96] | |
| let myResult = score.reduce(result) { result, score in | |
| guard let range = result.rangeOfString("{}") else { return result } | |
| return result.stringByReplacingCharactersInRange(range, withString: String(score)) | |
| } | |
| print(myResult) | |
| //"Math: 65, Science: 78, English: 96" | |
| //引数省略 | |
| let myResult = score.reduce(result) { | |
| guard let range = $0.rangeOfString("{}") else { return $0 } | |
| return $0.stringByReplacingCharactersInRange(range, withString: String($1)) | |
| } | |
| print(myResult) | |
| //"Math: 65, Science: 78, English: 96" |
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
| let sum = [1, 2, 3, 4, 5].reduce(0, combine: +) | |
| //15 | |
| let minusSum = [1, 2, 3, 4, 5].reduce(0, combine: -) | |
| //-15 | |
| let multipliedSum = [1, 2, 3, 4, 5].reduce(1, combine: *) | |
| //120 | |
| [1, 2, 3, 4, 5].reduce(0, combine: max) | |
| //5 | |
| [1, 2, 3, 4, 5].reduce(1, combine: min) | |
| //1 |
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
| let numbers = [1, 2, 3, 4] | |
| let mapped = numbers.map { Array(count: $0, repeatedValue: $0) } | |
| //[[1], [2, 2], [3, 3, 3], [4, 4, 4, 4]] | |
| let flatMapped = numbers.flatMap{ Array(count: $0, repeatedValue: $0) } | |
| //[1, 2, 2, 3, 3, 3, 4, 4, 4, 4] |
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
| let possibleNumbers = ["1", "2", "three", "///4///", "5"] | |
| let mapped: [Int?] = numbers.map { string in Int(string) } | |
| //基本的にmapだとoptionalが返る | |
| //[optional(1), optional(2), nil, nil, optional(5)] | |
| //[1, 2, nil, nil, 5] | |
| let flatMapped: [Int] = numbers.flatMap { string in Int(string) } | |
| //アンラップされた値が返る | |
| // [1, 2, 5] |
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
| let flatMapped = [[1], [2, 3], nil].flatMap { $0 } | |
| //アンラップのみされた配列が返る | |
| //[[1], [2, 3]] | |
| let reFlatMapped = flatMapped.flatMap { $0 } | |
| print(reFlatMapped) | |
| //1次元配列を返す | |
| //[1, 2, 3] |
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
| let names = ["Chris", "Alex", "Ewa", "Barry", "Daniella"] | |
| var reversed = names.sort({ (s1: String, s2: String) -> Bool in | |
| return s1 > s2 | |
| }) | |
| //["Ewa", "Daniella", "Chris", "Barry", "Alex"] |
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
| let names = ["Chris", "Alex", "Ewa", "Barry", "Daniella"] | |
| func backwards(s1: String, s2: String) -> Bool { | |
| return s1 > s2 | |
| } | |
| var reversed = names.sort(backwards) | |
| //["Ewa", "Daniella", "Chris", "Barry", "Alex"] |
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
| let array = [10.0, 20.0, 30.0] | |
| var doubleArray = [Double]() | |
| for value in array { | |
| doubleArray.append(value * 2.0) | |
| } | |
| print(doubleArray) | |
| //[20.0, 40.0, 60.0] |
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
| //一行でも書ける | |
| reversed = names.sort( { (s1: String, s2: String) -> Bool in return s1 > s2 } ) | |
| //型推論をしてくれるので以下のようにも書ける | |
| reversed = names.sort( { s1, s2 in return s1 > s2 } ) | |
| //(※1)returnを省略できる | |
| reversed = names.sort( { s1, s2 in s1 > s2 } ) | |
| //(※2)引数を省略できる | |
| reversed = names.sort( { $0 > $1 } ) | |
| //Closureを外に出すこともできる | |
| reversed = names.sort() { $0 > $1 } | |
| //()も省略できる | |
| reversed = names.sort { $0 > $1 } | |
| //このように省略することもできる。 | |
| reversed = names.sort(>) | |
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
| let array = [10.0, 20.0, 30.0] | |
| var doubleArray = array.map {value in value * 2.0} | |
| print(doubleArray) | |
| //[20.0, 40.0, 60.0] | |
| //このようにも書ける | |
| //$0は最初の引数を意味している | |
| var doubleArray = array.map {$0 * 2.0} | |
| //[20.0, 40.0, 60.0] |
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
| let dictionary = ["SWIFT": 2.1, "XCODE": 7.2].map { key, value in | |
| (key.lowercaseString, value + 0.1) | |
| } | |
| //[("swift", 2.2), ("xcode", 7.3)] | |
| //引数を省略した場合 | |
| let dictionary = ["SWIFT": 2.1, "XCODE": 7.2].map { ($0.0.lowercaseString, $0.1 + 0.1) } | |
| //[("swift", 2.2), ("xcode", 7.3)] |
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
| //Array | |
| [0, 1, 2, 3, 4, 5, 6,].forEach { value in | |
| if value == 0 { return } | |
| if value % 2 == 0 { | |
| print("\(value) is even value") | |
| } | |
| } | |
| //"2 is even value" | |
| //"4 is even value" | |
| //"6 is even value" | |
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
| //Dictionary | |
| ["Swift": 2.2, "Xcode": 7.3].forEach { key, value in | |
| if key == "Swift" { | |
| print("Swift version is \(value)") | |
| } | |
| } | |
| //"Swift version is 2.2" | |
| //引数省略 | |
| ["Swift": 2.2, "Xcode": 7.3].forEach { if $0.0 == "Swift" { print("Swift version is \($0.1)") } } | |
| //"Swift version is 2.2" |
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
| let filteredArray = [0, 5, 4, 3, 2, 1, 6, 7, 9, 8, 10].filter { value in | |
| value % 2 == 0 && value < 8 | |
| } | |
| //数字が昇順になって返される | |
| //[0, 2, 4, 6] |
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
| func function(paramerters) -> return type { | |
| statements | |
| } |
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
| @warn_unused_result | |
| public func reduce<T>(initial: T, @noescape combine: (T, Self.Generator.Element) throws -> T) rethrows -> T |
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
| @warn_unused_result | |
| public func flatMap<T>(@noescape transform: (Self.Generator.Element) throws -> T?) rethrows -> [T] |
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
| @warn_unused_result(mutable_variant="sortInPlace") | |
| public func sort(@noescape isOrderedBefore: (Self.Generator.Element, Self.Generator.Element) -> Bool) -> [Self.Generator.Element] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment