Skip to content
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

Add NonEmptyList.ofInitLast #1910

Merged
merged 1 commit into from
Sep 13, 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
6 changes: 6 additions & 0 deletions core/src/main/scala/cats/data/NonEmptyList.scala
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,12 @@ final case class NonEmptyList[+A](head: A, tail: List[A]) {
object NonEmptyList extends NonEmptyListInstances {
def of[A](head: A, tail: A*): NonEmptyList[A] = NonEmptyList(head, tail.toList)

def ofInitLast[A](init: List[A], last: A): NonEmptyList[A] =
init match {
case Nil => NonEmptyList(last, Nil)
case h :: t => NonEmptyList(h, t :+ last)
}

def one[A](head: A): NonEmptyList[A] = NonEmptyList(head, Nil)

/**
Expand Down
7 changes: 7 additions & 0 deletions tests/src/test/scala/cats/tests/NonEmptyListTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,13 @@ class NonEmptyListTests extends CatsSuite {
}
}

test("Creating NonEmptyList with init/last + toList is identity") {
forAll { (init: List[Int], last: Int) =>
val list = init :+ last
val nonEmptyList = NonEmptyList.ofInitLast(init, last)
list should === (nonEmptyList.toList)
}
}

test("NonEmptyList#filter is consistent with List#filter") {
forAll { (nel: NonEmptyList[Int], p: Int => Boolean) =>
Expand Down