Skip to content

Commit

Permalink
Add NonEmptyList#fromFoldable
Browse files Browse the repository at this point in the history
  • Loading branch information
markus1189 committed Apr 7, 2017
1 parent 1601570 commit 39ba433
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]): Eval[Option[NonEmptyList[A]]] =
F.reduceRightToOption(fa)(x => NonEmptyList.of(x))((e,acc) => Later(e :: acc.value))

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).value)
}
}
}

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

0 comments on commit 39ba433

Please sign in to comment.