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: [[Int]] = [[1, 3, 6, 2], [2, 5, 7], [1, 3]] | |
let sum: Int = numbers | |
.flatMap({ $0 }) | |
.reduce(0, {$0 + $1}) | |
// sum = 30 |
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 phrase = "The rain in Spain stays mainly in the plain." | |
let vowels: Set<Character> = ["a", "e", "i", "o", "u"] | |
phrase.removeAll(where: { vowels.contains($0) }) | |
// phrase == "Th rn n Spn stys mnly n th pln." |
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 numbers: [Int] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] | |
numbers.removeAll(where: { $0 % 2 == 0 }) | |
// numbers = [2, 4, 6, 8, 10] |
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
mutating func removeAll(where shouldBeRemoved: (Element) throws -> Bool) rethrows |
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 words: [String] = ["room", "home", "train", "green", "heroe"] | |
words.forEach({ word in | |
guard word.count > 4 else { | |
print(word.uppercased()) | |
return | |
} | |
print(word) | |
}) |
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 words: [String] = ["room", "home", "train", "green", "heroe"] | |
let sortedWords = words.sorted(by: > ) | |
//sortedWords = ["train", "room", "home", "heroe", "green"] |
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 sorted(by areInIncreasingOrder: (Element, Element) throws -> Bool) rethrows -> [Element] |
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 words: [String] = ["room", "home", "train", "green", "heroe"] | |
let sortedWords = words.sorted() | |
// sortedWords = ["green", "heroe", "home", "room", "train"] |
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 sorted() -> [Element] |
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 words: [String] = ["room", "home", "train", "green", "heroe"] | |
var swapWord: Bool = false | |
repeat { | |
swapWord = false | |
for n in 0...words.count - 2 { | |
if words[n] > words[n + 1] { | |
words.swapAt(n, n + 1) |
NewerOlder