Posts

Showing posts with the label filter

Ray Wenderlich Functions and Types Course - Part 5

Image
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 - // -------------------------------------- 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 } name...

Treehouse Intermediate Course - Part 12 (Closures 4)

Image
Right so I've connected to my personal hotspot and am going to code on the coach! I'll aim to do around an hour. Continuing with map, let's go! Start Time - 10:33 Flat Map Oh an example about blogs. How apt! So far, all clear - struct Post {          var post: String     var tags: [ String ] } let blog = Post (post: "Hi all!" , tags: [ "first" , "newbie" ]) let secondBlog = Post (post: "Now a bit about me!" , tags: [ "personal" , "info" , "details" ]) We are going to do something to combine these blogs I reckon! OK forget the above - I got confused! let blog = [      Post (post: "Hi all!" , tags: [ "first" , "newbie" ]), Post (post: "Now a bit about me!" , tags: [ "personal" , "info" , "details" ]) ] let tags = blog . flatMap { $0. tags } And extension time on the array agai...