#gdgny #flydart – – 26 Feb, 2014



  #gdgny #flydart – – 26 Feb, 2014

0 1


flydart

Slides for Dart Flight School (GDG New York Feb 2014)

On Github nitya / flydart

  #gdgny #flydart

Nitya Narasimhan | Damon Douglas

26 Feb, 2014

Many thanks to ...

Alaric Calmette, Benoît Ruiz, Kévin Langles, Kevin Yot, Victor Hiairrassary.

for their awesome jobs

kudos guys

Agenda

  welcome()
    .then((_) => introduceDart()) // 30 minutes
    .then((_) => codeLab(part1))  // 1 hour
    .then((_) => eatPizzas())     // 30 minutes
    .then((_) => codeLab(part2))  // 1 hour
    .then((_) => takeSurvey());   // 5 minutes

						

WHAT IS DART ?

  • Web programming language
  • Both client and server platform
  • Open source
  • Vibrant community
  • Developed by Google

WHY DART ?

'JavaScript is the most popular language on the client side'

But it's a pain !

 DART's GOALS

  • Improved productivity

  • Increased performance

FAMILIAR

Learn in under an hour

COMPILE TO JAVASCRIPT

runs across the modern web

OPTIMIZED JS: TREE SHAKING

RUN DART ON THE SERVER

with Dart VM

 BATTERIES INCLUDED

  • SDK Libraries and third party packages : 670+
  • Package manager (pub)
  • Tools (Dart Editor, Dartium, dart2js)
  • Plugins to Eclipse, IntelliJ, ...

NO CONTEXT SWITCHING

Run    Debug

Iterate quickly

SIMPLE SYNTAX

Ceremony free !

TYPES

Types are optional

  var name = 'Robert Nelson';
						

Type shows an intent for what a value category should be

NUMBERS

  var m = 3;
  num n = 9;
  int x = 33;
  double y = 2.548;
						
  • num : int or double
  • int : integer (53 bits because they compile to JS)
  • double : double (64 bits)

BOOLEAN

  bool selected = false;
							
  var displayed = true;
						
  • bool is 'true' or 'false' (nothing else)
  • Only 'true' is truthy

LIST

  List names = ['Anne', 'Robert', 'Louise'];
							
  var towns = [];

  List countries = new List();
  countries.add('France');

  String name = name.length > 1 ? name[1] : "unknown";
						

List is a sequence of values

MAP

  Map names = {
    'Dart' : 'http://www.dartlang.org',
    'Html5Rock' : 'http://www.html5rocks.com' };
							
  Map dartFS = new Map();
  dartFS['Montpellier'] = 'http://www.jug-montpellier.org';
  dartFS['Paris'] = 'http://www.parisjug.org';

  String url = dartFS['Montpellier'];
						

Map is a dictionary of pairs

CLASSES

Classes are first citizen : everything is a class !

  • Classes
  • Types (int, double, num, bool, ...)
  • Functions

=> Default instance value is always null

CLASSES

class Hug {
    int strength; // Public attribute 
    // --- Modify online to use a getter ---

    Hug(this.strength); // Terse

    Hug operator +(Hug other) { // Operator overriding
      return new Hug(strength + other.strength);
    }
  }	

  Hug hug = new Hug(10);
  int v = hug.strength;	

CLASSES

  class Hug {
    ...
    // Optional params w/ default value
    void patBack({int hands: 1}) { ... }

    // Named params w/ default value
    void sayGoodBye([int times= 1]) { ... }

    // One line function & String interpolation
    String toString() => "Embraceometer reads $_strength"; 
  }						

CONSTRUCTORS

  class Hug {
    static Hug _shared = new Hug(99);
	
    // Default constructor (no name)
    Hug(this._strength);

    // Named constructor
    Hug.withFriend(int strength) : this(strength * 2);

    // Factory : new Hug.shared() return the same instance
    factory Hug.shared() => _shared;
  }						

INHERITANCE / IMPLEMENTATION

  • Use 'extends' keyword to extend another class
  • Use 'implements' keyword to implement an interface
  • Every class is also an interface
  • So you can implement a class

MIXIN

class Employee extends Persistable { ... }

But Employee is NOT a Persistable

MIXIN

class Employee extends Person with Persistable { ... }

Employee has Persistable capabilities

