forked from scala/scala-java8-compat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAccumulates.scala
87 lines (76 loc) · 2.34 KB
/
Accumulates.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*
* Scala (https://www.scala-lang.org)
*
* Copyright EPFL and Lightbend, Inc. dba Akka
*
* Licensed under Apache License 2.0
* (http://www.apache.org/licenses/LICENSE-2.0).
*
* See the NOTICE file distributed with this work for
* additional information regarding copyright ownership.
*/
package scala.compat.java8.converterImpl
import scala.compat.java8.collectionImpl._
trait AccumulatesFromStepper[@specialized(Double, Int, Long) A, Acc <: AccumulatorLike[A, Acc]] {
def apply(stepper: Stepper[A]): Acc
}
final class CollectionCanAccumulate[A](private val underlying: TraversableOnce[A]) extends AnyVal {
def accumulate: Accumulator[A] = {
val a = new Accumulator[A]
underlying.foreach(a += _)
a
}
}
final class AccumulateDoubleCollection(private val underlying: TraversableOnce[Double]) extends AnyVal {
def accumulate: DoubleAccumulator = {
val da = new DoubleAccumulator
underlying.foreach(da += _)
da
}
}
final class AccumulateIntCollection(private val underlying: TraversableOnce[Int]) extends AnyVal {
def accumulate: IntAccumulator = {
val da = new IntAccumulator
underlying.foreach(da += _)
da
}
}
final class AccumulateLongCollection(private val underlying: TraversableOnce[Long]) extends AnyVal {
def accumulate: LongAccumulator = {
val da = new LongAccumulator
underlying.foreach(da += _)
da
}
}
final class AccumulateAnyArray[A](private val underlying: Array[A]) extends AnyVal {
def accumulate: Accumulator[A] = {
val a = new Accumulator[A]
var i = 0
while (i < underlying.length) { a += underlying(i); i += 1 }
a
}
}
final class AccumulateDoubleArray(private val underlying: Array[Double]) extends AnyVal {
def accumulate: DoubleAccumulator = {
val da = new DoubleAccumulator
var i = 0
while (i < underlying.length) { da += underlying(i); i += 1 }
da
}
}
final class AccumulateIntArray(private val underlying: Array[Int]) extends AnyVal {
def accumulate: IntAccumulator = {
val da = new IntAccumulator
var i = 0
while (i < underlying.length) { da += underlying(i); i += 1 }
da
}
}
final class AccumulateLongArray(private val underlying: Array[Long]) extends AnyVal {
def accumulate: LongAccumulator = {
val da = new LongAccumulator
var i = 0
while (i < underlying.length) { da += underlying(i); i += 1 }
da
}
}