Dart: functions

I wanted to give you some short snippets about functions in dart. They can really help you. The first thing is how you can write a method in one line:

sayHello() => "Hello";

This is helpful for dummy stuff and so on. But now something more special. This parameters in square brackets is known as optional positional parameter. An example:

String saySomething(String thingToSay, [String name]){
 if(name != null){
 return "$thingToSay $name";
 } else {
 return thingToSay;
 }
 }

You then can call this method in 2 ways.

  1. print(saySomething(“I really like pasta”));
  2. print(saySomething(“I really like”, “Dart”));

But when you want to write more readable code, then try the optional named parameter.  For this you can use curly brackets. You can give a default value too. An example:

screamAndShout(String scream, {int times: 1}){
 for(int i = 0; i<times;i++){
 print(scream.toUpperCase());
 }
 }

The usage looks like this:

  1. screamAndShout(“i love dart”, times : 4);
  2. screamAndShout(“i love dart”);

I think all of this things can really help.

Dart: Math Constants

Another small feature of the dart math library are math constants. Just import the following line:

import 'dart:math' as math;

This are all the math constants the library knows:

E (Base of the natural logarithms)

print(math.E);

Result: 2.718281828459045

PI

print(math.PI);

Result: 3.141592653589793

SQRT2 (Square root of 2)

print(math.SQRT2);

Result: 1.4142135623730951

LN10 (Natural logarithm of 10)

print(math.LN10);

Result: 2.302585092994046

LN2 (Natural logarithm of 2)

print(math.LN2);

Result: 0.6931471805599453

LOG10E (Base -10 logarithm of [E])

print(math.LOG10E);

Result: 0.4342944819032518

LOG2E (Base -2 logarithm of [E])

print(math.LOG2E);

Result: 1.4426950408889634

SQRT1_2 (Square root of 0.5)

print(math.SQRT1_2);

Result: 0.7071067811865476

Dart: Trigonometry

Here are some examples for this topic.

import 'dart:math' as math;

void main() {
var beta = 40;
var radians = beta * (math.PI / 180);
print("beta = $beta°: $radians");

var sinBeta = double.parse(math.sin(radians).toStringAsPrecision(3));
var cosBeta = double.parse(math.cos(radians).toStringAsPrecision(3));
var tanBeta = double.parse(math.tan(radians).toStringAsPrecision(3));

print("sinus : $sinBeta");
print("cosinus : $cosBeta");
print("tangens : $tanBeta");
}

Its very simple, and there is something real cool about Dart that we see here:

toStringAsPrecision(3)

This automatically rounds the number up or down. So the number -1.117214930923896 gets -1.12. I think this really makes many things easier when you work with this kind of math things. Another nice thing that you can see here is the following print-method:

print("beta = $beta°");

No need to use tokens like ‘+’ or ‘.’. Just use ‘$’. By the way, the output looked like that:

beta = 40°: 0.6981317007977318
sinus : 0.643
cosinus : 0.766
tangens : 0.839

Google ‘maps’ the Grand Canyon

Just search for it at Google Maps =).

Screen Shot 2013-02-07 at 21.21.31

The official announcement of Google.