Skip to content

Commit

Permalink
Add NonEmptyList.ofInitLast (#1910)
Browse files Browse the repository at this point in the history
  • Loading branch information
eddsteel authored and kailuowang committed Sep 13, 2017
1 parent eff36ef commit ca0204c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
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

0 comments on commit ca0204c

Please sign in to comment.