Slide 40
Slide 40 text
Functional programming in Dart
● final, const keywords
● Top-level functions
● Anonymus functions
● Extensions
extension ExtensionList on List {
// get the last item of the list
T get lastItem => this[length - 1];
// get a reversed copy of the list
List get reversedCopy => this.reversed.toList();
// repeat the list n times
List repeat(int times) => [for(int i = 0; i < times; i++) ...this];
}
void main() {
var list = [1, 2, 3, 4, 5];
print(list.lastItem); // 5
print(list.reversedCopy); // [5, 4, 3, 2, 1]
print(list.repeat(2)); // [1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2]
}