Unlike permutations & variations, picking more elements can lead to having fewer combinations.
Ex.
Going on a picnic and have six pieces of fruit you want to take with you. However, your basket only has a room for 4 of them. Using combinations formula:
C₆ ₄ = 15 possible choices
You go and buy a bigger basket, but only fits 5
C₆ ₅ = 6 possible choices
even bigger basket, fits 6
C₆ ₆ 1 possible choice
picking more elements leads to having fewer combinations
Instead of picking which pieces of fruit to take, we can choose the pieces to leave behind.
** Picking 4 fruits out of 6, is the same as choosing 2 fruits that will be left out
C₆ ₂ = 6! / 2! * 4! = 15
** Picking 5 fruits out of 6, is the same as choosing 1 furit that will be left out
C₆ ₁ = 6! / 1! 5! = 6
We can pick p-many elements in as many ways as we can pick n minus p many elements.
Cₙ ₚ = Cₙ ₙ₋ₚ
// symmetric with respect to no over 2
Recall
Select 3 out of 10 employees to represent the company
C₁₀ ₃ = 120
What if instead of choosing 3 we had to pick 7 people?
C₁₀ ₇ = 10! / 7! * 3! = 8 * 9 * 10 / 1 * 2 * 3 = 720 / 6
Thus we would also have 120 different ways of picking the 7 employees
** Picking 7 out of 10 employees to take to the conference is the same with choosing 3 out 10 to leave behind.
When
p > n / 2 > n - p
// Apply symmetry to avoid calculating factorials of large numbers
// We use symmetry to simplify calculations.
First class passengers in a airline receives a different service compared to that of other passengers.
Just like that, there is a first-class citizen in Javascript which is the function.
3 Characteristics
a. Assigning a function into a variable
const square = function(num) {
return num * num;
};
output square(7);
console.log(output); // --> 49
// Unlike function declaration, function expression does not encounter hoisting.
"hoisting" allows the function to work wherever the declaration is made.
// Relying on function declaration and hoisting is not good for a good maintenace of codes.
A callback function is a function used as a argument in another function(caller function).
Callback function is named that way because it is invoked when a task is done.
A caller fucntion can invoke a callback function multiple times or choose to not invoke at all.
A curry function is a function that returns a function.
If a curry function is used we say that a higher-order function is a function that uses another function as an argument in a caller function.
But, a curry function is also a part of higher-order function.
b. Using a callback fucntion in a caller function.
function double(num) {
return num * 2;
}
function dobuleNum(func,num) {
return func(num);
}
/*
** The function doubleNum is a higher-order function that received another function as a parameter/argument.
** If the func parameter in the doubleNum is a function, func is a callback function of doubleNum.
** In the example below, double is a callback function of doubleNum.
let output = doubleNum(double, 4);
console.log(output); // --> 8;
c. Returning a function
function adder(added) {
return function(num) {
return num + added;
};
}
//*
** The adder function is a higher-order function that returns another function.
** The adder function receives one parameter and returns a unknown function.
** The returned unknown function receives one parameter and adds it with added.
** adder(5) is a function therefore you can use the retrieve equation of a function '()'
let output = adder(5)(3); // --> 8
console.log(output); // --> 8
//*
** You can assign a variable to the function adder is returning.
** This is because function is a first-class citizen in Javascript.
const add3 = adder(3);
output = add3(2);
console.log(output); // --> 5
d. Receiving a function as a parameter and returning a function.
function double(num) {
return num * 2;
}
function doubleAdder(added,func) {
const doubled = func(added);
return function(num) {
return num + doubled;
};
}
/*
** doubleAdder function is a higher-order function
** The parameter func in doubleAdder is a callback function.
** The double function is sent to doubleAdder's callback function.
** doubleAdder(5, double) is a function therefore we w=can use the function retrieve equation '()'
doubleAdder(5, double)(3); // --> 13
** doubleAdder returned function can be assigned as a variable.
const addTwice3 = doubleAdder(3, double);
addTwice3(2); // --> 8