If you want everything to be familiar you will never learn anything new.
- Rich Hickey (Author of Clojure)
const arr = [1, 2, 3, 4, 5];
const doubler = (arr) => {
for (let i=0; i<arr.length; i++) {
arr[i] = arr[i] * 2;
}
return arr;
};
const doubleArr = doubler(arr);
console.log(doubleArr); // [2, 4, 6, 8, 10]
class Foo {
constructor(arr) {
this._arr = arr;
}
getSum() {
let sum = 0;
for (let i=0; i<this._arr.length; i++) {
sum += this._arr[i];
}
return sum;
}
}
const arr = [1, 2, 3, 4, 5];
const foo = new Foo(arr);
transformer.getSum(); // 15
SELECT EmployeeID, FirstName, LastName
FROM Employees
WHERE City = 'London'
INSERT INTO student (id, name, age)
VALUES (‘1’, ‘alan’, 28);
DELETE FROM student
WHERE name = ‘alan’;
Functional Programming is so called because a program consists entirely of functions.
- John Hughes , Why Functional Programming Matters
No matter what language you work in, programming in a functional style provides benefits. You should do it whenever it is convenient, and you should think hard about the decision when it isn't convenient.
- John Carmack, ID Software
// unfunctional function:
a = 0
const increment = () => {
return ++a;
};
increment();
increment();
increment();
// unfunctional function:
a = 0
const increment = () => {
return ++a;
};
increment(); // 1
increment(); // 2
increment(); // 3
// functional function:
const num = 0;
const increment = (a) => {
return a + 1;
};
increment(num);
increment(num);
increment(num);
// functional function:
const num = 0;
const increment = (a) => {
return a + 1;
};
increment(num); // 1
increment(num); // 1
increment(num); // 1
const arr = [1, 2, 3, 4, 5];
const doubler = (element) => {
return element * 2;
};
const doubleArr = arr.map(doubler);
console.log(doubleArr); // [2, 4, 6, 8, 10]
const arr = [1, 2, 3, 4, 5];
const summarizer = (previous, current) => {
return previous + current;
};
const sum = arr.reduce(summarizer);
console.log(sum); // 15
Docs: Reduce [MDN]
Elm (pure FP language purpose-built for front-end web development)
http://elm-lang.org/
Please fill out this feedback form:
https://goo.gl/forms/Hsn6oonjyIl1yxCm1