Last active
December 18, 2015 02:19
-
-
Save gclaramunt/5710280 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
avg xs = sm/lng | |
where (sm,lng) = foldl avgr (0,0) xs | |
avgr (s,l) x = (x+s,1+l) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def avg(xs:List[Int]):Float={ | |
val (sum,length)=xs.foldLeft((0,0))( { case ((s,l),x)=> (x+s,1+l) }) | |
sum/length | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Average of a list in one pass using tupling (banana split?)