Skip to content

Commit 13aa90a

Browse files
Merge pull request #83 from Yaskier/patch-1
Concatenate and add elements examples for Lists.scala
2 parents b81f080 + 4ddc0b6 commit 13aa90a

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

src/main/scala/stdlib/Lists.scala

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,24 @@ object Lists extends FlatSpec with Matchers with org.scalaexercises.definitions.
160160
a should be(res0)
161161
}
162162

163+
/** You can prepend elements to a List to get a new List:
164+
*/
165+
def addElementsLists(res0: List[Int]) {
166+
val a = List(1, 3, 5, 7)
167+
168+
0 :: a should be(res0)
169+
}
170+
171+
/** Lists can be concatened and Nil is an empty List:
172+
*/
173+
def concatenateLists(res0: List[Int], res1: List[Int]) {
174+
val head = List(1, 3)
175+
val tail = List(5, 7)
176+
177+
head ::: tail should be(res0)
178+
head ::: Nil should be(res1)
179+
}
180+
163181
/** Lists reuse their tails:
164182
*/
165183
def reuseTailsLists(
@@ -179,5 +197,4 @@ object Lists extends FlatSpec with Matchers with org.scalaexercises.definitions.
179197
b.tail should be(res4)
180198
c.tail should be(res5)
181199
}
182-
183200
}

src/test/scala/stdlib/ListsSpec.scala

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,24 @@ class ListsSpec extends Spec with Checkers {
129129
)
130130
}
131131

132+
def `add elements` = {
133+
check(
134+
Test.testSuccess(
135+
Lists.addElementsLists _,
136+
List(0, 1, 3, 5, 7) :: HNil
137+
)
138+
)
139+
}
140+
141+
def `concatenate lists` = {
142+
check(
143+
Test.testSuccess(
144+
Lists.concatenateLists _,
145+
List(1, 3, 5, 7) :: List(1, 3) :: HNil
146+
)
147+
)
148+
}
149+
132150
def `lists share tails` = {
133151
check(
134152
Test.testSuccess(

0 commit comments

Comments
 (0)