-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #52 from andreas-roehler/master
Example2.2.5.6 added
- Loading branch information
Showing
4 changed files
with
224 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
54
chapter02/worksheets/Example2.2.5.6_object_main_privat.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} | ||
|
||
} | ||
|