Skip to content

Commit 45916cc

Browse files
author
Chris Myers
committed
Merge branch 'master' into answers
Conflicts: src/main/scala/com/rea/higherorder/FoldingExercises.scala
2 parents 85db25a + 3c6513a commit 45916cc

File tree

1 file changed

+61
-5
lines changed

1 file changed

+61
-5
lines changed

src/main/scala/com/rea/higherorder/FoldingExercises.scala

Lines changed: 61 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,27 @@ object FoldingExercises {
1111
* and the next value in the list (of type A) and returns a value which will be feed back into
1212
* the accumulator of the next call.
1313
*
14+
* As the name suggests it processes the list from left to right.
15+
*
16+
* Have a close look at your implementations from the RecursionExercises. Which parts could you
17+
* pull out to a function to make them all common? Your implementation will be very close to
18+
* foldLeft.
19+
*
20+
* Good luck!
21+
*
1422
*/
1523
def foldLeft[A, B](initialValue: B, list: List[A])(f: (B, A) => B): B = {
1624
@tailrec
1725
def fl(acc: B, l: List[A]): B = l match {
1826
case Nil => acc
1927
case h :: t => fl(f(acc, h), t)
2028
}
21-
2229
fl(initialValue, list)
2330
}
2431

32+
/**
33+
* foldRight is the same as foldLeft, except it processes the list from right to left.
34+
*/
2535
def foldRight[A, B](initialValue: B, list: List[A])(f: (A, B) => B): B = {
2636
def fr(l: List[A], acc: B): B = l match {
2737
case Nil => acc
@@ -30,11 +40,57 @@ object FoldingExercises {
3040
fr(list, initialValue)
3141
}
3242

43+
44+
/**
45+
* Remember these, from our recursion exercises? They can all be implemented with either
46+
* foldLeft or foldRight.
47+
*/
48+
49+
def sum(l: List[Int]): Int = ???
50+
51+
def length[A](x: List[A]): Int = ???
52+
53+
def map[A, B](x: List[A])(f: A => B): List[B] = ???
54+
55+
def filter[A](x: List[A], f: A => Boolean): List[A] = ???
56+
57+
def append[A](x: List[A], y: List[A]): List[A] = ???
58+
59+
def flatten[A](x: List[List[A]]): List[A] = ???
60+
61+
def flatMap[A, B](x: List[A], f: A => List[B]): List[B] = ???
62+
63+
// Maximum of the empty list is 0
64+
def maximum(x: List[Int]): Int = ???
65+
66+
def reverse[A](x: List[A]): List[A] = ???
67+
68+
3369
def main(args: Array[String]) = {
34-
assert(foldLeft(0, List(1, 2, 3))(_ + _) == 6)
35-
assert(foldLeft(List[Int](), List(1, 2, 3))((a, e) => e :: a) == List(3, 2, 1))
36-
assert(foldRight(List[Int](), List(1, 2, 3))((e, a) => e :: a) == List(1, 2, 3))
37-
assert(foldRight(0, List(1, 2, 3))(_ + _) == 6)
70+
assert(foldLeft(0, List(1,2,3))(_+_) == 6)
71+
assert(foldLeft(List[Int](), List(1,2,3))((a,e) =>e :: a) == List(3,2,1))
72+
assert(foldRight(List[Int](), List(1,2,3))((e,a) =>e :: a) == List(1,2,3))
73+
assert(foldRight(0, List(1,2,3))(_+_) == 6)
74+
75+
println("Sum of List(1,2,3,4,5,6) = 21: " + sum(List(1, 2, 3, 4, 5, 6)))
76+
println("Length of List(1,2,3,4,5,6) = 6: " + length(List(1, 2, 3, 4, 5, 6)))
77+
78+
println("Add one to List(1,2,3,4,5,6) = List(2,3,4,5,6,7): " + map(List(1, 2, 3, 4, 5, 6))(_+1))
79+
80+
println("Remove elements under 4 for List(1,2,3,4,5,6) = List(4,5,6): " + filter(List(1, 2, 3, 4, 5, 6), {
81+
x: Int => x >= 4
82+
}))
83+
84+
println("Append List(a,b,c) with List(d,e,f) = List(a,b,c,d,e,f): " + append(List('a', 'b', 'c'), List('d', 'e', 'f')))
85+
86+
println("Flatten a List(List(a,b,c),List(e,f,g), List(h,i,j)) = List(a,b,c,d,e,f,g,h,i,j): " + flatten(
87+
List(List('a', 'b', 'c'), List('d', 'e', 'f'), List('h', 'i', 'j'))))
88+
89+
println("Run a flatMap over List(hello, world) with function split = List(h,e,l,l,o,w,o,r,l,d): " + flatMap(List("hello", "world"), (_: String).toCharArray.toList))
90+
91+
println("maxium of List(4,3,5,7,1,2,6,3,4,5,6) = 7: " + maximum(List(4, 3, 5, 7, 1, 2, 6, 3, 4, 5, 6)))
92+
93+
println("Reverse a List(1,2,3,4,5,6) = List(6,5,4,3,2,1) : " + reverse(List(1,2,3,4,5,6)))
3894
}
3995

4096
}

0 commit comments

Comments
 (0)