Functional Programming 101

02 December5 min read
Functional Programming 101

1) Functional programming

Judging by its name - functional programming uses  mostly functions when it comes to writing code. It is a declarative programming style that is focused on the question “What to solve?”. Functional programming uses mainly pure functions.

Functional programming is based on Lambda Calculus - a framework, developed by Alonzo Church. It gives the definition of what is computable. Lambda Calculus is the theoretical framework that is used to describe functions. 

Functional languages such as Erlang, Haskell, Elixir, Scala  are great for building more reliable distributed solutions. Compared to object-oriented programming this approach offers better modularity and a code that can be easily tested. 

Functional programs also tend to function with a shorter code due to their higher level of abstraction. The use of immutable data saves time and boosts the overall productivity and performance.

Functional programming consist of couple of concepts:

1.1 Pure functions

They have two main properties - they always produce the same output for the same parameters and have no side-effects. For instance in Erlang and its predecessor - Elixir, there are only pure functions and they always return something. 

1.2 Recursion

Iteration is usually accomplished via recursion. Recursive functions invoke themselves with different parameters until a base case is reached.

1.3. Referential transparency

Variables once defined do not change their value throughout the program. If a value needs to be stored, a new variable must be defined. This way any chance of side-effects is eliminated, because variables within the same scope cannot be re-assigned.

1.4. Functions are First-Class and can be Higher-Order

First-Class functions are treated like variables. First-Class functions can be passed down to functions as parameters and can be returned or stored in data structures. 

Higher order functions are related to first-class functions. They can both take other functions as arguments and return them as a result.

1.5. Variables are immutable

You can’t modify a variable after it’s been initialized.

Functional programming is used in big and dynamic projects, rapidly growing companies, telecoms, and fintech.

Functional programming is about concurrency and distribution. Most of the languages based on this stigma are fault-tolerant. They are giving the opportunity to develop speedy programs that could run on multicore CPUs. Functional languages are suitable for distributed applications where users can interact with each other. 

2) Functional Programming Vs Object Oriented Programming

Functional programming excels when all objects are known, but have behavior that might change. Functional programming emphasizes on evaluation of functions, while object oriented programming is based on the concept of objects. The data in functional programming is immutable, while in the OOP it is immutable. FP uses declarative programming model and OOP follows the imperative programming model. FP supports parallel programming, which is not supported in the OOP. In FP statements are executed by any order when in OOP are executed in particular order. The basic elements in FP are variables and functions while in OOP are objects and methods.

Which one is better depends on the business or the software needs. If you are looking for a solution that is based on a lot of operations and your application needs to scale horizontally - you go straight ahead to functional programming, but if your focus is on common behavior you can use object-oriented programming.

3) Functional programming languages

a) Erlang

Erlang is a general-purpose programming language with a runtime environment. It provides built-in support for concurrency and distribution. The language is fault-tolerant and is used to build massively scalable soft real-time systems with requirements of high availability. 

Erlang is a safe technology used in businesses with high levels of workload such as the telecommunication industry. Other industries that are using Erlang are banking, e-commerce, computer telephony and instant messaging. Erlang’s runtime system has built-in support for concurrency, distribution and fault tolerance. 

Erland is used by Facebook, WhatsApp, Motorola, Bet365, IBM, Ericsson, Cisco.

b) Elixir

Elixir, the predecessor of Erlang, is a dynamic, growing functional language used for building scalable and maintainable applications. The language is revolutionary for software development thanks to its unique features such as fault-tolerance and introspecting reports. Elixir combines the robust functional programming of Erlang with a syntax similar to Ruby and includes powerful features for metaprogramming.

Elixir is successfully used in web development, embedded software, data ingestion, and multimedia processing. The interactive development environment enables this technology to be easily used for creating stable high-performing solutions across a wide range of industries. The language is preferred by many experts around the world as it offers many benefits such as great syntax, productive tooling and an extensible design.

Elixir runs on the Erlang VM. 

This is the language used by companies such as Toyota, Pinterest, Spotify, PepsiCo, Discord.

c) Haskell

Haskell is a great functional language suited for academic research and industrial application. It maximizes the workflow and saves memory by offering features such as lazy evaluation, lambda expressions, pattern matching, type classes and type polymorphism.

Haskell is a time-proven technology that stands apart from the competition, facilitating testing and debugging.

4) In which industries is it best to use functional programming?

Functional programming is best used in industries that require many simultaneously running processes. 

Currently functional programming is mostly used in the finance industry and social media platforms. Facebook has used functional programming in the newsfeed for iOS customers. The feed is updated constantly and functions are improving data integrity and reliability. Twitter’s backend is also written in a  functional programming language - Scala. They chose it as they needed a language that could handle scaling and offer data security.