Skip to content

Commit 69d5dc8

Browse files
Boris MarinovBoris Marinov
Boris Marinov
authored and
Boris Marinov
committed
Add definitions: Value, Constant, Composition
1 parent 296ad67 commit 69d5dc8

File tree

1 file changed

+49
-1
lines changed

1 file changed

+49
-1
lines changed

readme.md

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,29 @@ let sum = (a, b) => a + b;
7070
let curriedSum = (a) => (b) => a + b;
7171

7272
curriedSum(40)(2) // 42.
73+
```
74+
---
75+
76+
## Composition
77+
78+
> A function which combines two values of a given type (usually also some kind of functions) to a third value of the same type.
79+
80+
The most straightforward type of composition, is called "normal function composition".
81+
It allows you to combines functions which accept and return a single value.
82+
83+
```js
84+
const compose = (f, g) => a => f(g(a)) // Definition
85+
const floorAndToString = compose((val)=> val.toString(), Math.floor)(222.44) //Usage
86+
floorAndToString(121.212121) // "121"
87+
7388
```
7489

7590
---
7691

7792
## Purity
7893

7994
> A function is said to be pure if the return value is only determined by its
80-
input values, without any side effects.
95+
input values, without any side effects and mutations.
8196

8297
```js
8398
let greet = "yo";
@@ -157,6 +172,37 @@ Points-free function definitions look just like normal assignments without `func
157172
158173
---
159174

175+
## Value
176+
177+
> Any complex or primitive value that is used in the computation, including functions. Values in functional programming are assumed to be immutable.
178+
179+
```js
180+
5
181+
Object.freeze({name: 'John', age: 30}) // The `freeze` function enforces immutability.
182+
(a) => a
183+
```
184+
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.
185+
186+
---
187+
188+
## Constant
189+
190+
> 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.
191+
```js
192+
const five = 5
193+
const john = {name: 'John', age: 30}
194+
```
195+
Constants are referentially transparent. That is, they can be replaced with the values that they represent without affecting the result.
196+
In other words with the above two constants the expression:
197+
198+
```js
199+
john.age + five === ({name: 'John', age: 30}).age + (5)
200+
201+
```
202+
Should always return `true`.
203+
204+
---
205+
160206
## Functor
161207

162208
> 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 `[]`
296342
```js
297343
[1, 2].concat([]); // [1, 2]
298344
```
345+
Functions also form a monoid with the normal functional compositon as an operation and the function which returns its input `(a) => a`
346+
299347

300348
---
301349

0 commit comments

Comments
 (0)