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.fromListGroupBy #2157

Closed
chuwy opened this issue Jan 19, 2018 · 4 comments
Closed

Add NonEmptyList.fromListGroupBy #2157

chuwy opened this issue Jan 19, 2018 · 4 comments

Comments

@chuwy
Copy link

chuwy commented Jan 19, 2018

Name probably can be improved (fromListGroupedBy?), implementation needs to be improved for sure - I and will be happy to work on it if feature looks useful for someone. Basically idea is following:

def fromListGroupBy[A, K](as: List[A], f: A => K): Map[K, NonEmptyList[A]] =
  as.groupBy(f).map { case (k, list) => list match {
    case h :: t => (k, NonEmptyList(h, t)).some
    case Nil => none
  }}.toList.sequence.map(_.toMap).getOrElse(Map.empty)

We always know that if List[A] is not empty then values in Map will be NonEmptyList.

This also can be implemented along with #2141 as there's clear connection between two structures.

@LukaJCB
Copy link
Member

LukaJCB commented Jan 19, 2018

That function already exists here: https://github.com/typelevel/cats/blob/master/core/src/main/scala/cats/syntax/list.scala

:)

@chuwy
Copy link
Author

chuwy commented Jan 19, 2018

Ah, thank you very much, @LukaJCB. I was looking in NonEmptyList. Closing.

@chuwy chuwy closed this as completed Jan 19, 2018
@chuwy
Copy link
Author

chuwy commented Jan 19, 2018

...and even in 0.9.0. SIgh.

@ShahOdin
Copy link

ShahOdin commented Apr 18, 2019

groupByNel needs an Order implementation and returns a SortedMap. Would be been really nice if we also had the above version which doesn't need all that.

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

No branches or pull requests

3 participants