Home > Net >  Optional positional parameter in Dart
Optional positional parameter in Dart

Time:07-20

I'm studying recursion and I wrote this method to calculate the N° number of the Fibonacci series:

fibonacci(int n, Map memo) {

  if (memo.containsKey(n)) return memo[n];  // Memo check
  if (n <= 2) return 1;  // base case
  // calculation
  memo[n] = fibonacci(n - 1, memo)   fibonacci((n - 2), memo);
  return memo[n];
}

I think it doesn't need to be explained, my problem is just how to call this function from the main, avoiding providing an empty Map.

this is how I call the function now:

fibonacci(n, {});

But I would rather prefer to call it just like this:

fibonacci(n);

CodePudding user response:

This is the solution I've found so far but looks very verbose and inelegant:

fibonacci(int n, [Map<int, int>? memo]) { 
memo == null ? memo = {} : null; // null check 
if (memo.containsKey(n)) return memo[n]; 
if (n <= 2) return 1; 
memo[n] = fibonacci(n - 1, memo)   fibonacci((n - 2), memo); 
return memo[n]; 
} 

In this way I can call just:

fibonacci(n);

CodePudding user response:

There are multiple ways to do it. This is my personal favorite, because it also limits the function that is only used for internal means and it doesn't have the need to check every recursion, as you already know there is a map provided:

int fibonacci(int n) {
  return _fibonacci(n, {});
}

int _fibonacci(int n, Map<int, int> memo) {
  if (n <= 2) return 1;  // base case
  
  final previouslyCalculated = memo[n];  // Memo check
  
  if(previouslyCalculated != null) {
    return previouslyCalculated;
  }
  
  // calculation
  final next = _fibonacci(n - 1, memo)   _fibonacci((n - 2), memo);
  
  memo[n] = next;
  
  return next;
}

void main() {
  print(fibonacci(4));
}

As Dart does not support overloading, if you actually need both versions to be publicly available (or want both private) you would have to pick different names.

Please note that I added proper types to your methods and cleaned them up a bit for everything that would not compile once proper types are used. Make sure you always use proper types and don't rely on dynamic to somehow works it's magic. The compiler can only help you, if you are explicit about what you want to do. Otherwise they can only nod and let you run into any mistake you may have made. Be smart, let your compiler help, it will catch a lot of errors for you at compile time that you would otherwise have to spent countless hours on debugging.

  • Related