Skip to main content

Universe of Functions

·480 words·3 mins
cyancaesar
Author
cyancaesar

Functional Programming
#

A programming approach to a problem as a mathematical functions with immutability of state changes.

  • Functions are the building blocks of your solution.
  • Functions act as an algorithm and data.
  • Problems are tackled and solved in smaller parts by composing.
  • Its a language agnostic, meaning it is not tied to a specific programming language and a way to model your problem.
  • Functions are treated as values to the solution.
  • Its declarative meaning you don’t explicitly say to the function how to do it like imperative but rather what to do.
  • Achieving declarative style by a chain of transformation over input through composing of functions.

Functions are simply transformations in this paradigm where one input produces one output.

In an imperative approach, a program is sequence of steps that gets executed until it reaches its desired result. Imperative solutions mutates its state and executes instructions instructions step by step.

On the other hand where declarative approach, we focus on the final result focusing how to do it to achieve the result. The final result doesn’t depend on any external factors of the program. Make use of immutability where function can’t mutate any internal or outer states between execution. Unlike the imperative approach, where order of executions matter. Here in declarative, orders aren’t always important, code can be executed asynchronous.

A function that takes same input and returns same output is called deterministic.

In functional design, functions is a first-class meaning:

  • A function can be passed as argument.
  • A function that returns another function, its called higher order function.
  • In general, function are the center of your designs and treated as values.

Functions in functional programming is always Total Function, meaning that the input set of the function has at most one output. Otherwise its called Partial Function where there is a value in the input set that doesn’t have value in the output. E.g. divide by zero function where it takes an input and divide that input by zero. If the input is zero, then there’s no output hence the function is called partial.

Pure Function
#

In functional programming, when modeling your solution/code, your function need to comprehend purity. Pure function has two properties that define the purity.

Side Effect
#

In order for purity, functions needs to be consistent and doesn’t cause any side effect and only its meant purpose is to compute the result based only on the passed argument. What side effects that could break the rule of purity?

  • Mutating outer state
  • Exchanging data with external service
  • File system read and write

Referential Transparency
#

The given input always produce the same output, deterministic right? Yes, otherwise its named unpredictable function that produces output based on inner state breaks this property.

So we conclude that a function is:

  1. Total. Every input returns an output
  2. Deterministic. Same input returns same output
  3. Pure. No side effects