Michael Long
Oct 18, 2022

--

Like the emphasis on using higher order functions, but what's wrong with simply doing a functional chain of users.filter{}.map{} ?

Developers already know those functions, and using them will probably be more performant than your bespoke solution in terms of memory allocation and processing time.

In your code, map creates and walks an array of size N, and compactMap also creates and walks an array of size N. Whereas in my example filter creates and walks an array of size N, while the second map creates and walks an array of size f(n) (i.e. only the number of filtered elements).

https://gist.github.com/hmlongco/022ca86724d4d5319e781ab31c9aa583

--

--

Michael Long
Michael Long

Written by Michael Long

I write about Apple, Swift, and SwiftUI in particular, and technology in general. I'm also a Lead iOS Engineer at InRhythm, a modern digital consulting firm.

Responses (1)