Skip to content

Concatenate and add elements examples for Lists.scala #83

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion src/main/scala/stdlib/Lists.scala
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,24 @@ object Lists extends FlatSpec with Matchers with org.scalaexercises.definitions.
a should be(res0)
}

/** You can prepend elements to a List to get a new List:
*/
def addElementsLists(res0: List[Int]) {
val a = List(1, 3, 5, 7)

0 :: a should be(res0)
}

/** Lists can be concatened and Nil is an empty List:
*/
def concatenateLists(res0: List[Int], res1: List[Int]) {
val head = List(1, 3)
val tail = List(5, 7)

head ::: tail should be(res0)
head ::: Nil should be(res1)
}

/** Lists reuse their tails:
*/
def reuseTailsLists(
Expand All @@ -179,5 +197,4 @@ object Lists extends FlatSpec with Matchers with org.scalaexercises.definitions.
b.tail should be(res4)
c.tail should be(res5)
}

}
18 changes: 18 additions & 0 deletions src/test/scala/stdlib/ListsSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,24 @@ class ListsSpec extends Spec with Checkers {
)
}

def `add elements` = {
check(
Test.testSuccess(
Lists.addElementsLists _,
List(0, 1, 3, 5, 7) :: HNil
)
)
}

def `concatenate lists` = {
check(
Test.testSuccess(
Lists.concatenateLists _,
List(1, 3, 5, 7) :: List(1, 3) :: HNil
)
)
}

def `lists share tails` = {
check(
Test.testSuccess(
Expand Down