Saturday, 14 June 2014

Pipe and Chains!

In the post written so far I've touched upon implementing the (|>) pipe forward operator and various of the traditional functional (in so far as Swift allows) mapping and folding options but haven't really touched upon why these can be useful in the software development process.

The code sample below shows how, when combined, they provide a powerful mechanism which can greatly reduce the amount of code a developer needs to write. As a side effect you should also note a distinct lack of more traditional control structures (condition,loops etc) and variable definitions.

The code is type checked by the compiler and handles, by using Swift option chaining, the optional elements within the model hierarchy.

Another helper function has been introduced here (getOrElse) which lets us provide default values for optional variables - in the cases here simply ensuring an empty array is returned as part of the search if any part of the model chaining is optional.

1 comment:

  1. I've written a blog post about pipe-forward in Swift. See if interested.