Posts

Showing posts with the label flat map

Ray Wenderlich Functions and Types Course - Part 4

Wanted to code at the weekend but had literally no time to! So a quick 20 minutes now, if that! Start Time - 17:38 Two more handy methods - compact map and flat map! Compact Map and Flat Map Compact - helps us deal with optionals. Iterates through each element then add resulting values if they are non nil. Flat - multi dimensional array - combine arrays into 1! This is how you would use a for loop to go through an array and convert what you can to an int - var arrayForValidInput : [ Int ] = [] for input in userInput {     guard let input = Int (input) else {         continue     }     arrayForValidInput . append (input) } But of course compact map is much better! let validInput = userInput . compactMap { (input) in     Int (input) } Flat Map  let arrayOfDwarfArrays = [   [ "Sleepy" , "Grumpy" , "Doc" , "Bashful" , "Sneezy" ],   [ "Thorin" ,...

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