You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: readme.md
+92-39Lines changed: 92 additions & 39 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -30,7 +30,7 @@ __Table of Contents__
30
30
*[Auto Currying](#auto-currying)
31
31
*[Function Composition](#function-composition)
32
32
*[Continuation](#continuation)
33
-
*[Purity](#purity)
33
+
*[Pure Function](#pure-function)
34
34
*[Side effects](#side-effects)
35
35
*[Idempotent](#idempotent)
36
36
*[Point-Free Style](#point-free-style)
@@ -39,6 +39,9 @@ __Table of Contents__
39
39
*[Category](#category)
40
40
*[Value](#value)
41
41
*[Constant](#constant)
42
+
*[Constant Function](#constant-function)
43
+
*[Constant Functor](#constant-functor)
44
+
*[Constant Monad](#constant-monad)
42
45
*[Functor](#functor)
43
46
*[Pointed Functor](#pointed-functor)
44
47
*[Lift](#lift)
@@ -78,16 +81,20 @@ __Table of Contents__
78
81
79
82
## Arity
80
83
81
-
The number of arguments a function takes. From words like unary, binary, ternary, etc. This word has the distinction of being composed of two suffixes, "-ary" and "-ity." Addition, for example, takes two arguments, and so it is defined as a binary function or a function with an arity of two. Such a function may sometimes be called "dyadic" by people who prefer Greek roots to Latin. Likewise, a function that takes a variable number of arguments is called "variadic," whereas a binary function must be given two and only two arguments, currying and partial application notwithstanding (see below).
84
+
The number of arguments a function takes. From words like unary, binary, ternary, etc.
82
85
83
86
```js
84
87
constsum= (a, b) => a + b
88
+
// The arity of sum is 2 (binary)
89
+
constinc=a=> a +1
90
+
// The arity of inc is 1 (unary)
91
+
constzero= () =>0
92
+
// The arity of zero is 0 (nullary)
93
+
```
85
94
86
-
constarity=sum.length
87
-
console.log(arity) // 2
95
+
__Further reading__
88
96
89
-
// The arity of sum is 2
90
-
```
97
+
*[Arity](https://en.wikipedia.org/wiki/Arity) on wikipedia.
A closure is a way of accessing a variable outside its scope.
111
-
Formally, a closure is a technique for implementing lexically scoped named binding. It is a way of storing a function with an environment.
112
-
113
117
A closure is a scope which captures local variables of a function for access even after the execution has moved out of the block in which it is defined.
114
-
ie. they allow referencing a scope after the block in which the variables were declared has finished executing.
118
+
This allows the values in the closure to be accessed by returned functions.
115
119
116
120
117
121
```js
118
122
constaddTo=x=>y=> x + y
119
123
var addToFive =addTo(5)
120
-
addToFive(3) //returns 8
124
+
addToFive(3) //=> 8
121
125
```
122
-
The function ```addTo()``` returns a function(internally called ```add()```), lets store it in a variable called ```addToFive``` with a curried call having parameter 5.
123
-
124
-
Ideally, when the function ```addTo``` finishes execution, its scope, with local variables add, x, y should not be accessible. But, it returns 8 on calling ```addToFive()```. This means that the state of the function ```addTo``` is saved even after the block of code has finished executing, otherwise there is no way of knowing that ```addTo``` was called as ```addTo(5)``` and the value of x was set to 5.
125
-
126
-
Lexical scoping is the reason why it is able to find the values of x and add - the private variables of the parent which has finished executing. This value is called a Closure.
127
126
128
-
The stack along with the lexical scope of the function is stored in form of reference to the parent. This prevents the closure and the underlying variables from being garbage collected(since there is at least one live reference to it).
129
-
130
-
Lambda Vs Closure: A lambda is essentially a function that is defined inline rather than the standard method of declaring functions. Lambdas can frequently be passed around as objects.
131
-
132
-
A closure is a function that encloses its surrounding state by referencing fields external to its body. The enclosed state remains across invocations of the closure.
127
+
In this case the `x` is retained in `addToFive`'s closure with the value `5`. `addToFive` can then be called with the `y`
128
+
to get back the sum.
133
129
134
130
__Further reading/Sources__
135
131
*[Lambda Vs Closure](http://stackoverflow.com/questions/220658/what-is-the-difference-between-a-closure-and-a-lambda)
@@ -139,7 +135,6 @@ __Further reading/Sources__
139
135
140
136
Partially applying a function means creating a new function by pre-filling some of the arguments to the original function.
141
137
142
-
143
138
```js
144
139
// Helper to create partially applied functions
145
140
// Takes a function and some arguments
@@ -206,7 +201,7 @@ __Further reading__
206
201
207
202
## Function Composition
208
203
209
-
The act of putting two functions together to form a third function where the output of one function is the input of the other.
204
+
The act of putting two functions together to form a third function where the output of one function is the input of the other. This is one of the most important ideas of functional programming.
A function is pure if the return value is only determined by its
251
-
input values, and does not produce side effects.
245
+
A function is pure if the return value is only determined by its input values, and does not produce side effects. The function must always return the same result when given the same input.
252
246
253
247
```js
254
248
constgreet= (name) =>`Hi, ${name}`
@@ -297,10 +291,6 @@ console.log('IO is a side effect!')
297
291
298
292
A function is idempotent if reapplying it to its result does not produce a different result.
299
293
300
-
```
301
-
f(f(x)) ≍ f(x)
302
-
```
303
-
304
294
```js
305
295
Math.abs(Math.abs(10))
306
296
```
@@ -320,16 +310,14 @@ const add = (a) => (b) => a + b
320
310
321
311
// Then
322
312
323
-
// Not points-free - `numbers` is an explicit argument
313
+
// Not point-free - `numbers` is an explicit argument
`incrementAll` identifies and uses the parameter `numbers`, so it is not points-free. `incrementAll2` is written just by combining functions and values, making no mention of its arguments. It __is__ points-free.
331
-
332
-
Points-free function definitions look just like normal assignments without `function` or `=>`.
320
+
Point-free function definitions look just like normal assignments without `function` or `=>`. It's worth mentioning that point-free functions are not necessarily better than their counterparts, as they can be more difficult to understand when complex.
333
321
334
322
## Predicate
335
323
A predicate is a function that returns true or false for a given value. A common use of a predicate is as the callback for array filter.
@@ -376,6 +364,27 @@ To be a valid category 3 rules must be met:
376
364
377
365
Since these rules govern composition at very abstract level, category theory is great at uncovering new ways of composing things.
378
366
367
+
As an example we can define a category Max as a class
0 commit comments