bat/tests/syntax-tests/highlighted/Dart/test.dart

134 lines
19 KiB
Dart
Raw Normal View History

2020-10-05 07:27:59 +02:00
/* array sorting alogorithm */
int partition(List list, int low, int high) {
 if (list == null || list.length == 0) return 0;
 int pivot = list[high];
 int i = low - 1;
2020-10-05 07:27:59 +02:00
 void swap(List list, int i, int j) {
 int temp = list[i];
2020-10-05 07:27:59 +02:00
 list[i] = list[j];
 list[j] = temp;
 }
 for (int j = low; j < high; j++) {
2020-10-05 07:27:59 +02:00
 if (list[j] <= pivot) {
 i++;
 swap(list, i, j);
2020-10-05 07:27:59 +02:00
 }
 swap(list, i + 1, high);
2020-10-05 07:27:59 +02:00
 return i + 1;
 }
}
void quickSort(List list, int low, int high) {
2020-10-05 07:27:59 +02:00
 if (low < high) {
 int pi = partition(list, low, high);
 quickSort(list, low, pi - 1);
 quickSort(list, pi + 1, high);
2020-10-05 07:27:59 +02:00
 }
}
void merge(List list, int leftIndex, int middleIndex, int rightIndex) {
 int leftSize = middleIndex - leftIndex + 1;
 int rightSize = rightIndex - middleIndex;
2020-10-05 07:27:59 +02:00
 List leftList = new List(leftSize);
 List rightList = new List(rightSize);
2020-10-05 07:27:59 +02:00
 for (int i = 0; i < leftSize; i++) leftList[i] = list[leftIndex + i];
 for (int j = 0; j < rightSize; j++) rightList[j] = list[middleIndex + j + 1];
2020-10-05 07:27:59 +02:00
 int i = 0, j = 0;
 int k = leftIndex;
2020-10-05 07:27:59 +02:00
 while (i < leftSize && j < rightSize) {
 if (leftList[i] <= rightList[j]) {
 list[k] = leftList[i];
 i++;
 } else {
 list[k] = rightList[j];
 j++;
 }
 k++;
 }
 while (i < leftSize) {
 list[k] = leftList[i];
 i++;
 k++;
 }
 while (j < rightSize) {
 list[k] = rightList[j];
 j++;
 k++;
 }
}
void mergeSort(List list, int leftIndex, int rightIndex) {
2020-10-05 07:27:59 +02:00
 if (leftIndex < rightIndex) {
 int middleIndex = (rightIndex + leftIndex) ~/ 2;
2020-10-05 07:27:59 +02:00
 mergeSort(list, leftIndex, middleIndex);
 mergeSort(list, middleIndex + 1, rightIndex);
2020-10-05 07:27:59 +02:00
 merge(list, leftIndex, middleIndex, rightIndex);
2020-10-05 07:27:59 +02:00
 }
}
/* variables */
var name = 'Voyager I';
var year = 1977;
var antennaDiameter = 3.7;
var flybyObjects = ['Jupiter', 'Saturn', 'Uranus', 'Neptune'];
2020-10-05 07:27:59 +02:00
var image = {
 'tags': ['saturn'],
 'url': '//path/to/saturn.jpg'
2020-10-05 07:27:59 +02:00
};
/*classes */
class Spacecraft {
 String name;
 DateTime launchDate;
 Spacecraft(this.name, this.launchDate) {}
2020-10-05 07:27:59 +02:00
 // Named constructor that forwards to the default one.
 Spacecraft.unlaunched(String name) : this(name, null);
2020-10-05 07:27:59 +02:00
 int get launchYear => launchDate?.year;
2020-10-05 07:27:59 +02:00
 void describe() {
 print('Spacecraft: $name');
2020-10-05 07:27:59 +02:00
 if (launchDate != null) {
 int years = DateTime.now().difference(launchDate).inDays ~/ 365;
 print('Launched: $launchYear ($years years ago)');
2020-10-05 07:27:59 +02:00
 } else {
 print('Unlaunched');
2020-10-05 07:27:59 +02:00
 }
 }
}
/* Mixins */
class PilotedCraft extends Spacecraft with Piloted {
 // ···
2020-10-05 07:27:59 +02:00
}
/* Interfaces and abstract classes */
class MockSpaceship implements Spacecraft {
 // ···
2020-10-05 07:27:59 +02:00
}
/* async */
Future<void> printWithDelay(String message) {
 return Future.delayed(const Duration(seconds: 2)).then((_) {
 print(message);
2020-10-05 07:27:59 +02:00
 });
}
Stream<String> report(Spacecraft craft, Iterable<String> objects) async* {
2020-10-05 07:27:59 +02:00
 for (var object in objects) {
 await Future.delayed(const Duration(seconds: 2));
 yield '${craft.name} flies by $object';
2020-10-05 07:27:59 +02:00
 }
}