Skip to content

Commit

Permalink
add assertions
Browse files Browse the repository at this point in the history
Signed-off-by: Andreas Roehler <[email protected]>
  • Loading branch information
andreas-roehler committed Mar 4, 2024
1 parent d56afb6 commit 03dc29f
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 17 deletions.
23 changes: 14 additions & 9 deletions chapter02/worksheets/Example2.2.5.6.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ Implement a function toPairs that converts a sequence of type
Seq[A] to a sequence of pairs, Seq[(A, A)], by putting together the adjacent elements pairwise. If the initial sequence has an odd number of elements, a given
default value of type A is used to fill the last pair. The required type signature and
an example test:
*/
// def toPairs[A](xs: Seq[A], default: A): Seq[(A, A)] = ???
def toPairs[A](xs: Seq[A], default: A): Seq[(A, A)] = ???
// scala> toPairs(Seq(1, 2, 3, 4, 5, 6), -1)
// res0: Seq[(Int, Int)] = List((1,2), (3,4), (5,6))
scala> toPairs(Seq(1, 2, 3, 4, 5, 6), -1)
res0: Seq[(Int, Int)] = List((1,2), (3,4), (5,6))
// scala> toPairs(Seq("a", "b", "c"), "<nothing>")
// res1: Seq[(String, String)] = List((a,b), (c,<nothing>))
scala> toPairs(Seq("a", "b", "c"), "<nothing>")
res1: Seq[(String, String)] = List((a,b), (c,<nothing>))
*/

def toPairs[A](xs: Seq[A], default: A): Seq[(A, A)] = {
type Acc = (Seq[(A, A)], Seq[A])
Expand All @@ -34,6 +34,11 @@ def toPairs[A](xs: Seq[A], default: A): Seq[(A, A)] = {
}
}

val a = toPairs(Seq(1, 2, 3, 4, 5, 6), -1)
println("a: %s".format(a))
//
val result = toPairs(Seq(1, 2, 3, 4, 5, 6), -1)
val expected: Seq[(Int, Int)] = List((1,2), (3,4), (5,6))
assert(result == expected)

val a = toPairs(Seq("a", "b", "c"), "<nothing>")
val b: Seq[(String, String)] = List(("a","b"), ("c","<nothing>"))
assert(a == b)

21 changes: 14 additions & 7 deletions chapter02/worksheets/Example2.2.5.6_object.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ Implement a function toPairs that converts a sequence of type
Seq[A] to a sequence of pairs, Seq[(A, A)], by putting together the adjacent elements pairwise. If the initial sequence has an odd number of elements, a given
default value of type A is used to fill the last pair. The required type signature and
an example test:
def toPairs[A](xs: Seq[A], default: A): Seq[(A, A)] = ???
scala> toPairs(Seq(1, 2, 3, 4, 5, 6), -1)
res0: Seq[(Int, Int)] = List((1,2), (3,4), (5,6))
scala> toPairs(Seq("a", "b", "c"), "<nothing>")
res1: Seq[(String, String)] = List((a,b), (c,<nothing>))
*/

object ToPairsObject {
Expand Down Expand Up @@ -32,11 +40,10 @@ object ToPairsObject {
}
}

val a = ToPairsObject.toPairs(Seq(1, 2, 3), 4)
println("a: %s".format(a))
val result = toPairs(Seq(1, 2, 3, 4, 5, 6), -1)
val expected: Seq[(Int, Int)] = List((1,2), (3,4), (5,6))
assert(result == expected)

// scala> :load Example2.2.5.6_object.scala
// :load Example2.2.5.6_object.scala
// a: List((1,2), (3,4))
// // defined object ToPairsObject
// val a: Seq[(Int, Int)] = List((1,2), (3,4))
val a = toPairs(Seq("a", "b", "c"), "<nothing>")
val b: Seq[(String, String)] = List(("a","b"), ("c","<nothing>"))
assert(a == b)
16 changes: 16 additions & 0 deletions chapter02/worksheets/Example2.2.5.6_object_main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ Implement a function toPairs that converts a sequence of type
Seq[A] to a sequence of pairs, Seq[(A, A)], by putting together the adjacent elements pairwise. If the initial sequence has an odd number of elements, a given
default value of type A is used to fill the last pair. The required type signature and
an example test:
def toPairs[A](xs: Seq[A], default: A): Seq[(A, A)] = ???
scala> toPairs(Seq(1, 2, 3, 4, 5, 6), -1)
res0: Seq[(Int, Int)] = List((1,2), (3,4), (5,6))
scala> toPairs(Seq("a", "b", "c"), "<nothing>")
res1: Seq[(String, String)] = List((a,b), (c,<nothing>))
*/

object ToPairsObject {
Expand Down Expand Up @@ -36,3 +44,11 @@ object ToPairsObject {
println("a: %s".format(a))
}
}

val result = toPairs(Seq(1, 2, 3, 4, 5, 6), -1)
val expected: Seq[(Int, Int)] = List((1,2), (3,4), (5,6))
assert(result == expected)

val a = toPairs(Seq("a", "b", "c"), "<nothing>")
val b: Seq[(String, String)] = List(("a","b"), ("c","<nothing>"))
assert(a == b)
18 changes: 17 additions & 1 deletion chapter02/worksheets/Example2.2.5.6_object_main_privat.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ Implement a function toPairs that converts a sequence of type
Seq[A] to a sequence of pairs, Seq[(A, A)], by putting together the adjacent elements pairwise. If the initial sequence has an odd number of elements, a given
default value of type A is used to fill the last pair. The required type signature and
an example test:
def toPairs[A](xs: Seq[A], default: A): Seq[(A, A)] = ???
scala> toPairs(Seq(1, 2, 3, 4, 5, 6), -1)
res0: Seq[(Int, Int)] = List((1,2), (3,4), (5,6))
scala> toPairs(Seq("a", "b", "c"), "<nothing>")
res1: Seq[(String, String)] = List((a,b), (c,<nothing>))
*/

object ToPairsObject {
Expand Down Expand Up @@ -32,8 +40,16 @@ object ToPairsObject {

}
def main(args: Array[String]): Unit = {
val a = ToPairsObject.toPairs(Seq(1, 2, 3), 4)
val result = ToPairsObject.toPairs(Seq(1, 2, 3), 4)
assert(result == expected)
println("a: %s".format(a))
}
}

val result = toPairs(Seq(1, 2, 3, 4, 5, 6), -1)
val expected: Seq[(Int, Int)] = List((1,2), (3,4), (5,6))
assert(result == expected)

val a = toPairs(Seq("a", "b", "c"), "<nothing>")
val b: Seq[(String, String)] = List(("a","b"), ("c","<nothing>"))
assert(a == b)

0 comments on commit 03dc29f

Please sign in to comment.