-
-
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 missing methods to NE collections and syntax #3998
Draft
satorg
wants to merge
13
commits into
typelevel:main
Choose a base branch
from
satorg:prepend-colls-to-nevs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
ef76701
add missing docs for `toNev`
satorg 2a10404
add `groupByNev`
satorg 8437bdc
add `groupByNevA`
satorg 21c99aa
add `scanLeftNev` and `scanRightNev`
satorg 19f5a89
add tests for the `ListOps`'s `scanLeftNel` and `scanRightNel`
satorg 203fad1
merge branch 'extend-vector-syntax' into 'prepend-colls-to-nevs'
satorg 71ce7fb
add `prependList` to `NonEmptyList`
satorg ca53c58
add tests to `NonEmptyListSuite`
satorg 209c34f
add `ListOps#concatNel`
satorg e865609
add `VectorOps#concatNev`
satorg e70f22a
add `SeqOps#concatNeSeq`
satorg c70efc8
add `groupByNeSeq` and `groupByNeSeqA` to `SeqOps`
satorg 87b26cb
add `scanLeftNeSeq` and `scanRightNeSeq` to `SeqOps`
satorg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,153 @@ | ||
package cats.syntax | ||
|
||
import cats.Applicative | ||
import cats.Functor | ||
import cats.Order | ||
import cats.Traverse | ||
import cats.data.NonEmptySeq | ||
|
||
import scala.collection.immutable.Seq | ||
import scala.collection.immutable.SortedMap | ||
|
||
trait SeqSyntax { | ||
implicit final def catsSyntaxSeqs[A](va: Seq[A]): SeqOps[A] = new SeqOps(va) | ||
} | ||
|
||
final class SeqOps[A](private val va: Seq[A]) extends AnyVal { | ||
|
||
/** | ||
* Returns an `Option` of `NonEmptySeq` from a `Seq` | ||
* | ||
* Example: | ||
* {{{ | ||
* scala> import cats.data.NonEmptySeq | ||
* scala> import cats.syntax.all._ | ||
* scala> import scala.collection.immutable.Seq | ||
* | ||
* scala> val seq1 = Seq(1, 2) | ||
* scala> seq1.toNeSeq | ||
* res1: Option[NonEmptySeq[Int]] = Some(NonEmptySeq(1, 2)) | ||
* | ||
* scala> val seq2 = Seq.empty[Int] | ||
* scala> seq2.toNeSeq | ||
* res2: Option[NonEmptySeq[Int]] = None | ||
* }}} | ||
*/ | ||
def toNeSeq: Option[NonEmptySeq[A]] = NonEmptySeq.fromSeq(va) | ||
|
||
/** | ||
* Concatenates this `Seq` with a `NonEmptySeq` producing a new `NonEmptySeq`. | ||
* | ||
* Example: | ||
* {{{ | ||
* scala> import cats.data.NonEmptySeq | ||
* scala> import cats.syntax.all._ | ||
* scala> import scala.collection.immutable.Seq | ||
* | ||
* scala> Seq(1, 2, 3).concatNeSeq(NonEmptySeq.of(4, 5, 6)) | ||
* res0: NonEmptySeq[Int] = NonEmptySeq(1, 2, 3, 4, 5, 6) | ||
* }}} | ||
*/ | ||
def concatNeSeq[AA >: A](neseq: NonEmptySeq[AA]): NonEmptySeq[AA] = neseq.prependSeq(va) | ||
|
||
/** | ||
* Groups elements inside this `Seq` according to the `Order` of the keys | ||
* produced by the given mapping function. | ||
* | ||
* {{{ | ||
* scala> import cats.data.NonEmptySeq | ||
* scala> import cats.syntax.all._ | ||
* scala> import scala.collection.immutable.Seq | ||
* scala> import scala.collection.immutable.SortedMap | ||
* | ||
* scala> val seq = Seq(12, -2, 3, -5) | ||
* scala> val res = SortedMap(false -> NonEmptySeq.of(-2, -5), true -> NonEmptySeq.of(12, 3)) | ||
* scala> seq.groupByNeSeq(_ >= 0) === res | ||
* res1: Boolean = true | ||
* }}} | ||
*/ | ||
def groupByNeSeq[B](f: A => B)(implicit B: Order[B]): SortedMap[B, NonEmptySeq[A]] = { | ||
implicit val ordering: Ordering[B] = B.toOrdering | ||
toNeSeq.fold(SortedMap.empty[B, NonEmptySeq[A]])(_.groupBy(f)) | ||
} | ||
|
||
/** | ||
* Groups elements inside this `Seq` according to the `Order` of the keys | ||
* produced by the given mapping monadic function. | ||
* | ||
* {{{ | ||
* scala> import cats.data.NonEmptySeq | ||
* scala> import cats.syntax.all._ | ||
* scala> import scala.collection.immutable.Seq | ||
* scala> import scala.collection.immutable.SortedMap | ||
* | ||
* scala> def f(n: Int) = n match { case 0 => None; case n => Some(n > 0) } | ||
* | ||
* scala> val seq = Seq(12, -2, 3, -5) | ||
* scala> val res = Some(SortedMap(false -> NonEmptySeq.of(-2, -5), true -> NonEmptySeq.of(12, 3))) | ||
* scala> seq.groupByNeSeqA(f) === res | ||
* res1: Boolean = true | ||
* | ||
* scala> // `f(0)` returns `None` | ||
* scala> (seq :+ 0).groupByNeSeqA(f) === None | ||
* res2: Boolean = true | ||
* }}} | ||
*/ | ||
def groupByNeSeqA[F[_], B]( | ||
f: A => F[B] | ||
)(implicit F: Applicative[F], B: Order[B]): F[SortedMap[B, NonEmptySeq[A]]] = { | ||
implicit val ordering: Ordering[B] = B.toOrdering | ||
val mapFunctor = Functor[SortedMap[B, *]] | ||
val nesTraverse = Traverse[NonEmptySeq] | ||
|
||
toNeSeq.fold(F.pure(SortedMap.empty[B, NonEmptySeq[A]])) { nes => | ||
F.map(nesTraverse.traverse(nes)(a => F.tupleRight(f(a), a))) { seq => | ||
mapFunctor.map(seq.groupBy(_._1))(_.map(_._2)) | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Produces a `NonEmptySeq` containing cumulative results of applying the | ||
* operator going left to right. | ||
* | ||
* Example: | ||
* {{{ | ||
* scala> import cats.data.NonEmptySeq | ||
* scala> import cats.syntax.all._ | ||
* scala> import scala.collection.immutable.Seq | ||
* | ||
* scala> val seq1 = Seq(1, 2) | ||
* scala> seq1.scanLeftNeSeq(100)(_ + _) | ||
* res1: NonEmptySeq[Int] = NonEmptySeq(100, 101, 103) | ||
* | ||
* scala> val seq2 = Seq.empty[Int] | ||
* scala> seq2.scanLeftNeSeq(123)(_ + _) | ||
* res2: NonEmptySeq[Int] = NonEmptySeq(123) | ||
* }}} | ||
*/ | ||
def scanLeftNeSeq[B](b: B)(f: (B, A) => B): NonEmptySeq[B] = | ||
NonEmptySeq.fromSeqUnsafe(va.scanLeft(b)(f)) | ||
|
||
/** | ||
* Produces a `NonEmptySeq` containing cumulative results of applying the | ||
* operator going right to left. | ||
* | ||
* Example: | ||
* {{{ | ||
* scala> import cats.data.NonEmptySeq | ||
* scala> import cats.syntax.all._ | ||
* scala> import scala.collection.immutable.Seq | ||
* | ||
* scala> val seq = Seq(1, 2) | ||
* scala> seq.scanRightNeSeq(100)(_ + _) | ||
* res0: NonEmptySeq[Int] = NonEmptySeq(103, 102, 100) | ||
* | ||
* scala> val seq2 = Seq.empty[Int] | ||
* scala> seq2.scanRightNeSeq(123)(_ + _) | ||
* res1: NonEmptySeq[Int] = NonEmptySeq(123) | ||
* }}} | ||
*/ | ||
def scanRightNeSeq[B](b: B)(f: (A, B) => B): NonEmptySeq[B] = | ||
NonEmptySeq.fromSeqUnsafe(va.scanRight(b)(f)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,141 @@ | ||
package cats.syntax | ||
|
||
import cats.data.NonEmptyVector | ||
import cats.{Applicative, Functor, Order, Traverse} | ||
|
||
import scala.collection.immutable.SortedMap | ||
|
||
trait VectorSyntax { | ||
implicit final def catsSyntaxVectors[A](va: Vector[A]): VectorOps[A] = new VectorOps(va) | ||
} | ||
|
||
final class VectorOps[A](private val va: Vector[A]) extends AnyVal { | ||
|
||
/** | ||
* Returns an Option of NonEmptyVector from a Vector | ||
* | ||
* Example: | ||
* {{{ | ||
* scala> import cats.data.NonEmptyVector | ||
* scala> import cats.implicits._ | ||
* | ||
* scala> val result1: Vector[Int] = Vector(1, 2) | ||
* scala> result1.toNev | ||
* res0: Option[NonEmptyVector[Int]] = Some(NonEmptyVector(1, 2)) | ||
* | ||
* scala> val result2: Vector[Int] = Vector.empty[Int] | ||
* scala> result2.toNev | ||
* res1: Option[NonEmptyVector[Int]] = None | ||
* }}} | ||
*/ | ||
def toNev: Option[NonEmptyVector[A]] = NonEmptyVector.fromVector(va) | ||
|
||
/** | ||
* Concatenates this `Vector` with a `NonEmptyVector` producing a new `NonEmptyVector`. | ||
* | ||
* Example: | ||
* {{{ | ||
* scala> import cats.data.NonEmptyVector | ||
* scala> import cats.implicits._ | ||
* | ||
* scala> Vector(1, 2, 3).concatNev(NonEmptyVector.of(4, 5, 6)) | ||
* res0: NonEmptyVector[Int] = NonEmptyVector(1, 2, 3, 4, 5, 6) | ||
* }}} | ||
*/ | ||
def concatNev[AA >: A](nev: NonEmptyVector[AA]): NonEmptyVector[AA] = nev.prependVector(va) | ||
|
||
/** | ||
* Groups elements inside this `Vector` according to the `Order` of the keys | ||
* produced by the given mapping function. | ||
* | ||
* {{{ | ||
* scala> import cats.data.NonEmptyVector | ||
* scala> import scala.collection.immutable.SortedMap | ||
* scala> import cats.implicits._ | ||
* | ||
* scala> val vector = Vector(12, -2, 3, -5) | ||
* | ||
* scala> val expectedResult = SortedMap(false -> NonEmptyVector.of(-2, -5), true -> NonEmptyVector.of(12, 3)) | ||
* | ||
* scala> vector.groupByNev(_ >= 0) === expectedResult | ||
* res0: Boolean = true | ||
* }}} | ||
*/ | ||
def groupByNev[B](f: A => B)(implicit B: Order[B]): SortedMap[B, NonEmptyVector[A]] = { | ||
implicit val ordering: Ordering[B] = B.toOrdering | ||
toNev.fold(SortedMap.empty[B, NonEmptyVector[A]])(_.groupBy(f)) | ||
} | ||
|
||
/** | ||
* Groups elements inside this `Vector` according to the `Order` of the keys | ||
* produced by the given mapping monadic function. | ||
* | ||
* {{{ | ||
* scala> import cats.data.NonEmptyVector | ||
* scala> import scala.collection.immutable.SortedMap | ||
* scala> import cats.implicits._ | ||
* | ||
* scala> val vector = Vector(12, -2, 3, -5) | ||
* | ||
* scala> val expectedResult = Option(SortedMap(false -> NonEmptyVector.of(-2, -5), true -> NonEmptyVector.of(12, 3))) | ||
* | ||
* scala> vector.groupByNevA(num => Option(0).map(num >= _)) === expectedResult | ||
* res0: Boolean = true | ||
* }}} | ||
*/ | ||
def groupByNevA[F[_], B]( | ||
f: A => F[B] | ||
)(implicit F: Applicative[F], B: Order[B]): F[SortedMap[B, NonEmptyVector[A]]] = { | ||
implicit val ordering: Ordering[B] = B.toOrdering | ||
val mapFunctor = Functor[SortedMap[B, *]] | ||
val nevTraverse = Traverse[NonEmptyVector] | ||
|
||
toNev.fold(F.pure(SortedMap.empty[B, NonEmptyVector[A]])) { nev => | ||
F.map(nevTraverse.traverse(nev)(a => F.tupleLeft(f(a), a))) { vector => | ||
mapFunctor.map(vector.groupBy(_._2))(_.map(_._1)) | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Produces a `NonEmptyVector` containing cumulative results of applying the | ||
* operator going left to right. | ||
* | ||
* Example: | ||
* {{{ | ||
* scala> import cats.data.NonEmptyVector | ||
* scala> import cats.implicits._ | ||
* | ||
* scala> val result1: Vector[Int] = Vector(1, 2) | ||
* scala> result1.scanLeftNev(100)(_ + _) | ||
* res0: NonEmptyVector[Int] = NonEmptyVector(100, 101, 103) | ||
* | ||
* scala> val result2: Vector[Int] = Vector.empty[Int] | ||
* scala> result2.scanLeftNev(1)(_ + _) | ||
* res1: NonEmptyVector[Int] = NonEmptyVector(1) | ||
* }}} | ||
*/ | ||
def scanLeftNev[B](b: B)(f: (B, A) => B): NonEmptyVector[B] = | ||
NonEmptyVector.fromVectorUnsafe(va.scanLeft(b)(f)) | ||
|
||
/** | ||
* Produces a `NonEmptyVector` containing cumulative results of applying the | ||
* operator going right to left. | ||
* | ||
* Example: | ||
* {{{ | ||
* scala> import cats.data.NonEmptyVector | ||
* scala> import cats.implicits._ | ||
* | ||
* scala> val result1: Vector[Int] = Vector(1, 2) | ||
* scala> result1.scanRightNev(100)(_ + _) | ||
* res0: NonEmptyVector[Int] = NonEmptyVector(103, 102, 100) | ||
* | ||
* scala> val result2: Vector[Int] = Vector.empty[Int] | ||
* scala> result2.scanRightNev(1)(_ + _) | ||
* res1: NonEmptyVector[Int] = NonEmptyVector(1) | ||
* }}} | ||
*/ | ||
def scanRightNev[B](b: B)(f: (A, B) => B): NonEmptyVector[B] = | ||
NonEmptyVector.fromVectorUnsafe(va.scanRight(b)(f)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I wonder if this should live here:
https://github.com/typelevel/cats/blob/main/core/src/main/scala/cats/NonEmptyTraverse.scala
and then you can call that here:
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.
Well, these particular methods are just an attempt to maintain some degree of consistency and interoperability among all linear "non-empty" collections we have in
cats.data
. Because initially there wasListOps#groupByNelA
only but no corresponding methods inVectorOps
norSeqOps
. Which is a bit confusing.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.
But I like the idea of generalization. I also have a feeling, that keeping stuffing various
*Ops
classes with almost identical methods is not the best way to deal with it. I like your suggestion in particular, but why justNonEmptyTraverse
?Seems, it should work with just a regular
Traverse
– shouldn't it?I mean, when we do grouping on some collection by some predicate, all the groups we will get eventually are going to be non-empty regardless of whether the source collection was empty or not. I.e. we will either get a non-empty group or don't get it at all.
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.
We can use the way you suggested of course:
But to me it has at least two flaws:
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.
to your point 2: putting
groupByA
onNonEmptyTraverse
we could actually returnNonEmptyMap[B, F[A]]
, so we know both the map is non-empty and sinceF[_]: NonEmptyTraverse
we also knowF[_]
is non-empty.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.
Yes, this is a valid case too. But I'd suppose that grouping over a regular
Traverse
is a more common case anyway. And it's guaranteed to produce a regularMap
ofNonEmpty*
groups. While grouping overNonEmptyTraverse
is guaranteed to produce aNonEmptyMap
ofNonEmpty*
groups. I mean both cases looks legit to me and I'd like to think on how to incorporate both of them.