Skip to content

Commit

Permalink
Add NonEmptyList#fromFoldable (#1594)
Browse files Browse the repository at this point in the history
  • Loading branch information
markus1189 authored and kailuowang committed Apr 7, 2017
1 parent 1601570 commit 175bdfa
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
3 changes: 3 additions & 0 deletions core/src/main/scala/cats/data/NonEmptyList.scala
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,9 @@ object NonEmptyList extends NonEmptyListInstances {
case h :: t => NonEmptyList(h, t)
}

def fromFoldable[F[_], A](fa: F[A])(implicit F: Foldable[F]): Option[NonEmptyList[A]] =
fromList(F.toList(fa))

def fromReducible[F[_], A](fa: F[A])(implicit F: Reducible[F]): NonEmptyList[A] =
F.toNonEmptyList(fa)
}
Expand Down
6 changes: 6 additions & 0 deletions tests/src/test/scala/cats/tests/NonEmptyListTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,12 @@ class NonEmptyListTests extends CatsSuite {
nel.groupBy(f).mapValues(_.toList) should === (nel.toList.groupBy(f))
}
}

test("NonEmptyList#fromFoldabale is consistent with NonEmptyList#fromList") {
forAll { (xs: List[Int]) =>
NonEmptyList.fromList(xs) should === (NonEmptyList.fromFoldable(xs))
}
}
}

class ReducibleNonEmptyListCheck extends ReducibleCheck[NonEmptyList]("NonEmptyList") {
Expand Down

0 comments on commit 175bdfa

Please sign in to comment.