-
Notifications
You must be signed in to change notification settings - Fork 0
/
Functions-Functional_Programming.js
104 lines (63 loc) · 4.1 KB
/
Functions-Functional_Programming.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
------------------------------------------------------------------------
Functional_Programming
------------------------------------------------------------------------
Functional Programming is what we call a programming paradigm.
You can think of a programming paradigm as a specific style of programming.
Some languages make it easier to code in certain paradigms than others.
Some other programming paradigms that you've encountered include imperative programming and object oriented programming.
Imperative Programming: (in order commands that a program performs)(step by step)
*/
var numberArray = [3,6,9], sum = 0; //<---top down, step by step and
for (var i=0; i< numberArray.length; i++) { // immediately performing the commands.
sum += numberArray[i];
}
console.log("sum is , sum");
/* <---side effect. something happens out the
environment outside the program.
Note: C is an example of a program that primarily uses an imperative programming style.
Object Oriented Programming (Java primarily takes advantage of this style)
Objects are used to descibe "Objects" in real life. The object holds the state of, as an example, a person.
*/
function Student(name){
this.name=name;
this.grades = [];
this.sumOfGrades = 0;
this.addGrade = function(grade){
this.grades.push(grade);
};
this.getGradeAverage(){
return this.sumOfGrades/this.grades.length;
}
}
var student = new Student("Vince");
student.addGrade(80);
student.addGrade(76);
student.addGrade(90);
student.getGradeAverage(); //returns: 82
/*
Functional programming is characterized by pure, higher-order functions and immutable data.
When using functional programming, programs are designed using functions as the primary building blocks
instead of using objects to hold state. Functional programming also emphasizes modularity.
For example, you could write a function that loops over an array and prints out each element.
Then you write another function that loops over an array and adds the elements to some variable.
There is now repetition in your code. Both functions loop over an array and perform some action.
We can separate these two purposes into separate functions--
- a looping function that takes some function as an argument,
- and a function that can be passed into the looping function.
We'll call our looping function forEach, and the function we pass in is called a callback.
*/
//------------------------------------------IMPORTANT-------------------------------------------------------------
//This is not the Array method .forEach. This is a self created method that is equivalent to the forEach method.
//----------------------------------------------------------------------------------------------------------------
function forEach(arr, callback) {
for(var i = 0; i < arr.length; i++) {
callback(arr[i]);
}
}
var arr = [1,2,3,4,5]
forEach(arr, function() {console.log})
//Now we can call forEach with different functions passed in.
//Now our code is more DRY (Don't Repeat Yourself) and more readable.
------------------------------------------------------------------------
*/