Created
December 17, 2017 12:27
-
-
Save NeoSPU/e7e0fcc04377f9e6f5969fead055bde3 to your computer and use it in GitHub Desktop.
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 UIKit | |
//================ # 1 ================== | |
let stringArray = ["Mr. Smith", "antony", "2", "cat Mr. Jons", "17", "other", "Mr.", "27", "home"] | |
func isInsertText(_ text: String) -> Bool { | |
return text.contains("Mr.") | |
} | |
func isLess(_ text: String) -> Bool { | |
return text.count <= 5 | |
} | |
print("\n================ # 1 ==================\n") | |
print("\(stringArray.filter(isInsertText(_:)))") | |
print("\(stringArray.filter { $0.contains("Mr.")})") | |
print("\(stringArray.filter(isLess(_:)))") | |
print("\(stringArray.filter { $0.count <= 5 })") | |
//================ # 2 ================== | |
let numberArray = [2, 3, 4, 29,3, 17] | |
let numberMult = numberArray.reduce(1, { $0 * $1 }) | |
let newString = stringArray.reduce("/ ", { $0 + $1 + " / " }) | |
print("\n================ # 2 ==================\n") | |
print("\(numberArray) --> \(numberMult)") | |
print(newString) | |
//================ # 3 ================== | |
let newStringAppend = numberArray.map { String($0) + "$" } | |
print("\n================ # 3 ==================\n") | |
print(newStringAppend) | |
//================ # 4 ================== | |
let optionalStringArray = [nil, "Alex", "", nil, "Bob"] | |
let newIntArray = stringArray.flatMap { Int($0) } | |
let newStringArray = optionalStringArray.flatMap { $0 == "" ? nil : $0 } | |
print("\n================ # 4 ==================\n") | |
print(newIntArray) | |
print(newStringArray) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment