Monday, October 28, 2019

Tail-Recursion, Tail-Call Optimization

https://stackoverflow.com/questions/33923/what-is-tail-recursion
https://stackoverflow.com/questions/310974/what-is-tail-call-optimization

Examples done after reading the answers in the 1st link above:

// 
//////////// Sum till n ////////////
//

// - normal recursion
function sumTo(n) {
  return (n == 0 ? 0 : (n+sumTo(n-1)));
}
var result = sumTo(10);
console.log(result);

// - tail recursion
function sumTo2(n) {
  return sumTo2_aux(n, 0);
}
function sumTo2_aux(n, acc) {
  return (n == 0) ? acc : sumTo2(n-1, acc+n);
}
var result2 = sumTo2(10);
console.log(result2);


//
//////////// Factorial ////////////
//

// - normal recursion
function factorial(n) {
  return (n == 1 ? 1 : n * factorial(n-1));
}
var factorial_result1 = factorial(10);
console.log(factorial_result1);

// - tail recursion
function factorial2(n) {
  return factorial2_aux(n,1);
}
function factorial2_aux(n, acc){
  return (n == 1 ? acc : n * factorial2_aux(n-1, acc));
}
var factorial_result2 = factorial2(10);
console.log(factorial_result2);

//

Thursday, October 24, 2019

Firebase related

Data Structures related

Uninstall Mongo old version and Install new version

1) backup data

mkdir mongodb-dump-oct24-2019
cd mongodb-dump-oct24-2019
mongodump

Above will create a folder called 'dump' within 'mongodb-dump-oct24-2019' folder.

2) Uninstall existing version of mongodb
https://medium.com/@rajanmaharjan/uninstall-mongodb-macos-completely-d2a6d6c163f9

3) Install new version
https://stackoverflow.com/questions/57856809/installing-mongodb-with-homebrew

brew tap mongodb/brew  
brew install mongodb-community@4.0
4) brew link --force mongodb-community@4.0
brew services start mongodb-community@4.0
5) Restore data
cd mongodb-dump-oct24-2019
mongorestore

Followers

Blog Archive