Skip to content

Commit f70474b

Browse files
committed
currying
1 parent 42503b6 commit f70474b

File tree

2 files changed

+78
-1
lines changed

2 files changed

+78
-1
lines changed

Assets/StructuralSharing.png

63.2 KB
Loading

FunctionalProgramming.md

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,4 +100,81 @@ One way of making a `for` loop more declarative is to use `forEach()` instead
100100

101101
```javascript
102102
[1,2,3].forEach(item => console.log(item)) // -> 1,2,3
103-
```
103+
```
104+
<br>
105+
106+
### <i>Functional programming teaches us be more declarative.</i>
107+
---
108+
<br>
109+
110+
## Immutability
111+
112+
In functional programming, immutability relates to not changing data or state but instead making copies of it and returning a new state every time.
113+
114+
Doesn't that just fill up our memory? That's where structural sharing comes in.
115+
116+
### Structural Sharing
117+
118+
![Structural Sharing](./Assets/StructuralSharing.png)
119+
120+
When a new copy of a datatype (object, array etc) is created instead of copying everything only the changes made to the state are copied. This saves memory.
121+
122+
---
123+
<br>
124+
125+
## Higher Order Functions
126+
127+
A higher order function is a function that does one of two things. It either (1) takes one or more functions as arguments, or (2) returns a function as a result, often called a callback.
128+
129+
<i>Example 1:</i>
130+
131+
```javascript
132+
const hof = () => () => 'hello';
133+
134+
hof() // -> Function () => 'hello'
135+
136+
hof()() // -> hello
137+
```
138+
139+
<i>Example 2:</i>
140+
141+
```javascript
142+
const hof = (fn) => fn('hello');
143+
144+
hof(function(x){ console.log(x) }) // -> hello
145+
```
146+
147+
---
148+
<br>
149+
150+
## Currying
151+
152+
Currying is when you break down a function that takes multiple arguments into a series of functions that each take only one argument.
153+
154+
<i>Example</i>
155+
156+
```javascript
157+
const multiply = (a,b) => a * b;
158+
```
159+
160+
Instead of this function taking two parameters, we're going to give the first function one parameter which returns another function, and that function will take another parameter which will multiply `a` with `b`. Because of closures we can access the `a` variable inside of the `b` function.
161+
162+
```javascript
163+
const curriedMultiply = (a) => (b) => a * b;
164+
165+
curriedMultiply(5)(4); // -> 20
166+
```
167+
168+
### Why is this useful?
169+
170+
You can now create additional utility functions ontop of this function. So you can see that in this example we're essentially saving the first function with a set argument passed into the parameter to a new variable. Then when we can use that variable as a new function which only takes an input for the second function.
171+
172+
```javascript
173+
const curriedMultiplyBy5 = curriedMultiply(5);
174+
175+
curriedMultiplyBy5(6) // -> 30
176+
curriedMultiplyBy5(10) // -> 50
177+
```
178+
179+
180+

0 commit comments

Comments
 (0)