Skip to content

Commit f377cfc

Browse files
committed
pure functions
1 parent 7404bcc commit f377cfc

File tree

6 files changed

+133
-11
lines changed

6 files changed

+133
-11
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
## Setting up alias in ~/.gitconfig file with which I can combine multiple commands in a single command - last implemented on 25-Jul-2018
22

3-
In $HOME/.gitconfig or .git/config include the below along with what ever was already existing in that file
3+
In \$HOME/.gitconfig or .git/config include the below along with what ever was already existing in that file
44

55
```
66
[alias]
@@ -9,19 +9,17 @@ In $HOME/.gitconfig or .git/config include the below along with what ever was al
99

1010
## And from now just do a push request with the following single command which will do all the above jobs set up in the above alias
1111

12-
## $ git p "message ..."
13-
12+
## \$ git p "message ..."
1413

1514
## How to alias 'git checkout' to 'git co'
1615

1716
The command:
1817

19-
``git config --global alias.co checkout``
20-
18+
`git config --global alias.co checkout`
2119

2220
will create a git alias to do that. It will add the following entry into your global ~/.gitconfig file:
2321

2422
```js
25-
[alias]
26-
co = checkout
27-
```
23+
[alias];
24+
co = checkout;
25+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
#### Basic example of mutations
2+
3+
```js
4+
let state = {
5+
wardens: 900,
6+
animals: 800
7+
};
8+
```
9+
10+
This above Object holds the information of a Zoo application. If we change the number of animals in the state Object:
11+
12+
```js
13+
let state = {
14+
wardens: 900,
15+
animals: 800
16+
};
17+
state.animals = 90;
18+
```
19+
20+
Our state object will hold/encode a new information:
21+
22+
```js
23+
state = {
24+
wardens: 900,
25+
animals: 90
26+
};
27+
```
28+
29+
This is called mutation.
30+
31+
Immutability comes when we want to preserve our state. To keep our state from changing we have to create a new instance of our state objects.
32+
33+
```js
34+
function bad(state) {
35+
state.prp = "yes";
36+
return state;
37+
}
38+
39+
function good(state) {
40+
let newState = { ...state };
41+
newState.prp = "yes";
42+
return newState;
43+
}
44+
```
45+
46+
Immutability makes our app state predictable, ups the performance rate of our apps and to easily track changes in state.
47+
48+
#### Pure Functions, Side Effects
49+
50+
Pure functions are functions that accept an input and returns a value without modifying any data outside its scope(Side Effects). Its output or return value must depend on the input/arguments and pure functions must return a value.
51+
52+
```js
53+
function impure(arg) {
54+
finalR.s = 90;
55+
return arg * finalR.s;
56+
}
57+
```
58+
59+
The above function is not a pure function because it modified a state finalR.s outside its scope.
60+
61+
```js
62+
function impure(arg) {
63+
let f = finalR.s _ arg
64+
}
65+
```
66+
67+
The above function also isn’t a pure function because it didn’t return a value though it didn’t modify any external state.
68+
69+
```js
70+
function impure(arg) {
71+
return finalR.s _ 3
72+
}
73+
```
74+
75+
The above function is impure, though it didn’t affect any external state, its output return finalR.s \_ 3 isn't dependent on the input arg. Not only must pure function return a value but it must depend on the input.
76+
77+
```js
78+
function pure(arg) {
79+
return arg \_ 4
80+
}
81+
```
82+
83+
The above is a pure function. It didn’t side effect any external state and it returns an output based on the input.
84+
85+
#### 1. A pure function is deterministic. This means, that given the same input, the function will always return the same output. To illustrate this as a function in mathematical terms it is a well defined function. Every input returns a single output, every single time.
86+
87+
A pure function
88+
89+
`const add = (x, y) => x + y` // A pure function
90+
91+
add is a pure function because it’s output is solely dependent on the arguments it receives. Therefore, given the same values, it will always produce the same output.
92+
93+
#### 2. A pure function will not cause side effects. A side effect is any change in the system that is observable to the outside world.
94+
95+
`const calculateBill = (sumOfCart, tax) => sumOfCart * tax`
96+
97+
Is calculateBill pure? Definitely :) It exhibits the two necessary characteristics:
98+
99+
The function depends only on its arguments to produce a result
100+
The function does not cause any side effects
101+
The Mostly Adequate Guide states that side effects include, but are not limited to:
102+
103+
changing the file system
104+
inserting a record into a database
105+
making an http call
106+
mutations
107+
printing to the screen / logging
108+
obtaining user input
109+
querying the DOM
110+
accessing system state
111+
112+
#### Further Reading
113+
114+
[https://hackernoon.com/javascript-and-functional-programming-pt-3-pure-functions-d572bb52e21c](https://hackernoon.com/javascript-and-functional-programming-pt-3-pure-functions-d572bb52e21c)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Basic concepts are pure functions, side effects, data mutation and declarative over imperative.
2+
3+
On top of that currying, compose and function composition are important concepts.
4+
5+
Knowledge of libraries like RamdaJS provide useful utilities build applications in a more functional way. Functions being a first class citizen make it possible for JS to be really functional is important as well.

ONLY-Questions-Collections-Yet-to-draft-ans/whats-the-concept-of-functional-programming.md

-2
This file was deleted.

React/HOC.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
To put it simply, a higher-order component is a function, that takes a component and returns a new component. I like to think of them as parameterized components. Many times I find myself creating several components with very similar logic, with only 1 or 2 changes. Once I find a use case like this, it’s very simple to abstract the shared logic, and put the logic that changes into parameters.
1+
A higher-order component is a function, that takes a component and returns a new component. I like to think of them as parameterized components. Many times I find myself creating several components with very similar logic, with only 1 or 2 changes. Once I find a use case like this, it’s very simple to abstract the shared logic, and put the logic that changes into parameters.
22

33
Since components are just functions and HOCs are just functions that return other functions, we can use functional concepts to chain them using utilities methods such as compose, which is provided by many libraries (it's included in Redux!).
44

system-design/e-Commerce-site.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Design Strategy:
2+
3+
To build high scaling application with ~zero downtime ~high availability one of our strategy would be to use Microservice based approach. Microservices is an approach to application development where a large application is built as a suite of granular services. Each service supports a specific business goal and uses a simple, well-defined interface to communicate with other services.
4+
5+
#### Further Reading
6+
7+
[http://blog.gainlo.co/index.php/2016/08/28/design-ecommerce-website-part-ii/](http://blog.gainlo.co/index.php/2016/08/28/design-ecommerce-website-part-ii/)

0 commit comments

Comments
 (0)