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.
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.
+ the laziest way to do memoization:
var cache = new Map();
fib (n) => cache.putIfAbsent(n, () => n < 2 ? n : fib(n - 1) + fib(n - 2));
Here you're invalidating all responses and making them sound non-sensical. Post a new message if you have something new to say.
The person attacking me for 'changing' my post has been on News.YC (ahem 'Hacker News') for 70 days, called criticism of the boilerplate code shown in the Dart demos 'FUD', and wrote three separate replies to the same point. Judge for yourself.
All of your posts attacking Dart are for nonsensical reasons, and then your replies are backsteps admitting part of what you originally said was wrong and then coming up with a new nonsensical argument. I don't know what you have against Dart, but your basic premise that it's verbose is absolutely wrong. They've actually taken pains in the syntax to not be verbose (factory constructors, => functions, getters and setters, .. syntax for cascades, etc). It just shows you judge the language based on glancing at the front page of the website for 2 seconds without doing significant reading. In other words your posts are FUD. I stand by that.
Yes, if the user is lecturing others on HN etiquette.
> All of your posts attacking Dart are for nonsensical reasons
If you think terse syntax and good quality documentation are nonsense then you are welcome to think so, I wish you the best of luck.
Cool. Send a pull request to the dartlang.org maintainers and they might fix it. ;)
I have no interest in or against Dart other than wanting to see a compelling modern alternative to JS. Not sure why you think criticising Dart and it's website is 'spreading FUD'.
Yes, that's my point. Why not just print("Hello World!"), like everything else?
if (itsTuesday) {
class Foo {
...
}
}
It also means mutual recursion just works, without weird semantic shenanigans like hoisting in JavaScript. In Dart, the top-level is "simultaneous": all of those definitions are static with no mixing of side-effecting imperative code at the top-level which would make ordering matter.The main side-effect of this approach is that you have to wrap your entrypoint imperative code in a main() function. It's a pretty small price to pay, especially in an application with thousands of lines of code.
In JavaScript it depends on the order of script tags and their attributes. It's really hard to determine what code is actually run at application startup, and what the initial state is.
Making the top-level only declarations means that the programmer knows what code runs first (the first line in main()) and the VM knows that the order of evaluating the declarations doesn't matter. It can evaluate them all and cache the result so that subsequent runs are fast, it can execute main() before other declarations and evaluate them on an as-needed basis, or something in between.
OT: script tag soup is, for first party stuff anyway, avoided with a single script for a module loader these days.
This is not really possible if you must interpret top level code to build up your top-level declarations, as done in JS.
Using snapshots, the Dart authors have said it should be possible to improve script start up time by 10x over V8. This is a big win for large script heavy apps.
Edit: link https://www.dartlang.org/articles/snapshots/
Also, if you import a library that contains this top-level imperative code, how do you invoke it? Is it invoked automatically? When? How does that play with cyclic imports?
The small amount of overhead of a main() method solves all of that.