From 52ef366042340de9b8530472b267c5197cf05b7f Mon Sep 17 00:00:00 2001 From: Andreas Roehler Date: Thu, 4 Apr 2024 20:27:54 +0200 Subject: [PATCH 1/3] solution2.2.6.3 provided Signed-off-by: Andreas Roehler --- chapter02/worksheets/solution2.2.6.3.scala | 25 ++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 chapter02/worksheets/solution2.2.6.3.scala diff --git a/chapter02/worksheets/solution2.2.6.3.scala b/chapter02/worksheets/solution2.2.6.3.scala new file mode 100644 index 0000000..0c61523 --- /dev/null +++ b/chapter02/worksheets/solution2.2.6.3.scala @@ -0,0 +1,25 @@ +/** + Exercise 2.2.6.3 + Use foldLeft to implement the zipWithIndex method for sequences. + + The required type signature and a sample test: + + def zipWithIndex[A](xs: Seq[A]): Seq[(A, Int)] = ??? + + scala> zipWithIndex(Seq("a", "b", "c", "d")) + res0: Seq[(String, Int)] = List((a, 0), (b, 1), (c, 2), (d, 3)) + */ + +def zipWithIndex[A](xs: Seq[A]): Seq[(A, Int)] = { + val a = (0 to xs.length - 1) + type Acc = Seq[(A, Int)] + var counter = (-1) + def init: Acc = Seq.empty + xs.foldLeft(init) { (x, y) => (counter = counter + 1); (x :+ (y, counter)) } +} + +// scala> :load solution2.2.6.3.scala +// :load solution2.2.6.3.scala +// def zipWithIndex[A](xs: Seq[A]): Seq[(A, Int)] +// val result: Seq[(String, Int)] = List((a,0), (b,1), (c,2), (d,3)) +// val expected: Seq[(String, Int)] = List((a,0), (b,1), (c,2), (d,3)) From ad3b8715eb623e44ada3b866c1c198d17c554ed8 Mon Sep 17 00:00:00 2001 From: Andreas Roehler Date: Sun, 14 Apr 2024 20:31:01 +0200 Subject: [PATCH 2/3] #61, Solution2.2.6.4.scala provided --- chapter02/worksheets/solution2.2.6.4.scala | 26 ++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 chapter02/worksheets/solution2.2.6.4.scala diff --git a/chapter02/worksheets/solution2.2.6.4.scala b/chapter02/worksheets/solution2.2.6.4.scala new file mode 100644 index 0000000..2e3ac43 --- /dev/null +++ b/chapter02/worksheets/solution2.2.6.4.scala @@ -0,0 +1,26 @@ +/** + Exercise 2.2.6.4 + Use foldLeft to implement a function filterMap that combines map and + filter for sequences. The predicate is applied to the elements of + the initial sequence, and values that pass the predicate are mapped. + + The required type signature and a sample test: + + def filterMap[A, B](xs: Seq[A])(pred: A => Boolean)(f: A => B): Seq[B] = ??? + + scala> filterMap(Seq(1, 2, 3, 4)) { x => x > 2 } { x => x * 10 } + res0: Seq[Int] = List(30, 40) + */ + +def filterMap[A, B](xs: Seq[A])(pred: A => Boolean)(f: A => B): Seq[B] = { + xs.filter(pred).map(f) +} + +val result = filterMap(Seq(1, 2, 3, 4)) { x => x > 2 } { x => x * 10 } +val expected = Seq[Int] = List(30, 40) +assert(result == expected) + +// scala> filterMap(Seq(1, 2, 3, 4)) { x => x > 2 } { x => x * 10 } +// filterMap(Seq(1, 2, 3, 4)) { x => x > 2 } { x => x * 10 } +// val res1: Seq[Int] = List(30, 40) + From d03d9e540480b0fd9b706650d949d3f894123bb4 Mon Sep 17 00:00:00 2001 From: Andreas Roehler Date: Sun, 14 Apr 2024 20:42:55 +0200 Subject: [PATCH 3/3] solution2.2.6.4.scala fixed Signed-off-by: Andreas Roehler --- chapter02/worksheets/solution2.2.6.4.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/chapter02/worksheets/solution2.2.6.4.scala b/chapter02/worksheets/solution2.2.6.4.scala index 2e3ac43..d8fc6b1 100644 --- a/chapter02/worksheets/solution2.2.6.4.scala +++ b/chapter02/worksheets/solution2.2.6.4.scala @@ -17,7 +17,7 @@ def filterMap[A, B](xs: Seq[A])(pred: A => Boolean)(f: A => B): Seq[B] = { } val result = filterMap(Seq(1, 2, 3, 4)) { x => x > 2 } { x => x * 10 } -val expected = Seq[Int] = List(30, 40) +val expected: Seq[Int] = List(30, 40) assert(result == expected) // scala> filterMap(Seq(1, 2, 3, 4)) { x => x > 2 } { x => x * 10 }