Skip to content

Commit

Permalink
use math library
Browse files Browse the repository at this point in the history
  • Loading branch information
sethladd committed Aug 24, 2012
1 parent 1a6f7ac commit 5d1d837
Show file tree
Hide file tree
Showing 16 changed files with 3,300 additions and 1,978 deletions.
3 changes: 2 additions & 1 deletion AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@

Google Inc.

Matthew Butler <butler.matthew@gmail.com>
Matthew Butler <butler.matthew@gmail.com>
Chris Buckett <chrisbuckett@gmail.com>
8 changes: 6 additions & 2 deletions src/site/_includes/language-tour/classes/index.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -366,12 +366,14 @@ The distanceTo() method in the following sample is an
example of an instance method.

{% highlight dart %}
#import('dart:math');

class Point {
num x, y;
Point(this.x, this.y);

num distanceTo(Point other) {
return Math.sqrt(((x-other.x)*(x-other.x)) + ((y-other.y)*(y-other.y)));
return sqrt(((x-other.x)*(x-other.x)) + ((y-other.y)*(y-other.y)));
}
}
{% endhighlight %}
Expand Down Expand Up @@ -569,12 +571,14 @@ Static methods (class methods) do not operate on an instance, and thus
do not have access to `this`.

{% highlight dart %}
#import('dart:math');

class Point {
num x, y;
Point(this.x, this.y);

static num distanceBetween(Point a, Point b) {
return Math.sqrt(((a.x-b.x)*(a.x-b.x)) + ((a.y-b.y)*(a.y-b.y)));
return sqrt(((a.x-b.x)*(a.x-b.x)) + ((a.y-b.y)*(a.y-b.y)));
}
}

Expand Down
29 changes: 20 additions & 9 deletions src/site/articles/idiomatic-dart/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -66,23 +66,29 @@ <h3>Named constructors</h3>
<p>Like most dynamically-typed languages, Dart doesn't support overloading. With methods, this isn't much of a limitation because you can always use a different name, but constructors aren't so lucky. To alleviate that, Dart lets you define <em>named constructors</em>:</p>

{% highlight dart %}
#import('dart:math');

class Point {
num x, y;
Point(this.x, this.y);
Point.zero() : x = 0, y = 0;
Point.polar(num theta, num radius) {
x = Math.cos(theta) * radius;
y = Math.sin(theta) * radius;
x = cos(theta) * radius;
y = sin(theta) * radius;
}
}
{% endhighlight dart 0 %}

<p>Here our Point class has three constructors, a normal one and two named ones. You can use them like so:</p>

{% highlight dart %}
var a = new Point(1, 2);
var b = new Point.zero();
var c = new Point.polar(Math.PI, 4.0);
#import('dart:math');

main() {
var a = new Point(1, 2);
var b = new Point.zero();
var c = new Point.polar(PI, 4.0);
}
{% endhighlight dart 0 %}

<p>Note that we're still using <code>new</code> here when we invoke the named constructor. It isn't just a static method.</p>
Expand Down Expand Up @@ -255,9 +261,11 @@ <h2 id="top-level-definitions">Top-level definitions</h2>
<p>Dart is a "pure" object-oriented language in that everything you can place in a variable is a real object (no mutant "primitives") and every object is an instance of some class. It's not a <em>dogmatic</em> OOP language though. You aren't required to place everything you define inside some class. Instead, you are free to define functions, variables, and even getters and setters at the top level if you want.</p>

{% highlight dart %}
#import('dart:math');

num abs(num value) => value < 0 ? -value : value;

final TWO_PI = Math.PI * 2.0;
final TWO_PI = PI * 2.0;

