Skip to content

Commit b3180ab

Browse files
committed
fixup! adds splitBy extension method on scala.collection.Iterable
1 parent b732778 commit b3180ab

File tree

1 file changed

+6
-24
lines changed

1 file changed

+6
-24
lines changed

src/main/scala/scala/collection/decorators/IterableDecorator.scala

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -35,44 +35,26 @@ class IterableDecorator[C, I <: IsIterable[C]](coll: C)(implicit val it: I) {
3535

3636

3737
/**
38-
* Constructs an iterator where consecutive elements are accumulated as
38+
* Constructs a collection where consecutive elements are accumulated as
3939
* long as the output of f for each element doesn't change.
4040
* <pre>
4141
* Vector(1,2,2,3,3,3,2,2)
4242
* .splitBy(identity)
4343
* </pre>
4444
* produces
4545
* <pre>
46-
* Iterator(Vector(1),
46+
* Vector(Vector(1),
4747
* Vector(2,2),
4848
* Vector(3,3,3),
4949
* Vector(2,2))
5050
* </pre>
5151
*
5252
* @param f the function to compute a key for an element
5353
* @tparam K the type of the computed key
54-
* @return an iterator of sequences of the consecutive elements with the
55-
* same key in the original iterator
54+
* @return a collection of collections of the consecutive elements with the
55+
* same key in the original collection
5656
*/
57-
def splitBy[K, That, CC](f: it.A => K)(implicit bf: BuildFrom[C, it.A, That], bff: BuildFrom[C, That, CC]): CC = {
58-
val builder = bff.newBuilder(coll)
59-
60-
val iterator = it(coll).iterator
61-
var init = bf.newBuilder(coll)
62-
if (iterator.hasNext) {
63-
var ref = iterator.next();
64-
init += ref
65-
while (iterator.hasNext) {
66-
val el = iterator.next();
67-
if (f(el) != f(ref)) {
68-
builder += init.result()
69-
init = bf.newBuilder(coll)
70-
ref = el
71-
}
72-
init += el
73-
}
74-
builder += init.result()
75-
}
76-
builder.result();
57+
def splitBy[K, CC1, CC2](f: it.A => K)(implicit bf: BuildFrom[C, it.A, CC1], bff: BuildFrom[C, CC1, CC2]): CC2 = {
58+
bff.fromSpecific(coll)(it(coll).iterator.splitBy(f).map(bf.fromSpecific(coll)))
7759
}
7860
}

0 commit comments

Comments
 (0)