-
Notifications
You must be signed in to change notification settings - Fork 675
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Tutorial(streams): update for Dart 2 and analayze/test code
- Loading branch information
Showing
7 changed files
with
302 additions
and
128 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import 'dart:async'; | ||
import 'dart:convert'; | ||
import 'dart:io'; | ||
|
||
Future<void> main(List<String> args) async { | ||
var file = new File(args[0]); | ||
var lines = file | ||
.openRead() | ||
.transform(utf8.decoder) //!<br> | ||
.transform(const LineSplitter()); | ||
await for (var line in lines) { | ||
if (!line.startsWith('#')) print(line); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// ignore_for_file: annotate_overrides, type_annotate_public_apis | ||
import 'dart:async'; | ||
|
||
// TODO(chalin): I believe that handleError's test argument should be declared | ||
// as follows, but it isn't: {bool Function(dynamic error) test}. | ||
|
||
abstract class MyStream<T> implements Stream<T> { | ||
// #docregion main-stream-members | ||
Future<T> get first; | ||
Future<bool> get isEmpty; | ||
Future<T> get last; | ||
Future<int> get length; | ||
Future<T> get single; | ||
Future<bool> any(bool Function(T element) test); | ||
Stream<E> asyncExpand<E>(Stream<E> Function(T event) convert); | ||
Stream<E> asyncMap<E>(FutureOr<E> Function(T event) convert); | ||
Stream<R> cast<R>(); | ||
Future<bool> contains(Object needle); | ||
Stream<T> distinct([bool Function(T previous, T next) equals]); | ||
Future<E> drain<E>([E futureValue]); | ||
Future<T> elementAt(int index); | ||
Future<bool> every(bool Function(T element) test); | ||
Stream<S> expand<S>(Iterable<S> Function(T element) convert); | ||
Future<T> firstWhere(bool Function(T element) test, {T Function() orElse}); | ||
Future<S> fold<S>(S initialValue, S Function(S previous, T element) combine); | ||
Future forEach(void Function(T element) action); | ||
Future<String> join([String separator = ""]); | ||
Future<T> lastWhere(bool Function(T element) test, {T Function() orElse}); | ||
Stream<S> map<S>(S Function(T event) convert); | ||
Future pipe(StreamConsumer<T> streamConsumer); | ||
Future<T> reduce(T Function(T previous, T element) combine); | ||
Stream<R> retype<R>(); | ||
Future<T> singleWhere(bool Function(T element) test, {T Function() orElse}); | ||
Stream<T> skip(int count); | ||
Stream<T> skipWhile(bool Function(T element) test); | ||
Stream<T> take(int count); | ||
Stream<T> takeWhile(bool Function(T element) test); | ||
Future<List<T>> toList(); | ||
Future<Set<T>> toSet(); | ||
Stream<T> where(bool Function(T event) test); | ||
// #enddocregion main-stream-members | ||
|
||
bool get isBroadcast; | ||
|
||
Stream<T> asBroadcastStream( | ||
{void Function(StreamSubscription<T> subscription) onListen, | ||
void Function(StreamSubscription<T> subscription) onCancel}); | ||
|
||
// #docregion special-stream-members | ||
Stream<T> handleError(Function onError, {bool test(error)}); | ||
Stream<T> timeout(Duration timeLimit, | ||
{void Function(EventSink<T> sink) onTimeout}); | ||
Stream<S> transform<S>(StreamTransformer<T, S> streamTransformer); | ||
// #enddocregion special-stream-members | ||
|
||
// #docregion listen | ||
StreamSubscription<T> listen(void Function(T event) onData, | ||
{Function onError, void Function() onDone, bool cancelOnError}); | ||
// #enddocregion listen | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// ignore_for_file: type_annotate_public_apis | ||
// #docregion | ||
import 'dart:async'; | ||
|
||
// #docregion sumStream | ||
Future<int> sumStream(Stream<int> stream) async { | ||
var sum = 0; | ||
await for (var value in stream) { | ||
sum += value; | ||
} | ||
return sum; | ||
} | ||
// #enddocregion sumStream | ||
|
||
Stream<int> countStream(int to) async* { | ||
for (int i = 1; i <= to; i++) { | ||
yield i; | ||
} | ||
} | ||
|
||
main() async { | ||
var stream = countStream(10); | ||
var sum = await sumStream(stream); | ||
print(sum); // 55 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// ignore_for_file: type_annotate_public_apis | ||
// #docregion | ||
import 'dart:async'; | ||
|
||
Future<int> sumStream(Stream<int> stream) async { | ||
var sum = 0; | ||
try { | ||
await for (var value in stream) { | ||
sum += value; | ||
} | ||
} catch (e) { | ||
return -1; | ||
} | ||
return sum; | ||
} | ||
|
||
Stream<int> countStream(int to) async* { | ||
for (int i = 1; i <= to; i++) { | ||
if (i == 4) { | ||
throw new Exception('Intentional exception'); | ||
} else { | ||
yield i; | ||
} | ||
} | ||
} | ||
|
||
main() async { | ||
var stream = countStream(10); | ||
var sum = await sumStream(stream); | ||
print(sum); // -1 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// ignore_for_file: type_annotate_public_apis | ||
import 'package:test/test.dart'; | ||
import 'package:examples/tutorial/sum_stream.dart' as sum_stream; | ||
import 'package:examples/tutorial/sum_stream_with_catch.dart' | ||
as sum_stream_with_catch; | ||
|
||
void main() { | ||
test('sumStream', () => expect(sum_stream.main, prints('55\n'))); | ||
test('sumStream with catch', | ||
() => expect(sum_stream_with_catch.main, prints('-1\n'))); | ||
} |
Oops, something went wrong.