Advertisement
Guest User

typedef readability

a guest
Mar 23rd, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 0.57 KB | None | 0 0
  1. def average(xs:GenTraversable[Int]):Int={
  2.         type IntTuple = (Int,Int)
  3.         def addIntTuples(x:IntTuple,y:IntTuple):IntTuple=(x._1+y._1,x._2+y._2)
  4.         val (sum,len)=xs.map(x=>(x,1))
  5.             .aggregate((0,0))(addIntTuples,addIntTuples)
  6.         sum/len
  7.     }
  8.  
  9. def readableAverage(xs:GenTraversable[Int]):Int={
  10.         type Sum = Int
  11.         type Len = Int
  12.         type SumLen = (Sum,Len)
  13.         def plus(x:SumLen,y:SumLen):SumLen=(x._1+y._1,x._2+y._2)
  14.         val (sum,len)=xs.map(x=>(x,1))
  15.             .aggregate((0,0))(plus,plus)
  16.         sum/len
  17.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement