mirror of
https://github.com/rjNemo/functional-programming-jargon
synced 2026-06-06 02:26:43 +00:00
Add definitions: Value, Constant, Composition
This commit is contained in:
parent
296ad67652
commit
69d5dc87bf
1 changed files with 49 additions and 1 deletions
50
readme.md
50
readme.md
|
|
@ -70,6 +70,21 @@ let sum = (a, b) => a + b;
|
|||
let curriedSum = (a) => (b) => a + b;
|
||||
|
||||
curriedSum(40)(2) // 42.
|
||||
```
|
||||
---
|
||||
|
||||
## Composition
|
||||
|
||||
> A function which combines two values of a given type (usually also some kind of functions) to a third value of the same type.
|
||||
|
||||
The most straightforward type of composition, is called "normal function composition".
|
||||
It allows you to combines functions which accept and return a single value.
|
||||
|
||||
```js
|
||||
const compose = (f, g) => a => f(g(a)) // Definition
|
||||
const floorAndToString = compose((val)=> val.toString(), Math.floor)(222.44) //Usage
|
||||
floorAndToString(121.212121) // "121"
|
||||
|
||||
```
|
||||
|
||||
---
|
||||
|
|
@ -77,7 +92,7 @@ curriedSum(40)(2) // 42.
|
|||
## Purity
|
||||
|
||||
> A function is said to be pure if the return value is only determined by its
|
||||
input values, without any side effects.
|
||||
input values, without any side effects and mutations.
|
||||
|
||||
```js
|
||||
let greet = "yo";
|
||||
|
|
@ -157,6 +172,37 @@ Points-free function definitions look just like normal assignments without `func
|
|||
|
||||
---
|
||||
|
||||
## Value
|
||||
|
||||
> Any complex or primitive value that is used in the computation, including functions. Values in functional programming are assumed to be immutable.
|
||||
|
||||
```js
|
||||
5
|
||||
Object.freeze({name: 'John', age: 30}) // The `freeze` function enforces immutability.
|
||||
(a) => a
|
||||
```
|
||||
Note that the value-containing structures defined below such as [Functor](#functor), [Monad](#monad) etc. are themselves values. This means, among other things, that they can be nested within each other.
|
||||
|
||||
---
|
||||
|
||||
## Constant
|
||||
|
||||
> An immutable reference to a value. Not to be confused with `Variable` - a reference to a value which can at any point be updated to point to a different value.
|
||||
```js
|
||||
const five = 5
|
||||
const john = {name: 'John', age: 30}
|
||||
```
|
||||
Constants are referentially transparent. That is, they can be replaced with the values that they represent without affecting the result.
|
||||
In other words with the above two constants the expression:
|
||||
|
||||
```js
|
||||
john.age + five === ({name: 'John', age: 30}).age + (5)
|
||||
|
||||
```
|
||||
Should always return `true`.
|
||||
|
||||
---
|
||||
|
||||
## Functor
|
||||
|
||||
> An object with a `map` function that adhere to certains rules. `Map` runs a function on values in an object and returns a new object.
|
||||
|
|
@ -296,6 +342,8 @@ The identity value is empty array `[]`
|
|||
```js
|
||||
[1, 2].concat([]); // [1, 2]
|
||||
```
|
||||
Functions also form a monoid with the normal functional compositon as an operation and the function which returns its input `(a) => a`
|
||||
|
||||
|
||||
---
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue