Map Map Map better in Swift

Map function is quite a convenience method that often used in functional(mainly) programming.
In Swift, all collection types(Array,Dictionary,Set...) support map. It take a closure that take element type from T and output type U then transform from [T] to [U].

In most case, you will see :
in short hand:
How convenience is that to old NSArray.
In real world case, things are more complicating.
Let's say you have a type A, which can init from another type B.
Now you have an array of [B], you would like to transform it to another array [A]. How would you do?
Typically, you may use the following code:
Solid?
Actually you make it better :
Sweet.

If you want to make it look more functional, you can declare a operator for example <<< in this case.
Then you can:
Clean and clear.
There are still many other small but interesting skills in Swift that can help us code in a better way.
We will only observe them if we can free our mind from Objective-C age.

 

Original post from my previous site: http://povoq.blogspot.com/2017/04/blog-post.html

and the original post is originally from my linked.in account...

Subscribe to TechRD.in

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
jamie@example.com
Subscribe