Skip to content

Instantly share code, notes, and snippets.

@tafsiri
Created June 4, 2011 04:02
Show Gist options
  • Select an option

  • Save tafsiri/1007562 to your computer and use it in GitHub Desktop.

Select an option

Save tafsiri/1007562 to your computer and use it in GitHub Desktop.
val words = List("this", "is", "a", "list", "of", "strings")
//foldLeft function (curried)
var size = words.foldLeft(0)((sum, word) => sum + 1)
var charCount = words.foldLeft(0)((sum, word) => sum + word.length())
println(size)
println(charCount)
//foldleft operator
//I definitely prefer the curried function form, why do we need curly braces around this code
//block? Other function take code blocks without curly braces. Tate text doesn't explain
//what these variations arise from, which irks me somewhat.
charCount = (0 /: words) {(sum, word) => sum + word.length()}
println(charCount)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment