23
loading...
This website collects cookies to deliver better user experience
const multiply = (a, b) => a * b;
for(let i = 0; i < 5; i++) {
console.log(i);
}
[1,2,3,4,5].forEach(i=>console.log(i))
const obj ={name:"Pranav"}
function clone(obj) { return {...obj} }
const logger = function (val) {
console.log(val);
};
function caller(fn, value) {
fn(value);// it returns function as return value
}
caller(logger,"hello");// caller taking logger function as param
// * Memory efficient
function heavyDuty(index) {
const bigArray = new Array(7000).fill("😃");//
console.log("every time new values assigned to bigArray....");
return bigArray[index];
}
function heavyDutyHelper() {
const bigArray = new Array(7000).fill("😃");
console.log("creating array at once...");
return function (index) {
return bigArray[index];
};
}
console.log(heavyDuty(10));
console.log(heavyDuty(10));
let getHeavyDuty = heavyDutyHelper();
console.log(getHeavyDuty(10));
console.log(getHeavyDuty(10));
// 2. Encapsulation
const makeNuclearButton = () => {
let timeWithoutDestruction = 0;
const launch = () => {
timeWithoutDestruction = -1;
return "🧨";
};
const totalPeaceTime = () => timeWithoutDestruction;
const passTime = () => {
timeWithoutDestruction++;
};
setInterval(passTime, 1000);
return {
totalPeaceTime,
//launch
};
};
const ohno = makeNuclearButton()
console.log(ohno.totalPeaceTime());// ohno.launch() this is encapsulated.
//Normal Function
const multiply = (a, b) => {
return a * b;
}
//Using curry
const curriedMultiply = (a) => (b) => a * b;
/**Using curriedMultiply function we can create multiple utility functions. For example */
const multiplyByTwo = curriedMultiply(2);
const multiplyByFive = curriedMultiply(5);
console.log(multiplyByTwo(3),multiplyByFive(3))
const multiply=(a,b,c)=>a*b*c;
const partialMultiplyBy5 = multiply.bind(null,5);
console.log(partialMultiplyBy5(2,10))
function addTo10(n) {
console.log('long time')
return n + 10;
}
let cache = {};
function memoizeAddTo10(n) {
if (n in cache) return cache[n];
cache[n] = addTo10(n);
return cache[n];
}
console.log(memoizeAddTo10(5))
console.log(memoizeAddTo10(5))
function memoizeAddTo10() {
let cache = {};
return function (n) {
if (n in cache) return cache[n]
cache[n] = addTo10(n);
return cache[n];
}
}
let memoize = memoizeAddTo10()
console.log(memoize(5))
console.log(memoize(6))
console.log(memoize(5))
const user = {
name: "pranav",
active: true,
cart: [],
purchases: []
}
const compose = (f, g) => (...args) => f(g(...args))
const purchase = purchaseItem(emptyCart, buyItem, applyTaxToItems, addItemToCart)(user, { name: "laptop", price: 200 });
function purchaseItem(...fns) { return fns.reduce(compose) }
function addItemToCart(user, item) {
const updatedCart = user.cart.concat(item)
return Object.assign({}, user, { cart: updatedCart })
}
function applyTaxToItems(user) {
const { cart } = user;
const taxRate = 1.3;
const updatedCart = cart.map(({ name, price }) => {
return { name, price: price * taxRate }
})
return Object.assign({}, user, { cart: updatedCart })
}
function buyItem(user) {
return Object.assign({}, user, { purchases: user.cart })
}
function emptyCart(user) {
return Object.assign({}, user, { cart: [] })
}