Functional Programming (FP) (What is a Function Type? (Function Type…
What is the Functional Programming Paradigm?
One Main Function which can call further functions to achieve a specific tasks.
A Function can call other functions or itself (recursion)
The Functional Programming Paradigm is an example of a
Declarative Programming Language
This means all the algorithms call Functions.
Lines of code look and behave like mathematical Functions, requiring an Input and an Output.
Why Use the Functional Programming paradigm?
Program Requirements may be better defined as a series of abstractions based on Functions rather than a more complex series of steps.
Broader Abstractions can lead to fewer errors during implementation.
Functions can be applied at any level of data abstraction making them highly re-usable within a program.
Functional Code is easier to test and debug as each Function cannot have any side effects, only needing testing once.
There are no
issues as no data is modified by two threads at the same time.
In multi-processor environments the sequence that Functions are evaluated in are not critical.
How are Functions Used to Construct Programming Code?
Applies a given function to every element within a list and returns a corresponding list of results.
Map Function Diagram
Processes a list and creates a new list that only contains elements that meet certain criteria.
Filter Function Diagram
What is a Function Type?
refers to the way in which the expression is created.
All Functions are of the type
, with A an argument type and B a result type.
A is also called the
and contains objects within a data type.
B is called the
and is the set from which the output values are chose.
Not every value that exists in the codomain will necessarily be output.
What's a First Class Object and How are they used in code?
In FP, Functions are first-class objects, meaning they have certain properties can can be used in particular ways within the program.
A first-class object is any object that can be passed as an argument to or can be returned by a Function.
Other objects such as integer values , which can be passed as arguments to a Function, are also first-class objects.
Higher order functions
are functions that can accept other functions as arguments.
Three most common are
How to Apply Functions Fully and Partially
is the process of providing a function with its inputs.
Partial Function Application
is the process of applying a Function by creating an intermediate function by fixing some of the arguments to the function.
- There's no such thing as a Partial Function, just Partial Application of a Function.
How to Combine Existing Functions to make new ones.
- the process of creating a new function by combining 2 existing functions.
A key principle of Functional Programming as the concept is to have complex functions that are in turn made up of simpler functions.
The result of f() is sent through g()
It is written: (g º f)
Which means: g(f(x))
If f(x) = 2x+3 and g(x) = x^2
(g º f) = g(f(x)) = (2x+3)^2