Last active
September 9, 2015 17:06
-
-
Save Sfshaza/da80b1e7eed75db53ef3 to your computer and use it in GitHub Desktop.
streams/last_positive
This file contains hidden or 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
// 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