-
-
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 Traverse1 typeclass (#1577) #1611
Merged
Merged
Changes from all commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
5452d01
Add Traverse1 typeclass (#1577)
96d79d2
Add tests and laws for Traverse1 typeclass (#1577)
4a38545
Merge branch 'master' into master
3e9afe3
Format with newline at end of file
7ff5e1e
Replace NonEmptyReducible with NonEmptyTraverse1
2e2e7ac
Add Scaladocs to Traverse1
82f4537
Remove unneeded Unapply version
ab40d79
Readd Reducible instance to OneAnd so a Monad is not needed for a Fol…
11e8a5d
Use NonEmptyTraverse1 for traverse1 implementation of OneAnd
6945a26
Rename flatSequence
17d8d62
Remove NonEmptyReducible class and replace with NonEmptyReducible + T…
b1e115b
Replace traverse1 with more performant implementation
796ebb4
Merge branch 'master' into master
f437a98
Remove Unapply syntax
647df06
Separate traverse and traverse1 for OneAnd
1f69c30
Override traverse implementation of NonEmptyVector
caac5f7
Change type annotation of NonEmptyvector instances to reflect actual …
aa5d6aa
Add Law tests for traverse1 instances of NonEmptyList and Vector
590ba54
Correct inheritance for OneAnd
dfbc064
Add traverse1 law testing for OneAnd instance
9093a53
Add doctests for traverse1 and sequence1
5c49d9f
Add remaining doctests
0cc4531
Override traverse in traverse1 instance of OneAnd
a2bbee4
Fix Indentation
8e9656b
Remove redundant tests and replace with Traverse1 tests
f214c64
Add Traverse1#compose and instance for Nested
9ed10ea
Move nested traverse1 instance to NestedInstances
7f55edc
Move reducible nested tests
02c25d5
rename traverse1 to nonEmptyTraverse
50cc4c0
Rename intercalate1 to nonEmptyIntercalate
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package cats | ||
|
||
import simulacrum.typeclass | ||
|
||
/** | ||
* NonEmptyTraverse, also known as Traversable1. | ||
* | ||
* `NonEmptyTraverse` is like a non-empty `Traverse`. In addition to the traverse and sequence | ||
* methods it provides nonEmptyTraverse and nonEmptySequence methods which require an `Apply` instance instead of `Applicative`. | ||
*/ | ||
@typeclass trait NonEmptyTraverse[F[_]] extends Traverse[F] with Reducible[F] { self => | ||
|
||
/** | ||
* Given a function which returns a G effect, thread this effect | ||
* through the running of this function on all the values in F, | ||
* returning an F[B] in a G context. | ||
* | ||
* Example: | ||
* {{{ | ||
* scala> import cats.implicits._ | ||
* scala> import cats.data.NonEmptyList | ||
* scala> def countWords(words: List[String]): Map[String, Int] = words.groupBy(identity).mapValues(_.length) | ||
* scala> NonEmptyList.of(List("How", "do", "you", "fly"), List("What", "do", "you", "do")).nonEmptyTraverse(countWords) | ||
* res0: Map[String,cats.data.NonEmptyList[Int]] = Map(do -> NonEmptyList(1, 2), you -> NonEmptyList(1, 1)) | ||
* }}} | ||
*/ | ||
def nonEmptyTraverse[G[_]: Apply, A, B](fa: F[A])(f: A => G[B]): G[F[B]] | ||
|
||
/** | ||
* Thread all the G effects through the F structure to invert the | ||
* structure from F[G[A]] to G[F[A]]. | ||
* | ||
* Example: | ||
* {{{ | ||
* scala> import cats.implicits._ | ||
* scala> import cats.data.NonEmptyList | ||
* scala> val x = NonEmptyList.of(Map("do" -> 1, "you" -> 1), Map("do" -> 2, "you" -> 1)) | ||
* scala> val y = NonEmptyList.of(Map("How" -> 3, "do" -> 1, "you" -> 1), Map[String,Int]()) | ||
* scala> x.nonEmptySequence | ||
* res0: Map[String,NonEmptyList[Int]] = Map(do -> NonEmptyList(1, 2), you -> NonEmptyList(1, 1)) | ||
* scala> y.nonEmptySequence | ||
* res1: Map[String,NonEmptyList[Int]] = Map() | ||
* }}} | ||
*/ | ||
def nonEmptySequence[G[_]: Apply, A](fga: F[G[A]]): G[F[A]] = | ||
nonEmptyTraverse(fga)(identity) | ||
|
||
|
||
/** | ||
* A nonEmptyTraverse followed by flattening the inner result. | ||
* | ||
* Example: | ||
* {{{ | ||
* scala> import cats.implicits._ | ||
* scala> import cats.data.NonEmptyList | ||
* scala> def countWords(words: List[String]): Map[String, Int] = words.groupBy(identity).mapValues(_.length) | ||
* scala> val x = NonEmptyList.of(List("How", "do", "you", "fly"), List("What", "do", "you", "do")) | ||
* scala> x.nonEmptyFlatTraverse(_.groupByNel(identity)) | ||
* res0: Map[String,cats.data.NonEmptyList[String]] = Map(do -> NonEmptyList(do, do, do), you -> NonEmptyList(you, you)) | ||
* }}} | ||
*/ | ||
def nonEmptyFlatTraverse[G[_], A, B](fa: F[A])(f: A => G[F[B]])(implicit G: Apply[G], F: FlatMap[F]): G[F[B]] = | ||
G.map(nonEmptyTraverse(fa)(f))(F.flatten) | ||
|
||
/** | ||
* Thread all the G effects through the F structure and flatten to invert the | ||
* structure from F[G[F[A]]] to G[F[A]]. | ||
* | ||
* Example: | ||
* {{{ | ||
* scala> import cats.implicits._ | ||
* scala> import cats.data.NonEmptyList | ||
* scala> val x = NonEmptyList.of(Map(0 ->NonEmptyList.of(1, 2)), Map(0 -> NonEmptyList.of(3))) | ||
* scala> val y: NonEmptyList[Map[Int, NonEmptyList[Int]]] = NonEmptyList.of(Map(), Map(1 -> NonEmptyList.of(3))) | ||
* scala> x.nonEmptyFlatSequence | ||
* res0: Map[Int,cats.data.NonEmptyList[Int]] = Map(0 -> NonEmptyList(1, 2, 3)) | ||
* scala> y.nonEmptyFlatSequence | ||
* res1: Map[Int,cats.data.NonEmptyList[Int]] = Map() | ||
* }}} | ||
*/ | ||
def nonEmptyFlatSequence[G[_], A](fgfa: F[G[F[A]]])(implicit G: Apply[G], F: FlatMap[F]): G[F[A]] = | ||
G.map(nonEmptyTraverse(fgfa)(identity))(F.flatten) | ||
|
||
override def traverse[G[_] : Applicative, A, B](fa: F[A])(f: (A) => G[B]): G[F[B]] = | ||
nonEmptyTraverse(fa)(f) | ||
|
||
def compose[G[_]: NonEmptyTraverse]: NonEmptyTraverse[λ[α => F[G[α]]]] = | ||
new ComposedNonEmptyTraverse[F, G] { | ||
val F = self | ||
val G = NonEmptyTraverse[G] | ||
} | ||
|
||
|
||
} |
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
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
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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package cats | ||
package syntax | ||
|
||
trait NonEmptyTraverseSyntax extends NonEmptyTraverse.ToNonEmptyTraverseOps |
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.
Can you add the implementation of
traverse
used in theTraverse
instance above here as well?