Imperative programming vs Functional programming – a Beginner’s approach Part 2: Filter

1. Introduction

Computer programming is about solving problems and getting things done the right way. However, as a beginner, you don’t care how you do it as long as you get it done. It works so why bother tackling it in a completely different way after all?

I thought the same way when I first stumbled upon functional programming in Swift. I knew about functional programming languages like Haskell or Scala, but I was under the impression that they are only good for research at an academic level and have no practical value of any kind. So I approached it reluctantly at first and soon asked myself how I lived without it.

The biggest challenge you face when discovering new programming paradigms is changing the way you think. This can be scary at first because you are used to doing things in a certain way and now you suddenly feel overwhelmed. It takes time and patience to get used to anything new but it’s worth doing it in the long run – being able to choose the right technique for a certain problem is a skill that any software developer should have in his toolbox.

OK, enough talking – let’s see what functional programming is all about.

2. The imperative approach

Your task for this tutorial is an easy one: create an array with the even elements of a given array. Before diving into coding think how you would solve the problem and break it into steps. This is how I would do it:

  • create the initial array and populate it with default values;
  • create the new array;
  • loop through the original array and do three things for each iteration:
    • extract the current value from the array;
    • test if it’s even;
    • add it to the new array if the tests succeeds;
  • print the new array to the console;

That’s it for the algorithm – let’s move on to the implementation.

3. The imperative implementation

Fire up Xcode and open a playground. Delete everything in it and add this line to kick things off:

let array = [1, 2, 3, 4, 5]

This creates the initial constant array and assigns its default values.

Note: There are many different ways of creating arrays in Swift – this is the easiest approach of all because it uses type inference to determine the array’s type – Int in this case.

Next create the new array:

var evenArray = [Int]()

This line creates a variable array of integers – it is variable because you are going to add values to it soon.

Now loop through the original array with the for in control flow statement:

for value in array {

}

Note: There are many ways of looping in Swift – this is the best approach for arrays because it gives you access to the current value for each iteration.

There are two things you should do inside the for loop for each value of the array. First test if the value is even:

if value % 2 == 0 {

}

Note: You can also use guard instead of if and take the happy path approach instead (Download).

Then add it to the new array inside the if statement if the test succeeds:

evenArray.append(value)

Finally, print the new array to the console:

print(evenArray, terminator: "")

Note: There are other ways to print to the console as well – this approach doesn’t print the newline “\n” character in the playground.

That’s it – time for your next challenge: create an array with the odd elements of a given array.

4. The imperative implementation revisited

First create the new array:

var oddArray = [Int]()

Next loop through the original array:

for value in array {

}

Now test if the value is odd and add it to the new array if the test succeeds:

if value % 2 == 1 {

   oddArray.append(value)

}

Finally, print the new array to the console:

print(oddArray, terminator: "")

That’s it – let’s compare the two implementations.

5. The imperative implementation analysed

Both implementations are similar:

  • create the new array;
  • loop through the original array;
  • test the current value;
  • append it to the new array if the test succeeds;
  • print the new array to the console;

The only major difference between them is the test’s condition – you check whether the remainder of the value’s division by 2 is either 0 or 1.

Note: You could refactor the whole thing in two ways:

Both approaches avoid code redundancy and respect the DRY (do not repeat yourself) principle, but they don’t deal with the boilerplate code that is common for both implementations. Functional programming to the rescue!

6. The functional approach

Functional programming is about focusing only on what changes and not caring about what stays the same. The only thing that varies between the two previous implementations is the logic that filters each value in the original array, so this is the only thing you have to worry about.

The filter function does exactly this: it creates an array from the elements of another array that respect a certain condition.

Enough theory – let’s see the filter function in action!

7. The functional implementation

Add this line to the playground to get started:

let evenfArray = array.filter{$0 % 2 == 0}

The filter function uses a closure – a fancy word for anonymous function – as its parameter. The closure’s body is the condition that filters the array’s elements and the closure’s argument – $0 – is the array’s current value.

Note: The closure’s body can be written in many other ways in Swift – this is the simplest approach of all because it uses trailing closure syntax and shorthand argument names.

Now print the array to the console:

print(evenfArray, terminator: "")

That’s it – it can’t get any simpler than that! Let’s use the filter function some more.

8. The functional implementation revisited

Create an array with the odd elements of a given array:

let oddfArray = array.filter{$0 % 2 == 1}

Now print the array to the console:

print(oddfArray, terminator: "")

That’s it – let’s compare the two implementations.

9. The functional implementation analysed

Both implementations are similar – declare the filtering condition in the closure and let the filter function do the rest – no more boilerplate code.

Note: You could refactor the whole thing by encapsulating the algorithm in a function of its own to avoid code redundancy and respect the DRY principle (Download).

10. Imperative vs functional comparison

Imperative advantages:

  • clear steps
  • easy to understand

Imperative disadvantages:

  • boilerplate code

Functional advantages:

  • no boilerplate code
  • short and sweet

Functional disadvantages:

  • difficult to understand

11. Conclusion

Both paradigms have their pros and cons – feel free to try them both and choose the one that suits you best. Happy coding! 🙂

Note: All the code in this tutorial was tested with a playground in Xcode 7.1 and Swift 2.1 (Download).

Advertisements
Posted in Functional programming

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: