-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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#fromFoldable #1594
Conversation
@@ -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)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about NonEmptyList.fromList(F.toList(fa))
?
The Foldable#toList
implementation using a mutable.ListBuffer
, which is overridden in most Foldable
instances (eg List
just returns the list itself, Vector
calls toList
, etc), should be more efficient than prepending to a NonEmptyList
.
Fine with me, I thought that going through a |
Codecov Report
@@ Coverage Diff @@
## master #1594 +/- ##
==========================================
+ Coverage 92.52% 92.52% +<.01%
==========================================
Files 249 249
Lines 3974 3975 +1
Branches 131 130 -1
==========================================
+ Hits 3677 3678 +1
Misses 297 297
Continue to review full report at Codecov.
|
Sorry about that I accidentally clicked the close button. There's a small argument for adding this method to the 👍 thanks! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks
Adds a new method
NonEmptyList#fromFoldable
. This generalizesNonEmptyList.fromList
such that you can also create aNonEmptyList
from anything that has aFoldable
instance, e.g.,Vector
,Set
, ...