fib(n) => n < 2 ? n : (fib(n - 1) + fib(n - 2));
Examples on the main page just exists to show cases as much language features as possible. It does not say that you have to wrap your Fibonacci into a class.
fib(n) => n < 2 ? n : (fib(n - 1) + fib(n - 2));
Examples on the main page just exists to show cases as much language features as possible. It does not say that you have to wrap your Fibonacci into a class.
+ the laziest way to do memoization:
var cache = new Map();
fib (n) => cache.putIfAbsent(n, () => n < 2 ? n : fib(n - 1) + fib(n - 2));
Documentation that showcases features in the terse form is available at http://synonym.dartlang.org/
If you have better ideas you can file a bug or even submit a pull request for the site: https://github.com/dart-lang/dartlang.org
I also would love to propose a self-imposed internet-wide ban on the recursive Fibonacci implementation as I'm pretty sure it causes brain rot and makes dynamic programming seem way more magical than it is.