Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1268)

Unified Diff: packages/quiver_iterables/lib/src/partition.dart

Issue 2119523002: Added full js & js_util packages (Closed) Base URL: git@github.com:dart-lang/observatory_pub_packages.git@master
Patch Set: Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « packages/quiver_iterables/lib/src/min_max.dart ('k') | packages/quiver_iterables/lib/src/range.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: packages/quiver_iterables/lib/src/partition.dart
diff --git a/packages/quiver_iterables/lib/src/partition.dart b/packages/quiver_iterables/lib/src/partition.dart
new file mode 100644
index 0000000000000000000000000000000000000000..626c58783d3c90fa3a44c03630e87ea717370212
--- /dev/null
+++ b/packages/quiver_iterables/lib/src/partition.dart
@@ -0,0 +1,44 @@
+// Copyright 2014 Google Inc. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+part of quiver.iterables;
+
+/// Partitions the input iterable into lists of the specified size.
+Iterable<List> partition(Iterable iterable, int size) {
+ if (size <= 0) throw new ArgumentError(size);
+ return _partition(iterable, size);
+}
+
+Iterable<List> _partition(Iterable iterable, int size) sync* {
+ if (iterable.isEmpty) return;
+ var iterator = iterable.iterator;
+ iterator.moveNext();
+ var first = iterator.current;
+ while (true) {
+ var part = [first];
+ for (var i = 0; i < size - 1; i++) {
+ if (!iterator.moveNext()) {
+ yield part;
+ return;
+ }
+ part.add(iterator.current);
+ }
+ yield part;
+ if (iterator.moveNext()) {
+ first = iterator.current;
+ } else {
+ return;
+ }
+ }
+}
« no previous file with comments | « packages/quiver_iterables/lib/src/min_max.dart ('k') | packages/quiver_iterables/lib/src/range.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698