This is the first ever piece that I've written first as a playground and second as a blogpost. This is something that I'd like to make the norm wherever possible and will endeavour to do so. It covers a selection of higher order functions in Swift and draws attention to their similarities and differences. Enjoy! Similarly different: join(), reduce() and flatMap() in Swift 2 reduce(), flatMap() and join() can produce the same results let nestedArray = [[1,2,3,4],[6,7,8,9]] let joined = [].join(nestedArray) let flattened = nestedArray.flatMap{$0} let reduced = nestedArray.reduce([], combine: {$0 + $1}) joined // [1, 2, 3, 4, 6, 7, 8, 9] flattened // [1, 2, 3, 4, 6, 7, 8, 9] reduced // [1, 2, 3, 4, 6, 7, 8, 9] The differences only real become clear when we want to add elements to the array we're flattening let joinedPlus = [5].join(nestedArray) let flattenedPlus = nestedArray.flatMap{$0 + [5]} let reducedPlus = nestedArray.reduce([], combine: {$0 + [5] + $1}) joine
Technology tips for writers, editors, designers, illustrators, programmers and publishers, and now Retro Gamers.