int get today() {
final date = new Date.now();
Expand All @@ -267,7 +275,7 @@ <h2 id="top-level-definitions">Top-level definitions</h2>

<p>Even in languages that don't require you to place everything inside a class or object, like JavaScript, it's still common to do so as a form of namespacing: top-level definitions with the same name could inadvertently collide. To address that, Dart has a library system that allows you to import definitions from other libraries with a prefix applied to disambiguate it. That means you shouldn't <em>need</em> to defensively squirrel your definitions inside classes.</p>

<p>We're still exploring what this actually means for how we define libraries. Most of our code does place definitions inside classes, like Math. It's hard to tell if this is just an ingrained habit we have from other languages or a practice that's also good for Dart. This is an area we want feedback on.</p>
<p>We're still exploring what this actually means for how we define libraries. For example, the project used to have a Math class, but we moved all functionality from that class to top-level methods inside the dart:math library.</p>

<p>We do have some examples where we use top-level definitions. The first you'll run into is <code>main()</code> which is expected to be defined at the top level. If you work with the DOM, the familiar <code>document</code> and <code>window</code> "variables" are actually top-level getters in Dart.</p>

Expand Down Expand Up @@ -304,8 +312,11 @@ <h2 id="strings-and-interpolation">Strings and interpolation</h2>
placing them inside curly braces:</p>

{% highlight dart %}
var r = 2;
print('The area of a circle with radius $r is ${Math.PI * r * r}');
#import('dart:math');
main() {
var r = 2;
print('The area of a circle with radius $r is ${PI * r * r}');
}
{% endhighlight dart 0 %}

<h2 id="operators">Operators</h2>
Expand Down
2 changes: 1 addition & 1 deletion src/site/articles/mocking-with-dart/index.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ themselves can be `expect()`-style `Matcher`s, for example:
{% highlight dart %}
m.when(callsTo('sqrt', isNegative)).
alwaysThrow('No imaginary number support');
m.when(callsTo('sqrt', isNonNegative)).alwaysCall((x) => Math.sqrt(x));
m.when(callsTo('sqrt', isNonNegative)).alwaysCall((x) => sqrt(x));
{% endhighlight %}

You don't need to provide argument matchers in `callsTo()` for all arguments of a method, but you do need
Expand Down
5 changes: 4 additions & 1 deletion src/site/articles/puzzlers/chapter-2.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -677,9 +677,12 @@ So the mysterious behavior is explained.
What happens when we do a naive translation to Dart?

{% highlight dart %}
#import('dart:math');

main() {
StringBuffer word = null;
switch ((Math.random() * 2).toInt()) {
var random = new Random();
switch ((random.nextDouble() * 2).toInt()) {
case 1: word = new StringBuffer('P');
case 2: word = new StringBuffer('G');
default: word = new StringBuffer('M');
Expand Down
4 changes: 3 additions & 1 deletion src/site/articles/style-guide/index.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -612,8 +612,10 @@ together.

<div class="good">
{% highlight dart good %}
#import('dart:math');
// ...
/* Rolls both [Dice] and returns the highest rolled value. */
num greatestRoll(Dice a, Dice b) => Math.max(a.roll(), b.roll());
num greatestRoll(Dice a, Dice b) => max(a.roll(), b.roll());
{% endhighlight %}
</div>

Expand Down
10 changes: 5 additions & 5 deletions src/site/docs/editor/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ <h3>Using autocomplete</h3>
<p>
Type a class, interface, or variable name, and then type a period.
<br />
For example, type <code>document.</code> or <code>Math.</code>
For example, type <code>document.</code>
and pause a moment.
Once the suggestions appear,
continue typing to pare down the list.
Expand All @@ -348,9 +348,9 @@ <h3>Using autocomplete</h3>
<p>
Type <b>Ctl+Space</b>.
<br />
For example, type <code>Mat</code>, then Ctl+Space
For example, type <code>Str</code>, then Ctl+Space
to see a list of classes and interfaces
that start with "Mat".
that start with "Str".
</p>
</li>
</ul>
Expand Down Expand Up @@ -410,9 +410,9 @@ <h4>Finding where an API is declared</h4>

<p>
The editor displays the file that declares the item.
For example, if you Command-click Math,
For example, if you Command-click String,
the file that declares the
<a href="http://api.dartlang.org/dart_core/Math.html"><code>Math</code></a> type appears.
<code>String</code> type appears.
</p>
</li>
</ol>
Expand Down
Loading

0 comments on commit 5d1d837

Please sign in to comment.