Permalink
Browse files

Sum of Pairs: Scala solutions by Julian Exenberger

  • Loading branch information...
1 parent 5f9587d commit a1bdb58ff63c8e210a0e11dbd6ca15e8b215a017 Andreas Pauley committed Sep 18, 2009
Showing with 25 additions and 0 deletions.
  1. +9 −0 CodeSnippets/SumPairs/sumpairs-flatmap.scala
  2. +16 −0 CodeSnippets/SumPairs/sumpairs-zipfold.scala
@@ -0,0 +1,9 @@
+// Scala solution by Julian Exenberger
+// http://dotneverland.blogspot.com/2009/09/quick-sample-of-lists-and-tuples-in.html
+
+val numbers = List(1,2,3,4,5);
+numbers.zip(numbers.tail).flatMap {
+ value => {
+ List(value._1+value._2);
+ }
+};
@@ -0,0 +1,16 @@
+// Scala solution by Julian Exenberger
+// http://dotneverland.blogspot.com/2009/09/quick-sample-of-lists-and-tuples-in.html
+
+object SumPairs extends Application {
+
+val numbers = List(1,2,3,4,5);
+val result = numbers.zip(numbers.tail)
+ .foldLeft(List[Int]()) (
+ (list, y) => {
+ y._1 + y._2 :: list
+ }
+ )
+ .reverse;
+println(result);
+
+}

0 comments on commit a1bdb58

Please sign in to comment.