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 NonEmptyChain #2406

Merged
merged 23 commits into from
Aug 16, 2018
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions core/src/main/scala/cats/data/Chain.scala
Original file line number Diff line number Diff line change
Expand Up @@ -219,17 +219,17 @@ sealed abstract class Chain[+A] {
* Groups elements inside this `Chain` according to the `Order`
* of the keys produced by the given mapping function.
*/
final def groupBy[B](f: A => B)(implicit B: Order[B]): SortedMap[B, Chain[A]] = {
final def groupBy[B](f: A => B)(implicit B: Order[B]): SortedMap[B, NonEmptyChain[A]] = {
implicit val ordering: Ordering[B] = B.toOrdering
var m = SortedMap.empty[B, Chain[A]]
var m = SortedMap.empty[B, NonEmptyChain[A]]
val iter = iterator

while (iter.hasNext) {
val elem = iter.next
val k = f(elem)

m.get(k) match {
case None => m += ((k, one(elem))); ()
case None => m += ((k, NonEmptyChain.one(elem))); ()
case Some(cat) => m = m.updated(k, cat :+ elem)
}
}
Expand Down Expand Up @@ -320,17 +320,17 @@ sealed abstract class Chain[+A] {
/**
* Returns the number of elements in this structure
*/
final def length: Int = {
final def length: Long = {
val iter = iterator
var i: Int = 0
var i: Long = 0
Copy link
Contributor

Choose a reason for hiding this comment

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

just curious, did we actually tried one instance with more than 2 billion items?

Copy link
Member Author

Choose a reason for hiding this comment

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

No I don't think so, I just wanted to be consistent with NonEmptyList and NonEmptyVector :)

while(iter.hasNext) { i += 1; iter.next; }
i
}

/**
* Alias for length
*/
final def size: Int = length
final def size: Long = length


/**
Expand Down Expand Up @@ -555,7 +555,7 @@ private[data] sealed abstract class ChainInstances extends ChainInstances1 {
def coflatMap[A, B](fa: Chain[A])(f: Chain[A] => B): Chain[B] = {
@tailrec def go(as: Chain[A], res: ListBuffer[B]): Chain[B] =
as.uncons match {
case Some((h, t)) => go(t, res += f(t))
case Some((_, t)) => go(t, res += f(as))
case None => Chain.fromSeq(res.result())
}

Expand Down
Loading