Skip to content

Instantly share code, notes, and snippets.

View raulferrerdev's full-sized avatar

Raúl Ferrer raulferrerdev

View GitHub Profile
let numbers: [[Int]] = [[1, 3, 6, 2], [2, 5, 7], [1, 3]]
let sum: Int = numbers
.flatMap({ $0 })
.reduce(0, {$0 + $1})
// sum = 30
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."
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]
mutating func removeAll(where shouldBeRemoved: (Element) throws -> Bool) rethrows
let words: [String] = ["room", "home", "train", "green", "heroe"]
words.forEach({ word in
guard word.count > 4 else {
print(word.uppercased())
return
}
print(word)
})
let words: [String] = ["room", "home", "train", "green", "heroe"]
let sortedWords = words.sorted(by: > )
//sortedWords = ["train", "room", "home", "heroe", "green"]
func sorted(by areInIncreasingOrder: (Element, Element) throws -> Bool) rethrows -> [Element]
let words: [String] = ["room", "home", "train", "green", "heroe"]
let sortedWords = words.sorted()
// sortedWords = ["green", "heroe", "home", "room", "train"]
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)