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#fromFoldable #1594

Merged
merged 1 commit into from
Apr 7, 2017
Merged

Conversation

markus1189
Copy link
Contributor

@markus1189 markus1189 commented Apr 7, 2017

Adds a new method NonEmptyList#fromFoldable. This generalizes NonEmptyList.fromList such that you can also create a NonEmptyList from anything that has a Foldable instance, e.g., Vector, Set, ...

@@ -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))
Copy link
Collaborator

@peterneyens peterneyens Apr 7, 2017

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.

@markus1189
Copy link
Contributor Author

Fine with me, I thought that going through a List is probably worse than folding it into an NonEmptyList

@codecov-io
Copy link

codecov-io commented Apr 7, 2017

Codecov Report

Merging #1594 into master will increase coverage by <.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            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
Impacted Files Coverage Δ
core/src/main/scala/cats/data/NonEmptyList.scala 97.11% <100%> (+0.02%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 1601570...91eaf20. Read the comment docs.

@ceedubs ceedubs closed this Apr 7, 2017
@ceedubs ceedubs reopened this Apr 7, 2017
@ceedubs
Copy link
Contributor

ceedubs commented Apr 7, 2017

Sorry about that I accidentally clicked the close button.

There's a small argument for adding this method to the Foldable type class so that the NonEmptyList Foldable instance could override it for efficiency. However, I guess you probably don't want to bloat the Foldable type class with a bunch of methods to create specific collection types. And also the implementation for NonEmptyList will still be quite efficient anyway.

👍 thanks!

Copy link
Contributor

@kailuowang kailuowang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks

@kailuowang kailuowang merged commit 175bdfa into typelevel:master Apr 7, 2017
@kailuowang kailuowang modified the milestone: 1.0.0-MF Apr 26, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants