File tree 1 file changed +9
-4
lines changed
src/main/scala/scala/collection/decorators
1 file changed +9
-4
lines changed Original file line number Diff line number Diff line change @@ -97,6 +97,7 @@ class IteratorDecorator[A](val `this`: Iterator[A]) extends AnyVal {
97
97
def splitBy [K ](f : A => K ): Iterator [immutable.Seq [A ]] =
98
98
new AbstractIterator [immutable.Seq [A ]] {
99
99
private var hd : A = _
100
+ private var hdKey : K = _
100
101
private var hdDefined : Boolean = false
101
102
102
103
override def hasNext : Boolean = hdDefined || `this`.hasNext
@@ -107,20 +108,24 @@ class IteratorDecorator[A](val `this`: Iterator[A]) extends AnyVal {
107
108
if (hdDefined) {
108
109
seq += hd
109
110
} else {
110
- hd = `this`.next()
111
+ val init = `this`.next()
112
+ hd = init
113
+ hdKey = f(init)
111
114
hdDefined = true
112
- seq += hd
115
+ seq += init
113
116
}
114
117
var hadSameKey = true
115
118
while (`this`.hasNext && hadSameKey) {
116
119
val el = `this`.next()
117
120
hdDefined = true
118
- if (f(el) == f(hd)) {
121
+ val key = f(el)
122
+ if (key == hdKey) {
119
123
seq += el
120
124
} else {
121
125
hadSameKey = false
126
+ hdKey = key
127
+ hd = el
122
128
}
123
- hd = el
124
129
}
125
130
if (hadSameKey) {
126
131
hdDefined = false
You can’t perform that action at this time.
0 commit comments