What is Functional Programming?

swift

For quite sometime now I am on a new programming language from Apple – Swift. Apart from the hype Swift got there are multiple programming paradigms that I got introduced to – Functional Programming (FP), Protocol oriented programming (PoP).

I was little curious as I knew what functions are and what protocols are used for especially in objective-C, and my mind asked me few questions –

  • What are Functional Programming and Protocol oriented programming?
  • Is swift a Functional Programming language or a Protocol oriented programming language?
  • Is it mandatory to learn in Functional Programming or Protocol oriented programming to code in Swift?
  • What is the right way?

Browsing through some websites and reading few books, this is what I understand. In this post I will be sharing my thoughts & understanding on FP and a subsequent post on PoP.

Q: What is Functional Programming? Answer: (In non-developer’s term) Functional Programming is a way of how you code your solution, it’s a philosophy, it’s not related to any specific programming language.

Technical Answer: Functional programming is a way of programming where focus is on designing functions and their interactions rather than on data unlike Object Oriented Programming(OOP) where focus in on designing classes around data.

A functional programming language supports and encourages programming without side-effects. FP is a way in which you code only pure functions with all objects used in the function are passed as input parameters and all results are defined as output/return objects. In here, Functions are always pure and deterministic.

Now, what are pure functions? Answer: Functions that returns same value for given set of input values at any given point in time. These functions never modify the input values, never access, consume or modify anything outside of its scope.

Technical Answer: The result cannot depend on any hidden objects/values/state that change over a period of time. The function, In the process of execution, cannot cause any semantically observable side effects, like mutation of mutable objects or state change of any other objects.

Following holds true for FP:

  • In FP the focus is on how you design functions rather than modeling the data.
  • In FP utmost preference is given to immutability. FP hates mutating data.
  • In FP results are always same for same values supplied.
  • Functions in FP does not change any values supplied to it, neither change values outside of scope. Input parameters are only consumed but not modified.
  • In FP like Swift and Scala results are most of the time return in containers/optionals. (Yes, I dug into Scala programming language concept to understand few concepts of FP. Optionals in Swift are ‘inspired’ from Monads from Scala 😀 )

Advantages of Functional Programming:

  1. You definitely know what functions do and what they return. No side-effects.
  2. You need not worry much about wrapping data into threads, mutex and locks.
  3. Helps in writing lots of Unit test cases, increasing the code coverage.
  4. Modules can be isolated and independent from each other.
  5. Crash free: Functions return values wrapped in containers [Swift, Scala]

Keeping in mind all of this 2 questions raise in mind:

  1. Can we adopt functional programming in Swift? Yes, Definitely!
  2. Can we build iOS Apps in Swift completely with FP philosophy? Partially Yes. Why? Given my experience with the MV-X (MVC, MVVM, etc.,) patterns used in iOS app development access object properties and mutating datasources somewhere in the code is a must and cannot be avoided. So it is up to a developer to use their creativity to design functions or some modules in an app which adhere completely to FP.

I am new to FP and still experimenting with it, trying to understand it still more deeply. I also thought of adding few code snippets in this post to clearly explain and convert Impure functions into Pure ones, but surely we can discuss this in comments. Let’s discuss in comments section about these interpretations and understandings.

Keep visiting this blog for more programming stuff. Next post on Protocol oriented Programming.

Referred links which helped me understand:

 

Compiler directives @ objective-C 2.0

Here are the list of directives in Objective-C 2.0. Lemme know if anything is missing.

// directives used in class declaration
@interface @public @package @protected @private @property @end

// directives used in class implementation
@implementation @synthesize @dynamic @end

// creating a string object
@”string”

// for forward declaration
@class

// similar to function pointers
@selector()

// used for method declaration mainly for delegation purpose
@protocol @required @optional @end

// to assure thread safety
@synchronized

// handling memory issues (useful in ARC enabled apps)
@autoreleasepool

// throwing and handling exceptions
@throw @try @catch @finally

// type encoding (Apple docs)
@encode

// aliasing an existing class name to a new one (useful in refactoring)
@compatibility_alias

[ Examples of all these directives are on the way 😉 ]

[reader enjoy:keep_programming];