Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def average(xs:GenTraversable[Int]):Int={
- type IntTuple = (Int,Int)
- def addIntTuples(x:IntTuple,y:IntTuple):IntTuple=(x._1+y._1,x._2+y._2)
- val (sum,len)=xs.map(x=>(x,1))
- .aggregate((0,0))(addIntTuples,addIntTuples)
- sum/len
- }
- def readableAverage(xs:GenTraversable[Int]):Int={
- type Sum = Int
- type Len = Int
- type SumLen = (Sum,Len)
- def plus(x:SumLen,y:SumLen):SumLen=(x._1+y._1,x._2+y._2)
- val (sum,len)=xs.map(x=>(x,1))
- .aggregate((0,0))(plus,plus)
- sum/len
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement