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
let squaress = values.map {$0*$0} |
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
let scores :[NSNumber] = [0,28,124] | |
let words = scores.map {NumberFormatter.localizedString(from: $0, number:.spellOut) } | |
print(words) | |
//Console: ["zero", "twenty-eight", "one hundred twenty-four"] | |
// | |
let milesToPoint = ["Point1":120.0,"Point2":45.0,"Point3":55.0] | |
let kmToPoint = milesToPoint.map{name,miles in miles*1.67} | |
//console: [200.39999999999998, 75.149999999999991, 91.849999999999994] |
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
let digits = [1,4,10,13] | |
let event = digits.filter { number -> Bool in | |
number % 2 == 0 | |
} | |
//short way | |
let otherEvent = digits.filter {$0%2==0} |
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
let items = [2.0, 4.0, 6.0, 8.0] | |
let total = items.reduce(10.0,+) | |
//Console: 30.0 |
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
/// Returns the result of combining the elements of the sequence using the | |
/// given closure. | |
/// | |
/// Use the `reduce(_:_:)` method to produce a single value from the elements | |
/// of an entire sequence. For example, you can use this method on an array | |
/// of numbers to find their sum or product. | |
/// | |
/// The `nextPartialResult` closure is called sequentially with an | |
/// accumulating value initialized to `initialResult` and each element of | |
/// the sequence. This example shows how to find the sum of an array of |
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
let addItem = ["abc","f","a"] | |
let csv = addItem.reduce("zyz") { (text, name) -> String in | |
"\(text),\(name)" | |
} | |
//Console: zyz,abc,f,a |
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
let collections = [[5,3,6],[5,6,8]] | |
let flat = collections.flatMap {$0} | |
print(flat) | |
//Console: [5, 3, 6, 5, 6, 8] | |
//Remove optional | |
let peple : [String?] = ["Tom", nil,"Teddy", nil] | |
let valid = peple.flatMap{$0} | |
//Console: ["Tom", "Teddy"] |
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
//1. flapMap vs filter | |
let otherCollection = [[5,6,4],[5,8],[9,8,7]] | |
let onlyEven = otherCollection.flatMap { intArray in | |
intArray.filter({$0%2 == 0}) | |
} | |
print(onlyEven) | |
//Console: [6, 4, 8, 8] | |
//or short-way | |
let onlyEventShort = otherCollection.flatMap{$0.filter{$0%2 == 0}} |
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
protocol Area { | |
var area: Double {get} | |
} |
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
struct Square: Area { | |
let side: Double | |
var area: Double { | |
return side*side | |
} | |
} | |
struct Triangle: Area { | |
let base: Double | |
let height: Double |