Last active
August 29, 2015 14:17
-
-
Save aakashns/4599e5db4b28e02f79b9 to your computer and use it in GitHub Desktop.
sum, sumNonEmpty and sumDifference
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
| // 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) { _ + _ } |
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 sumDifference(elems1: Seq[Int], elems2: Seq[Int]): Int = | |
| sum(elems1) - sum(elems2) |
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 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