blob: 58063faf5d171a9d6cfa249c9cdb36ff670c246e (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
package scalam
import scala.collection.SeqLike
import scala.collection.generic.CanBuildFrom
package object math {
def smooth[Elem, Coll[Elem]](width: Int, passes: Int, collection: Coll[Elem])(implicit fractional: Fractional[Elem], cbf: CanBuildFrom[Coll[Elem], Elem, Coll[Elem]], view: Coll[Elem] => Seq[Elem]): Coll[Elem] = {
def average(xs: Seq[Elem]): Elem = {
import fractional._
xs.sum / fromInt(xs.length)
}
def smoothOnce(width: Int, collection: Coll[Elem]) = {
val b = cbf(collection)
collection.sliding(width).foreach(neighbours => b += average(neighbours))
b.result()
}
if (passes <= 0) collection
else smooth(width, passes - 1, smoothOnce(width, collection))
}
}
|