Skip to content

Instantly share code, notes, and snippets.

@Sfshaza
Last active September 9, 2015 17:06
Show Gist options
  • Save Sfshaza/da80b1e7eed75db53ef3 to your computer and use it in GitHub Desktop.
Save Sfshaza/da80b1e7eed75db53ef3 to your computer and use it in GitHub Desktop.
streams/last_positive
// Copyright (c) 2015, the Dart project authors.
// Please see the AUTHORS file for details.
// All rights reserved. Use of this source code is governed
// by a BSD-style license that can be found in the LICENSE file.
import 'dart:async';
Future<int> lastPositive(Stream<int> stream) async {
var lastValue = null;
await for (var value in stream) {
if (value < 0) continue;
lastValue = value;
}
return lastValue;
}
main() async {
var data = [1, -2, 3, -4, 5, -6, 7, -8, 9, -10];
var stream = new Stream.fromIterable(data);
var lastPos = await lastPositive(stream);
print(lastPos); // 9
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment