# Programming in Swift: Functions & Types

Jan 4 2022 Swift 5.5, iOS 15, Xcode 13

## Part 2: Closures

### Lesson Complete

Play Next Lesson
Next

More About Algorithms

Update Notes: This course was originally recorded in 2019. It has been reviewed and all content and materials updated as of October 2021.

Heads up... You've reached locked video content where the transcript will be shown as obfuscated text.

You can unlock the rest of this video course, and our entire catalogue of books and videos, with a kodeco.com Professional subscription.

This is the last episode that will focus on using closures and collections together, and here, we’ll tackle filter, reduce, and the sort methods.

``````dwarves.filter(<#T##isIncluded: (String) throws -> Bool##(String) throws -> Bool#>)
``````
``````  dwarves.filter { (<#String#>) -> Bool in
<#code#>
}
``````
``````  dwarves.filter { (😺dwarf🛑) -> Bool in
``````
``````    dwarf > "M"
``````
``````  dwarves.filter { ❌(dwarf) -> Bool in❌
dwarf > "M"
}
``````
``````dwarves.filter { \$0 > "M" }
``````
``````  var totalGrade = ❌0
for grade in grades {
totalGrade += grade
}❌
``````
``````var totalGrade = 😺grades.reduce(<#T##initialResult: Result##Result#>, <#T##nextPartialResult: (Result, Int) throws -> Result##(Result, Int) throws -> Result#>)
``````
``````grades.reduce(😺0🛑) {

}
``````
``````let totalGrade = grades.reduce(0) { (<#Result#>, <#Int#>) -> Result in
<#code#>
}
``````
``````grades.reduce(0) { 😺total, grade🛑 -> Result
``````
``````grades.reduce(0) { total, grade -> 😺Int🛑 in
``````
``````  total + grade
``````
``````getPassStatus(for: ozmaGrades, lowestPass: 60)
``````
``````  let totalGrade = grades.reduce(0, 😺+) //{ total, grade -> Int in
//    total + grade
//  }🛑
``````
``````let stockSums =
``````
``````let stockSums = stock.reduce(into: <#T##Result#>, <#T##updateAccumulatingResult: (inout Result, (key: Double, value: Int)) throws -> ()##(inout Result, (key: Double, value: Int)) throws -> ()#>)
``````
``````let stockSums = stock.reduce(into: 😺[]) { (<#inout Result#>, <#(key: Double, value: Int)#>) in
<#code#>
}
``````
``````let stockSums = stock.reduce(into: []) { (😺result, item🛑) in
``````
``````  result.append(item.key * Double(item.value))
``````
``````names.sort()
``````
``````names.sort { (a, b) -> Bool in

}
``````
``````  a > b
}
names
``````
``````names.sort(by: >)
``````
``````let longToShortNames = names.sorted {

}
``````
``````let longToShortNames = names.sorted {
\$0.count > \$1.count
}
``````
``````longToShortNames
names
``````
``````  dwarves.filter { \$0 > "M" }
}.sorted()
``````