@@ -35,44 +35,26 @@ class IterableDecorator[C, I <: IsIterable[C]](coll: C)(implicit val it: I) {
35
35
36
36
37
37
/**
38
- * Constructs an iterator where consecutive elements are accumulated as
38
+ * Constructs a collection where consecutive elements are accumulated as
39
39
* long as the output of f for each element doesn't change.
40
40
* <pre>
41
41
* Vector(1,2,2,3,3,3,2,2)
42
42
* .splitBy(identity)
43
43
* </pre>
44
44
* produces
45
45
* <pre>
46
- * Iterator (Vector(1),
46
+ * Vector (Vector(1),
47
47
* Vector(2,2),
48
48
* Vector(3,3,3),
49
49
* Vector(2,2))
50
50
* </pre>
51
51
*
52
52
* @param f the function to compute a key for an element
53
53
* @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
56
56
*/
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)))
77
59
}
78
60
}
0 commit comments