Skip to content

Instantly share code, notes, and snippets.

@aakashns
Last active August 29, 2015 14:17
Show Gist options
  • Select an option

  • Save aakashns/4599e5db4b28e02f79b9 to your computer and use it in GitHub Desktop.

Select an option

Save aakashns/4599e5db4b28e02f79b9 to your computer and use it in GitHub Desktop.
sum, sumNonEmpty and sumDifference
// xs.foldLeft(z)(op) reduces to
// (..(((z op x(0)) op x(1)) op x(2)) ... op x(n))
def sum(elems : Seq[Int]): Int =
elems.foldLeft(0) { _ + _ }
def sumDifference(elems1: Seq[Int], elems2: Seq[Int]): Int =
sum(elems1) - sum(elems2)
def sumNonEmpty(elems: Seq[Int]): Int =
if (elems.isEmpty) None
else Some(sum(elems))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment