@@ -321,8 +321,6 @@ object IterableOnce {
321321trait IterableOnceOps [+ A , + CC [_], + C ] extends Any { this : IterableOnce [A ]^ =>
322322 // ///////////////////////////////////////////////////////////// Abstract methods that must be implemented
323323
324- import IterableOnceOps .Maximized
325-
326324 /** Produces a $coll containing cumulative results of applying the
327325 * operator going left to right, including the initial value.
328326 *
@@ -1052,6 +1050,29 @@ trait IterableOnceOps[+A, +CC[_], +C] extends Any { this: IterableOnce[A]^ =>
10521050 case _ => foldLeft(new Maximized [A , B ](" maxBy" )(f)(ord.gt))((m, a) => m(m, a)).result
10531051 }
10541052
1053+ private class Maximized [X , B ](descriptor : String )(f : X -> B )(cmp : (B , B ) -> Boolean ) extends AbstractFunction2 [Maximized [X , B ], X , Maximized [X , B ]] {
1054+ var maxElem : X = null .asInstanceOf [X ]
1055+ var maxF : B = null .asInstanceOf [B ]
1056+ var nonEmpty = false
1057+ def toOption : Option [X ] = if (nonEmpty) Some (maxElem) else None
1058+ def result : X = if (nonEmpty) maxElem else throw new UnsupportedOperationException (s " empty. $descriptor" )
1059+ def apply (m : Maximized [X , B ], a : X ): Maximized [X , B ] =
1060+ if (m.nonEmpty) {
1061+ val fa = f(a)
1062+ if (cmp(fa, maxF)) {
1063+ maxF = fa
1064+ maxElem = a
1065+ }
1066+ m
1067+ }
1068+ else {
1069+ m.nonEmpty = true
1070+ m.maxElem = a
1071+ m.maxF = f(a)
1072+ m
1073+ }
1074+ }
1075+
10551076 /** Finds the first element which yields the largest value measured by function f.
10561077 *
10571078 * $willNotTerminateInf
@@ -1335,31 +1356,3 @@ trait IterableOnceOps[+A, +CC[_], +C] extends Any { this: IterableOnce[A]^ =>
13351356 xs
13361357 }
13371358}
1338-
1339- object IterableOnceOps :
1340-
1341- // Moved out of trait IterableOnceOps to here, since universal traits cannot
1342- // have nested classes in Scala 3
1343- private class Maximized [X , B ](descriptor : String )(f : X -> B )(cmp : (B , B ) -> Boolean ) extends AbstractFunction2 [Maximized [X , B ], X , Maximized [X , B ]] {
1344- var maxElem : X = null .asInstanceOf [X ]
1345- var maxF : B = null .asInstanceOf [B ]
1346- var nonEmpty = false
1347- def toOption : Option [X ] = if (nonEmpty) Some (maxElem) else None
1348- def result : X = if (nonEmpty) maxElem else throw new UnsupportedOperationException (s " empty. $descriptor" )
1349- def apply (m : Maximized [X , B ], a : X ): Maximized [X , B ] =
1350- if (m.nonEmpty) {
1351- val fa = f(a)
1352- if (cmp(fa, maxF)) {
1353- maxF = fa
1354- maxElem = a
1355- }
1356- m
1357- }
1358- else {
1359- m.nonEmpty = true
1360- m.maxElem = a
1361- m.maxF = f(a)
1362- m
1363- }
1364- }
1365- end IterableOnceOps
0 commit comments