Python - Functional Programming - Pure Function - Higher Order Functions - Memoization

Functional Programming


Functional programming is a style of programming that (as the name suggests) is based around functions. 
A key part of functional programming is higher-order functions. i.e. functions as objects. 


In mathematics and computer science, a higher-order function is a function that does at least one of the following:
  • takes one or more functions as arguments (i.e. procedural parameters),
  • returns a function as its result.
All other functions are first-order functions. In mathematics, higher-order functions are also termed operators or functionals. The differential operator in calculus is a common example, since it maps a function to its derivative, also a function. Higher-order functions should not be confused with other uses of the word "functor" throughout mathematics, see Functor (disambiguation).

In the untyped lambda calculus, all functions are higher-order; in a typed lambda calculus, from which most functional programming languages are derived, higher-order functions that take one function as argument are values with types of the form .


Higher-order functions take other functions as arguments or return them as results.
Example:
def apply_twice(func, arg):
return func(func(arg))

def add_five(x):
return x + 5

print(apply_twice(add_five, 10))

Pure Functions



Functional programming seeks to use pure functions. Pure functions have no side effects, and return a value that depends only on their arguments.
This is how functions in math work: for example, The cos(x) will, for the same value of x, always return the same result.
Below are examples of pure and impure functions.
Pure function:def pure_function(x, y):
temp = x + 2*y
return temp / (2*x + y)

Impure function:some_list = []

def impure(arg):
some_list.append(arg)

The function above is not pure, because it changed the state of some_list.
Using pure functions has both advantages and disadvantages. 
Pure functions are:
- easier to reason about and test.
- more efficient. Once the function has been evaluated for an input, the result can be stored and referred to the next time the function of that input is needed, reducing the number of times the function is called. This is called memoization.

- pure: when uses only own resources; - impure: when uses other functions and/or variables defined outside.
also, impure functions can change the value of variables used outside of the function.
MEMOIZATION EXPLAINED Memoization is whereby the result of a previous section can be referred and used to get the result of the next without having to go through a lot of work. Example. If you write 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 and asked for an answer, you will take time to do that and get 10 Now if I add another +1 at the end 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 You will instantly say 11 That is memoization. You will not go back to adding the whole list of 1s. A practical example can be in the calculation of factorials. Once you know that 3! is 6 (ie 1 * 2 * 3) then if I asked you 4! you simply take 3! * 4 = 24 other than doing it over again as 1 * 2 * 3 * 4 Now let us apply Memoization: 9! = 362880 find 10! Answer: Simply 9! * 10 = 3628800

- easier to run in parallel.
The main disadvantage of using only pure functions is that they majorly complicate the otherwise simple task of I/O, since this appears to inherently require side effects. 
They can also be more difficult to write in some situations.











Comments

Popular posts from this blog

Python - How to Escape special characters in String

Python - Argument Passing