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 sequenceFilter syntax #2600

Merged
merged 1 commit into from
Dec 7, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions core/src/main/scala/cats/implicits.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ object implicits
with syntax.AllSyntaxBinCompat1
with syntax.AllSyntaxBinCompat2
with syntax.AllSyntaxBinCompat3
with syntax.AllSyntaxBinCompat4
with instances.AllInstances
with instances.AllInstancesBinCompat0
with instances.AllInstancesBinCompat1
Expand Down
3 changes: 3 additions & 0 deletions core/src/main/scala/cats/syntax/all.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ abstract class AllSyntaxBinCompat
with AllSyntaxBinCompat1
with AllSyntaxBinCompat2
with AllSyntaxBinCompat3
with AllSyntaxBinCompat4

trait AllSyntax
extends AlternativeSyntax
Expand Down Expand Up @@ -77,3 +78,5 @@ trait AllSyntaxBinCompat2
with ValidatedSyntaxBincompat0

trait AllSyntaxBinCompat3 extends UnorderedFoldableSyntax with Function1Syntax

trait AllSyntaxBinCompat4 extends TraverseFilterSyntaxBinCompat0
2 changes: 1 addition & 1 deletion core/src/main/scala/cats/syntax/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ package object syntax {
object strong extends StrongSyntax
object try_ extends TrySyntax
object traverse extends TraverseSyntax
object traverseFilter extends TraverseFilterSyntax
object traverseFilter extends TraverseFilterSyntax with TraverseFilterSyntaxBinCompat0
object nonEmptyTraverse extends NonEmptyTraverseSyntax
object unorderedFoldable extends UnorderedFoldableSyntax
object unorderedTraverse extends UnorderedTraverseSyntax
Expand Down
18 changes: 18 additions & 0 deletions core/src/main/scala/cats/syntax/traverseFilter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,21 @@ package cats
package syntax

trait TraverseFilterSyntax extends TraverseFilter.ToTraverseFilterOps

trait TraverseFilterSyntaxBinCompat0 {
Copy link
Contributor

Choose a reason for hiding this comment

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

We also want this to go to cats.syntax.all and cats.implicits right?

Copy link
Contributor

Choose a reason for hiding this comment

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

also can we add at least a doctest for it?

Copy link
Member Author

Choose a reason for hiding this comment

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

It was in cats.syntax.all :) I added it to cats.implicits now, also made a doctest. Because there's been a release in the meantime, I added back the new bincompat trait. I'll push the changes once sbt gives me a green light.

implicit def toSequenceFilterOps[F[_], G[_], A](fgoa: F[G[Option[A]]]): SequenceFilterOps[F, G, A] =
new SequenceFilterOps(fgoa)
}

final class SequenceFilterOps[F[_], G[_], A](val fgoa: F[G[Option[A]]]) extends AnyVal {

/**
* {{{
* scala> import cats.implicits._
* scala> val a: List[Either[String, Option[Int]]] = List(Right(Some(1)), Right(Some(5)), Right(Some(3)))
* scala> val b: Either[String, List[Int]] = a.sequenceFilter
* b: Either[String, List[Int]] = Right(List(1, 5, 3))
* }}}
* */
def sequenceFilter(implicit F: TraverseFilter[F], G: Applicative[G]): G[F[A]] = F.traverseFilter(fgoa)(identity)
}
16 changes: 13 additions & 3 deletions tests/src/test/scala/cats/tests/SyntaxSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import scala.collection.immutable.SortedSet
import scala.collection.immutable.SortedMap
import cats.arrow.Compose
import cats.data.{Binested, Nested, NonEmptyChain, NonEmptyList, NonEmptySet}
import cats.instances.AllInstances
import cats.syntax.{AllSyntax, AllSyntaxBinCompat}
import cats.instances.{AllInstances, AllInstancesBinCompat0, AllInstancesBinCompat1, AllInstancesBinCompat2}
import cats.syntax.AllSyntaxBinCompat

/**
* Test that our syntax implicits are working.
Expand All @@ -26,7 +26,12 @@ import cats.syntax.{AllSyntax, AllSyntaxBinCompat}
*
* None of these tests should ever run, or do any runtime checks.
*/
object SyntaxSuite extends AllSyntaxBinCompat with AllInstances with AllSyntax {
object SyntaxSuite
extends AllSyntaxBinCompat
with AllInstances
with AllInstancesBinCompat0
with AllInstancesBinCompat1
with AllInstancesBinCompat2 {

// pretend we have a value of type A
def mock[A]: A = ???
Expand Down Expand Up @@ -387,4 +392,9 @@ object SyntaxSuite extends AllSyntaxBinCompat with AllInstances with AllSyntax {
val grouped: SortedMap[B, NonEmptyChain[A]] = list.groupByNec(f)
}

def testSequenceFilter[A, B]: Unit = {
val f = mock[List[Either[A, Option[B]]]]

val result: Either[A, List[B]] = f.sequenceFilter
}
}