-
-
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 NonEmptyTraverse docs #1787
Merged
peterneyens
merged 6 commits into
typelevel:master
from
LukaJCB:add-non-empty-traverse-docs
Oct 11, 2017
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
46e7be6
Add NonEmptyTraverse documentation
74f67c1
Initial version of nonEmptyTraverse docs
6b9c357
Add menu entry
631c4fb
Add links to other typeclasses
156c14f
Remove misplaced backtick
bfa0685
Merge branch 'master' into add-non-empty-traverse-docs
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,47 @@ | ||
--- | ||
layout: docs | ||
title: "NonEmptyTraverse" | ||
section: "typeclasses" | ||
source: "core/src/main/scala/cats/NonEmptyTraverse.scala" | ||
scaladoc: "#cats.NonEmptyTraverse" | ||
--- | ||
|
||
# NonEmptyTraverse` | ||
|
||
`NonEmptyTraverse` is a non-empty version of the [Traverse](traverse.html) type class, just like [Reducible](reducible.html) is a non-empty version of [Foldable](foldable.html)`. | ||
As such, it extends both `Reducible` and `Traverse` in the type class hierarchy. | ||
It provides the `nonEmptyTraverse` and `nonEmptySequence` methods that require an instance of `Apply` instead of `Applicative`: | ||
|
||
```scala | ||
def nonEmptyTraverse[G[_]: Apply, A, B](fa: F[A])(f: A => G[B]): G[F[B]] | ||
|
||
def nonEmptySequence[G[_]: Apply, A](fga: F[G[A]]): G[F[A]] | ||
``` | ||
|
||
In the [Applicative tutorial](applicative.html) we learned of `Apply` as a weakened `Applicative` lacking the `pure` method. | ||
One example type lacking an `Applicative` instance is `Map[K, ?]`, it's impossible to implement a `pure` method for it. | ||
|
||
Knowing this, we can make use of `NonEmptyTraverse`, to traverse over a sequence of `Map`s. | ||
One example application one could think of is, when we have a list of text snippets, | ||
count the occurrence of each word in each snippet and return all the common words and their occurrences in each snippet: | ||
|
||
```tut:book | ||
import cats.implicits._ | ||
import cats.data.NonEmptyList | ||
|
||
val snippets = NonEmptyList.of("What do you do", "What are you doing") | ||
|
||
def countWords(text: String): Map[String, Int] = | ||
text.split(" ").groupBy(identity).mapValues(_.length) | ||
|
||
snippets.nonEmptyTraverse(countWords) | ||
``` | ||
|
||
Note that, just like `traverse`, `nonEmptyTraverse(f)` is equivalent to `map(f).nonEmptySequence`, so the above could be rewritten as: | ||
|
||
```tut:book | ||
snippets.map(countWords).nonEmptySequence | ||
``` | ||
|
||
`NonEmptyTraverse` also offers `flatNonEmptyTraverse` and `flatNonEmptySequence` methods that are analogous to `flatTraverse` and `flatSequence` in `Traverse`. | ||
Just like with `nonEmptyTraverse` these methods don't require a `Monad` instance, but only a `FlatMap`, which is the weakened version of `Monad` without `pure`. |
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 think that there's a dangling backtick on this line.