Skip to content

Commit

Permalink
Merge pull request #52 from andreas-roehler/master
Browse files Browse the repository at this point in the history
Example2.2.5.6 added
  • Loading branch information
winitzki authored Mar 5, 2024
2 parents 73c23ee + 7ddd570 commit 6b5bce6
Show file tree
Hide file tree
Showing 4 changed files with 224 additions and 0 deletions.
51 changes: 51 additions & 0 deletions chapter02/worksheets/Example2.2.5.6.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
Example 2.2.5.6
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>))
*/

def toPairs[A](xs: Seq[A], default: A): Seq[(A, A)] = {
type Acc = (Seq[(A, A)], Seq[A])
// Type alias, for brevity.
def init: Acc = (Seq(), Seq())
def updater(acc: Acc, x: A): Acc = acc match {
case (result, Seq())
=> (result, Seq(x))
case (result, Seq(prev)) => (result :+ ((prev, x)), Seq())
}
val (result, holdover) = xs.foldLeft(init)(updater)
holdover match {
// May need to append the last element to the result.
case Seq()
=> result
case Seq(x)
=> result :+ ((x, default))
}
}

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)

// scala> :load Example2.2.5.6.scala
// :load Example2.2.5.6.scala
// def toPairs[A](xs: Seq[A], default: A): Seq[(A, A)]
// val result: Seq[(Int, Int)] = List((1,2), (3,4), (5,6))
// val expected: Seq[(Int, Int)] = List((1,2), (3,4), (5,6))
// val a: Seq[(String, String)] = List((a,b), (c,<nothing>))
// val b: Seq[(String, String)] = List((a,b), (c,<nothing>))
57 changes: 57 additions & 0 deletions chapter02/worksheets/Example2.2.5.6_object.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/**
Example 2.2.5.6
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 {
def toPairs[A](xs: Seq[A], default: A): Seq[(A, A)] = {
type Acc = (Seq[(A, A)], Seq[A])

// Type alias, for brevity.
def init: Acc = (Seq(), Seq())

def updater(acc: Acc, x: A): Acc = acc match {
case (result, Seq())
=> (result, Seq(x))
case (result, Seq(prev)) => (result :+ ((prev, x)), Seq())

}

val (result, holdover) = xs.foldLeft(init)(updater)
holdover match {
// May need to append the last element to the result.
case Seq()
=> result
case Seq(x)
=> result :+ ((x, default))
}
}
}

val result = ToPairsObject.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 = ToPairsObject.toPairs(Seq("a", "b", "c"), "<nothing>")
val b: Seq[(String, String)] = List(("a","b"), ("c","<nothing>"))
assert(a == b)

// scala> :load Example2.2.5.6_object.scala
// :load Example2.2.5.6_object.scala
// // defined object ToPairsObject
// val result: Seq[(Int, Int)] = List((1,2), (3,4), (5,6))
// val expected: Seq[(Int, Int)] = List((1,2), (3,4), (5,6))
// val a: Seq[(String, String)] = List((a,b), (c,<nothing>))
// val b: Seq[(String, String)] = List((a,b), (c,<nothing>))
62 changes: 62 additions & 0 deletions chapter02/worksheets/Example2.2.5.6_object_main.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**
Example 2.2.5.6
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 {
def toPairs[A](xs: Seq[A], default: A): Seq[(A, A)] = {
type Acc = (Seq[(A, A)], Seq[A])

// Type alias, for brevity.
def init: Acc = (Seq(), Seq())

def updater(acc: Acc, x: A): Acc = acc match {
case (result, Seq())
=> (result, Seq(x))
case (result, Seq(prev)) => (result :+ ((prev, x)), Seq())

}

val (result, holdover) = xs.foldLeft(init)(updater)
holdover match {
// May need to append the last element to the result.
case Seq()
=> result
case Seq(x)
=> result :+ ((x, default))
}

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

val result = ToPairsObject.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 = ToPairsObject.toPairs(Seq("a", "b", "c"), "<nothing>")
val b: Seq[(String, String)] = List(("a","b"), ("c","<nothing>"))
assert(a == b)

// scala> :load Example2.2.5.6_object_main.scala
// :load Example2.2.5.6_object_main.scala
// // defined object ToPairsObject
// val result: Seq[(Int, Int)] = List((1,2), (3,4), (5,6))
// val expected: Seq[(Int, Int)] = List((1,2), (3,4), (5,6))
// val a: Seq[(String, String)] = List((a,b), (c,<nothing>))
// val b: Seq[(String, String)] = List((a,b), (c,<nothing>))
54 changes: 54 additions & 0 deletions chapter02/worksheets/Example2.2.5.6_object_main_privat.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
Example 2.2.5.6
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 {
private def toPairs[A](xs: Seq[A], default: A): Seq[(A, A)] = {
type Acc = (Seq[(A, A)], Seq[A])

// Type alias, for brevity.
def init: Acc = (Seq(), Seq())

def updater(acc: Acc, x: A): Acc = acc match {
case (result, Seq())
=> (result, Seq(x))
case (result, Seq(prev)) => (result :+ ((prev, x)), Seq())

}

val (result, holdover) = xs.foldLeft(init)(updater)
holdover match {
// May need to append the last element to the result.
case Seq()
=> result
case Seq(x)
=> result :+ ((x, default))
}

}
def main(args: Array[String]): Unit = {
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)
println("a: %s".format(a))
}

}

0 comments on commit 6b5bce6

Please sign in to comment.