x | f(x) |
---|---|
0 | 3 |
1 | 5 |
2 | 11 |
function parabola(x) {
return 2 * Math.pow(x, 2) + 3
}
/* Tests */
expect(parabola(0)).toEqual(3); // Always true
expect(parabola(1)).toEqual(5); // Always true
expect(parabola(2)).toEqual(11); // Always true
let count = 0;
function increaseCount(val) {
count += val;
return count;
}
increaseCount(1); // 1
increaseCount(1); // 2
count = 5;
increaseCount(1); // 6
Math.random(); // => 0.4011148700956255
Math.random(); // => 0.8533405303023756
Math.random(); // => 0.3550692005082965
const time = () => new Date().toLocaleTimeString();
time(); // => "5:15:45 PM"
function add(a, b) {
return a + b;
}
function mult(a, b) {
return a * b;
}
var result = add(2, mult(3, 4)); // 14
// or
var result = add(2, 12); // 14
(f o g)(x) or f(g(x))
function toggleModal(isOpen: boolean) {
setModalOpen(isOpen);
}
function openModal() {
toggleModal(true);
}
function closeModal() {
toggleModal(false);
}
(f o g)(x) or f(g(x))
Right to Left
Write code as an association of the functions rather than a series of imperative steps
$ wc -l * | sort -n | head -n 3
$ wc -l * | sort -n | head -n 3
Word Count → Sort Asc → Take top 3
Left to Right
const users = [
{ firstName: 'Jane', lastName: 'Doe' },
{ firstName: 'John', lastName: 'Doe' }
]
function getUserDisplayName(user) {
return `${user.firstName} ${user.lastName}`;
}
users.map(user => getUserDisplayName(user)); // ["Jane Doe", "John Doe"]
const users = [
{ firstName: 'Jane', lastName: 'Doe' },
{ firstName: 'John', lastName: 'Doe' }
]
function getUserDisplayName(user) {
return `${user.firstName} ${user.lastName}`;
}
users.map(getUserDisplayName); // ["Jane Doe", "John Doe"]
Point refers to the function argument
👎
const multiply = ( a, b ) => a * b;
const mul = curry(multiply);
mul(2)(3);
👍
var multiply = (a, b) => a * b;
var double = partial(multiply, [2]);
double(2); //=> 4
var multiply = (a, b, c) => a * b * c;
var mul = partial(multiply, [2, 3]);
mul(2); //=> 12
$ npm install ramda @types/ramda -S
// or
$ yarn add ramda @types/ramda
import * as R from 'ramda';
// or
import { curry, compose, pipe, partial } from 'ramda';
// or
const R = require('ramda');
R.curry(...)
R.compose(...)
R.pipe(...)
1. It checks if the change event resulted in a check or uncheck of the checkbox.
2. Converts it to a boolean.
3. Gets the equivalent “todo” status value from the checked value.
4. Calls the onChange function to emit output to the parent React component.
Pros
Cons