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