Ray Wenderlich Functions and Types Course - Part 5
No need for any pre text, let's go! The aim is to complete this chapter on closures today.
Start Time - 17:05
Filter, Reduce and Sort
So filter can be used - having certain criteria rather than the for loop!
Xcode updating so following along without typing at the moment.

Another example with grades. This is what we had before -

Start Time - 17:05
Filter, Reduce and Sort
So filter can be used - having certain criteria rather than the for loop!
Xcode updating so following along without typing at the moment.

Another example with grades. This is what we had before -

// --------------------------------------
var stock = [1.50: 5, 10.00: 2, 4.99: 20, 2.30: 5, 8.19: 30]
// --------------------------------------
let stockSums = stock.reduce(into: []) { (result, item) in
result.append(item.key * Double(item.value))
}
So this is another use of reduce - complex stuff but good practice!
You basically don't need to create your own sorting algorithms - this is what the closure does.
Sort
Sorting an array of strings -
names.sort()
names.sort { (a, b) -> Bool in
a > b
}
names.sort(by: >)
Sort and sorted are very useful!
Challenge - tough! Had to look up online (not on Catie's answer) for the first bit. Next few seemed ok - let's see how I did...
OK, learned something - the point of putting in parameter names rather than using the $ is that you can then be more specific with what each bit is. I used the $0 and $1, which was perfectly logical to me.
OK I had this -
let longerThanFour = names.filter { (name) -> Bool in
name.count > 4
}
longerThanFour.reduce("") { $0 + $1}
But actually could have used the dot syntax next to the last line of the curly bracket...
let longerThanFour = names.filter { (name) -> Bool in
name.count > 4
}.reduce("") { $0 + $1}
Next bit - I've done it WITH parameters this time! But the second line was definitely more succinct.
let charactersUnder18 = namesAndAges.filter { (name, age) -> Bool in
age < 18
}
let under18 = namesAndAges.filter { $0.value < 18 }
This again is combining two different closures! And the syntax for the last bit I did not get but all good.
let charactersOver18 = namesAndAges.filter { (name, age) -> Bool in
age >= 18
}.map { nameAndAge in
nameAndAge.key
}
Conclusion - recap!
ForEach - similar to for loop but you don't use break/continue etc. but you can chain in with other methods like filter. You can pass functions into it too.
Map - works like forEach but returns an array - great for transforming from one type to another or changing each element into an array. Or from dictionary to array.
CompactMap - gets rid of values than don't have any values; collections of optionals or just the successful results when there may be nil basically
FlatMap - handles multi-dimensional arrays - gets more than one into one; deals with many arrays
Filter - could alternative to where clauses and if conditions; criteria is met to then remove items from an array
Reduce/reduce into - when starting with an empty array or dict...
Sort/sort by/sorted by - all shortcuts for organising your information in an array
All of these methods are associated with functional programming. This makes use of higher order functions. Good stuff!
Finish Time - 18:01 (56 minutes)
Good session! Lots of useful practice with these closures and ways of organising information. Sure, more practise is needed but definitely getting the hang of closures and how much more useful and concise these are than for loops. Three more chapters to go!
Another cool thing is that new stuff is popping up all the time on RW.com. What I WILL and MUST do is to do a consolidation entry in between courses, just to make sense of the info. Looking forward to getting way more done this week!
Comments
Post a Comment