Skip to content

Instantly share code, notes, and snippets.

@gclaramunt
Last active December 18, 2015 02:19
Show Gist options
  • Save gclaramunt/5710280 to your computer and use it in GitHub Desktop.
Save gclaramunt/5710280 to your computer and use it in GitHub Desktop.
avg xs = sm/lng
where (sm,lng) = foldl avgr (0,0) xs
avgr (s,l) x = (x+s,1+l)
def avg(xs:List[Int]):Float={
val (sum,length)=xs.foldLeft((0,0))( { case ((s,l),x)=> (x+s,1+l) })
sum/length
}
@gclaramunt
Copy link
Author

Average of a list in one pass using tupling (banana split?)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment