- Programming jargon
- example of procedural vs functional in JS: for loop vs forEach
- how to do something
- uses statements (e.g. for, if, switch) that changes the program flow
- groups instructions into procedures
- e.g. C
- groups instructions with the part of the state they operate on
- objects contain properties and methods
- e.g. Java
- what the problem is = expresses the logic, not how to solve the problem
- doesn't describe its control flow,
- relies on expressions (function calls, values and operators)
- desired result is declared as the value of a series of pure function applications
- avoids: shared state, mutating state and side effects
- shared state = any non-constant variable used by multiple separate scopes
- e.g. Haskell
- based on formal logic
- in which the desired result is declared as the answer to a question about a system of facts and rules
- e.g. Prolog
- desired result is declared as the solution of a optimization problem
- e.g. MatLab
- doesn't require compilation
- are interpreted at runtime
- usually have less access to the computer's native abilities
- requires compilation
- run faster because they're first converted to native machine code
- errors are caught before execution
- or call-by-need delays the evaluation of an expression until its value is needed
- benefits: define potentially infinite data structures, performance increase by avoid needless calculations
- e.g. Haskell
- produces a value(s); an expression is evaluated
- often don't have side effects
- e.g. values and function
- syntactic unit of an imperative programming language
- performs actions; a statement is executed
- don't return results and are executed solely for their side effects
- many statements are introduced by identifiers like if, while, for
- expressions that have side effects
- e.g. assign/change a variable
- in many languages (C++) a expression becomes an expression statement when is ended with a semicolon
- this makes the program evaluate the expression only for its side-effects and disregard its result
- function call's outer environment
- define which variables are available to a specific function execution
- scope is defined by the location of its definition within the source code, not by the location of its call
- if a variable is not available in the local environment the function always go searching one level up from where it was written until it reaches the global level
- most languages have this type of scope
- JavaScript = the only dynamically scoped mechanism is
this
inside an non-arrow function
- JavaScript = the only dynamically scoped mechanism is
- scope is defined by the location of the function calling, not by the location of its definition
- bash is the most common language to have this type of scope
- takes a function as argument or returns a function
- e.g. map, filter...
- a language that allow this is said to have first-class functions, i.e. it treats function as first-class citizens
- a function you provide to another piece of code, allowing it to be executed by that code
- execution may be immediate as in a synchronous callback, or it might happen at a later time as in an asynchronous callback
- implemented in languages with first-class functions (functions can return a function)
- stores a function together with its lexical environment (referenced variables of the outer function)
- functions that call themselves within their code
- usually recursive problems can also be solved with iteration
- function which takes multiple arguments one at a time
- in Haskell all functions are curried; this is hide in notation and not apparent
- *applying a function to its arguments in order to produce a return value
- apply a function to some of its arguments
- this function will return a function with fewer parameters
- the result of each function is passed as the argument of the next, and the result of the last one is the result of the whole
- e.g.
z = f(g(x));
= C[3, 5, 8].map(x => x * 2).filter(x => x >= 10)
= JavaScript functional style(f . g . z) x
= in Haskell (equivalent tof (g (z x))
)
- function definition not bound to an identifier
- often, is used in a higher-order function
- any object that can be mapped over
- apply a function in a specific context (type)
- JavaScript’s built in array and promise objects act like functors
.then()
is an asynchronous.map()
- monoid is when you have an associative binary function and a value which acts as an identity with respect to that function
- acts as an identity = when called with that function and some other value, the result is always equal to that other value
- e.g. number 1 is the multiplication identity
- pattern for chaining (compose) functions that require context (computation, branching, I/O) in addition to the return value
- if you have a value with a context (M a) how do you apply to it a function that takes a normal 'a' and returns a value with a context
- Functors map with context, monads flatten and map (flatMap) with context
- monads are a type of functor but with the special power to unwrap any value from its context using the flatMap
- usefulness: needed because lots of functions aren’t simple mappings from
a => b
. Some functions to deal with side effects (promises, streams), handle branching (Maybe), deal with exceptions (Either)
- no side-effects; only produces and returns a value
- deterministic: same input always returns the same output
- referential transparency is a property of pure functions
- allows the replacement of a function call with its resulting value without changing the meaning of the program
- specify flow of control in programs
- sequential execution of code statements (one line after another)
- e.g. if-else, case & switch
- e.g. for loop, while, repeat
- is the degree of interdependence between software modules and routines
- modules and routines are highly dependent on one another
- disadvantages: changes cause ripple effect, harder to reuse
- promotes independency, single-responsibility and separation of concerns
- advantages: changes are easier to make, code reusability
- which operator gets called first
- what order should operators of the same precedence (without parentheses) be called
- left-to-right or right-to-left
- e.g. plus (
+
)
AND
,OR
,NOT
- e.g. equal (
=
)
- usually
=
,:=
or<-
- usually
.
- typing = assigning a data type
- capability of the type system to deduce the type of a variable
- type of variables are know at compile time
- this means that you as the programmer must specify what type each variable is (e.g. Java, C++); other languages offer some form of type inference (e.g. Haskell, Scala)
- a lot of trivial bugs are caught early
- don't have to specify type
- type of variables are know at run time (the interpreter will assign a type based on the variable's value)
- e.g.
- Python, PHP, JavaScript
- scripting languages have this feature as there is no compiler
- stricter rules, error and exceptions are more likely to happen
- implicit conversion doesn't happen
- may produce unpredictable or may perform implicit type conversion
one() && two()
=two
will be executed only ifone
returns a truthy valueone() || two()
=two
will be executed only ifone
returns a falsy value
42 === myValue
= solves a very common mistake: assign a value unintentionally instead of writing a conditional statement
4.1.7-beta
- 4 = major
- 1 = minor
- 7 = patch
- beta = optional pre-release label
^
= minor releases~
= patch releases
- variables defined at function definition
- values passed to a function call