Skip to content

Instantly share code, notes, and snippets.

@msbaek
Created June 5, 2015 05:50
Show Gist options
  • Save msbaek/c6ac06b5af704b117b33 to your computer and use it in GitHub Desktop.
Save msbaek/c6ac06b5af704b117b33 to your computer and use it in GitHub Desktop.
foldLeft
/**
* foldLeft
*/
List(1, 2, 3, 4, 5).foldLeft(0) { (totalSofar, current) =>
totalSofar + current
}
List(1, 2, 3, 4, 5).foldLeft(List[Int]())((b, a) => a :: b)
val rr : Range = 1 to 10
rr.foldLeft(0)(_ + _) // sum
rr.foldLeft(1)(_ * _) // product
rr.foldLeft(0)((c, _) => c + 1) // count
rr.foldLeft(0)(_ + _) / rr.foldLeft(0)((c, _) => c + 1) // average
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment