Posts

Showing posts with the label reduce

Ray Wenderlich Functions and Types Course - Part 6

Two days in a row baby! We're onto enumerations first. I want to get a couple more entries this week to finish this course, consolidate then explore more on rw.com. Start Time - 18:50 Intro Enums take advantage of type safety - options to choose from. They are value types. Enums Mostly recap here. Case syntax, raw values. Own raw values can be put in. enum Month : Int {          case january = 1 , february , march , april , may , june , july , august , september , october , november , december } let month = Month . january func monthsUntilChristmas (from month: Month ) -> Int {          Month . december . rawValue - month. rawValue } enum Season : String , CaseIterable {          /// ❄️     case winter          /// 🌾     case spring          /// 🌞     c...

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...