Pure Functions in Swift

Pure functions make up the core of the functional programming paradigm. They are functions that always return a specific output when given a particularly input.


Since contrast is a powerful aid for obtaining clarity, we will start by taking a look at impure an function then use that as a base to cross over to a pure function.


Below we have a function called fetchdata which accepts an input, a url endpoint. The function uses this input to fetch us data from the web, create objects using the JSON data received, then appends this data to an array of users we have created above. This pattern should be familiar to most developers.


impure_function

The fetchData function above is not pure because it is modifying something outside of its scope. In our case, our function is modifying the users array above it. This brings us to the first rule in functional programming- pure functions cannot modify anything outside of their inner scope. A good way to fix this issue would be to instead of modifying an already existing array, the function can return its own array.


impure_functions

Now you may think this is a pure function because it reliably returns an array of users but mistake not- it still isn't. That is because the array it returns is subject to change in the future. If ten more users join the platform whose endpoint we request data from, the function will now return an array of 13 users rather than the 3 it previously returned.


Let's make a call the fetchData function and print out the data it returns inside the console. Here is the set of data we get printed to the console on our first call.


xcode_console

Ok, so far everything looks good. But let's make a second call to fetchData using the same input, the /users url endpoint, and see what happens.


console.log

Notice that when the function was called two minutes later, different objects were returned. Remember, pure functions cannot return different outputs for the same input. When we give a pure function the same input, it must return the same exact output independent through time. In other words, pure functions must be exactly deterministic . Writing a pure function is rather simple. They take an input and returns an output without modifying anything outside of their scope. Below is a practical example of a pure function.


create_button_swift

This function returns a button based on the input of color you give it. If you pass it a red color, you will always be returned a red button. It will never return you a black button when you give it an input of red. This functions is also not modifying any button outside of its scope. It returns you a brand new button each and every time it is called. This function is therefore pure!


To prove the reliability of this pure function just created. Let's call it repeatedly and see if it returns the same thing. We can achieve this of course by using a loop.


swift_for_loop

This loop calls our pure function iteratively over the Y and X axis of our view. Since pure functions are deterministic, the result of this should be identical buttons laid out over different positions of the view. You can see the results below.


swift_for_loop

As you can tell by the image above, we have a grid of identical buttons as a result of calling our pure functions iteratively. This elegant grid was achieved without writing much code and with minimal side effect.


So what’s the benefit of using pure functions? Well for one, because pure functions do not have side effects, they make your code more loosely coupled and therefore more robust. Pure functions also make your code easy to test. You can test assertions for pure functions without much setup work. Lastly, pure functions also make your more reusable. In summary, pure functions allow for more loosely coupled, easy to test, reusable code. Use them as much as you can.


If you found this article useful, please support me and invest in yourself by getting my book Becoming More Intelligent. In it, I discuss methods to build what I call a 'Custom Mind'. A custom mind is a brain that has the instincts you consciously chosen, and has former unwanted instincts eliminated. There is no better investment than building a custom mind.


A custom mind is a brain that has the instincts you consciously chosen, and has former unwanted instincts eliminated. There is no better investment than building a custom mind.


keep_coding

Keep Coding!