METHOD CASCADES

  List dfSchools = new List();
  dfSchools.add('San Francisco');
  dfSchools.add('Montpellier');
  dfSchools.add('Paris');
					

Yikes ! 'dfSchools' is repeated 4 times, so ...

  List dfSchools = new List() ..add('San Francisco') 
                              ..add('Montpellier')
                              ..add('Paris');
					

FUNCTIONS / ITERABLE / CLOSURE

FUNCTIONS

  String addSign(String msg) {
    return '$msg (from Montpellier JUG)';
  }
					

or short form

  addSign(msg) => '$msg (from Montpellier JUG)';
					

FUNCTIONS AS PARAM

addSign(msg) => '$msg (from Montpellier JUG)';						

String sayHi(String to, String msg, Function signFct) {
  return 'Hello $to, ${signFct(msg)}';
}

main() {
  print(sayHi('Dartisans', 'Dart Flight School is awesome',addSign));
}
					

LOOPS

  for(int i = 0; i < list.length; i++) {
    print(list[i]);
  } 
					

or short form

  list.forEach((v) => print(v));
					

or shorter form

  list.forEach(print);
					

MAP

List<int> result = [];
List<String> values = ['19','02','2014','19','00'];
for(int i = 0; i < values.length; i++) {
  result.add(int.parse(values[i]));
}					

or short form

['19','02','2014','19','00'].map((String s) {
    return int.parse(s);
  });					

or shorter form

  ['19','02','2014','19','00'].map((s) => int.parse(s));
					

REDUCE

  new List.generate(4, (i) => i).reduce((int v, int e) {
    return v * e;
  });
					

or short form

  new List.generate(4, (i) => i).reduce((v,e) => v+e);
					

WHERE

  for(int i = 0; i < list.length; i++) {
    if (list[i] % 2 == 0) {
      print('$i is even');
    }
  } 
					

or short form

  list.where((v) => v.isEven).forEach(print);
					

EVERY

  bool allEven = true;
    for(int i = 0; i < list.length; i++) {
      if (list[i] % 2 != 0) {
        allEvent = false;
        break;
      }
    } 
  if (allEvent) print('Great!');
					

or short form

  if (list.every((v) => v.isEven)) print('Great!');					
					

ANY

  for(int i = 0; i < list.length; i++) {
    if (list[i] % 2 == 0) {
      print('Great!');
      break;
    }
  } 
  if (allEvent) print('Great!');
					

or short form

  if (list.any((v) => v.isEven)) print('Great!');					
					

ITERABLE

  Iterable l1 = [1,2,3,4,5].where((x) => x.isEven);
  Iterable l2 = l1.map((x) => x*2);
  Iterable l3 = l2.map((x) => x+1);
					

or short form

  [1,2,3,4,5].where((x) => x.isEven)
             .map((x) => x*2)
             .map((x) => x+1);
					

Quiz: how many times, items are accessed?

LAZY ITERABLE

where, expand and map return lazy iterable

=> Call toList, reduce, forEach, ... to iterate over items

CLOSURE

makeAdder(int addBy) {
  adder(int a) {
    return a + addBy;
  }
  return adder;
}					

shorter form

makeAdder(int addBy) => (int a) => a + addBy;

Use it

var add100 = makeAdder(100);
var result = add100(53); // 153

CLOSURE

  DateTime today = new DateTime.now();
  employees.where((e) => e.name.startsWith('T'))
           .where((e) => e.birthday == today); 
					

This code is not reusable!

  nameStartWith(start) => (e) => e.name.startsWith(start);
  birthdayIs(when) => (e) => e.birthday == when;

  employees.where(nameStartWith('T'))
           .where(birthdayIs(new DateTime.now())); 
					

This one is better

CLOSURE

  nameStartWith(start) => (e) => e.name.startsWith(start);
  birthdayIs(when) => (e) => e.birthday == when;

  and(List filters) => (e) => filters.every((f) => f(e));
  or(List filters) => (e) => filters.any((f) => f(e));

  employees.where(
       and([ birthdayIs(new DateTime.now()),
	     or([ nameStartWith('T'), namesStartWith('A')]) ])
  ); 
					

I love <3

Questions

Project sources and slides are available: https://github.com/eric-taix/movie-board

CODELAB: Movie-Board

LET'S TAKE OFF...