Functional Programming Flashcards
What is functional programming?
Functional programming (often abbreviated FP) is the process of building software by composing pure functions, avoiding shared state, mutable data, and side-effects. Functions are first class citizens of such languages.
Which is imperative and which is declarative?
FP - Declarative and OOP - Imperative
Where does application state reside in FP vs OOP?
Application state flows through pure functions, as opposed to OOP where the state is usually shared and colocated with methods in objects.
Examples of some functional languages
LISP, Erlang, Haskell, F#, Clojure,
A pure function ____
- Given same input always returns same value
- Has no side effects
Function composition is ______
Is process of combining two or more functions to produce new function or produce some output.
f(g(x))
Shared state is ______
Any variable, object or memory that is shared scope or is a property of object being passed between scopes.
Problems with shared state
- To understand the behavior of function you need to know the history of every shared variable that function uses or affects
- Race condition
Examples of side effects
- Changing global variable or shared variable
- Logging to console
- Writing to file
- Writing to network
- Triggering any external process
- Calling other function that has side effects
What are higher order functions?
HOF is any function which takes function as argument, returns a function or both. They are at higher abstraction levels.
Eg. Monads, Promise, Async etc