-
-
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
Docs for Func and AppFunc #4378
Open
UlisesTorrella
wants to merge
16
commits into
typelevel:main
Choose a base branch
from
UlisesTorrella:doc-for-func
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.
Open
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
deb9ee3
a bit of comentary on data.Func
UlisesTorrella 7954176
first draft
UlisesTorrella e0999bf
func and appfunc datatype docs
UlisesTorrella 6036cdd
renaming and adding a link to AppFunc doc
UlisesTorrella 700099b
bit of rewording in AppFunc docs
UlisesTorrella a029422
corrections to Func docs
UlisesTorrella 2cebdb6
Update docs/datatypes/func.md
UlisesTorrella b8613ff
Update docs/datatypes/func.md
UlisesTorrella 06def49
Update docs/datatypes/func.md
UlisesTorrella e73fe8e
refactor in Func documentation capitalized vals
UlisesTorrella 0ea4eff
Update docs/datatypes/func.md
UlisesTorrella aee7268
Update docs/datatypes/func.md
UlisesTorrella 5b0faa9
Include AppFunc in the Func documentation
UlisesTorrella 9e91be0
Merge branch 'main' into doc-for-func
UlisesTorrella 1af3c3b
include links in Func docs and added my name to contributors
UlisesTorrella c815fe0
link to nested
UlisesTorrella 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 |
---|---|---|
@@ -0,0 +1,78 @@ | ||
# Func and AppFunc | ||
|
||
API Documentation: @:api(cats.data.Func) | ||
|
||
Func is a wrapper around a `run` function `A => F[B]` where `F` is a functor. Given that, the Func data type is equipped with the known `map` function, and a `mapK` function to apply natural transformations (from a `Func[F[_], A, B]` get an `Func[G[_], A, B]`). | ||
|
||
The signature `Func[F[_], A, B]` is very similar to the signature for [Kleisli](../datatypes/kleisli.md): `Kleisli[F[_], -A, B]`. The difference is that `Func` is a less restrictive data type that wraps around functors, and only provides basic methods `run`, `map`, and `mapK`, while `Kleisli` is strong enough to provide composition, flatMap, and more. We will see a more useful data type just next with `AppFunc`. | ||
|
||
## Quick example | ||
|
||
```scala mdoc:silent:nest | ||
import cats.data.{ Func, AppFunc } | ||
import cats._ | ||
|
||
val f: Func[List, Int, String] = Func.func((x: Int) => List(x.toString)) | ||
|
||
val g: Func[List, Int, Option[String]] = f.map((x: String) => if (x=="0") None else Some(x)) | ||
|
||
val optToList = new (Option ~> List) { | ||
def apply[T](opt: Option[T]): List[T] = | ||
opt.toList | ||
} | ||
|
||
// We transform the elements of List, of type | ||
// Option[String] to List[String] | ||
g.map(optToList(_)) | ||
// val res0: cats.data.Func[List,Int,List[String]] = ... | ||
``` | ||
|
||
|
||
|
||
# AppFunc | ||
|
||
AppFunc extends Func to wrap around a special type of functor: [Applicative] functors. | ||
|
||
With applicative functors we can `compose`, form the `product`, and also `traverse` traversable functors | ||
|
||
Signature: `AppFunc[F[_], A, B] extends Func[F, A, B]` | ||
|
||
Now, for the reader familiar with [Kleisli](../datatypes/kleisli.md), we find an even more similar data type. `AppFunc` provides compositions of weaker constraint, allowing `AppFunc[F[_], A, B]` to be composed with `AppFunc[G[_], C, A]`. | ||
## Composition | ||
|
||
All of functional programming revolves around composing, and functors cannot be left behind. If we are working with multiple contexts we might want to compose them, for example: we want to `List` things, and discard some (`Option`). | ||
|
||
To achieve this nested context behavior `AppFunc` uses the [`Nested`] datatype. | ||
|
||
```scala mdoc:silent:nest | ||
val appFuncOption: AppFunc[Option,Int,Int] = Func.appFunc((i: Int) => if (i==0) None else Some(i)) | ||
|
||
val appFuncList: AppFunc[List,Int,Int] = Func.appFunc((o: Int) => {List(o+1)}) | ||
(appFuncOption andThen appFuncList).run(1) //Nested(Some(List(2))) | ||
|
||
(appFuncOption andThen appFuncList).run(0) //Nested(None) | ||
// same thing with compose | ||
|
||
(appFuncList compose appFuncOption) | ||
``` | ||
## Product | ||
|
||
Applicative functors, like monads, are closed under product. Cats models product of two applicative functors (they can be different!) in the @:api(cats.data.Tuple2K) data type. | ||
|
||
For further reading: [herding cats](http://eed3si9n.com/herding-cats/combining-applicative.html#Product+of+applicative+functions) | ||
|
||
```scala mdoc:silent:nest | ||
(appFuncOption product appFuncList).run(1) | ||
``` | ||
## Traverse | ||
|
||
This explained in the implementation of the Applicative trait: [Applicative - Traverse](https://typelevel.org/cats/typeclasses/applicative.html#traverse) | ||
|
||
```scala mdoc:silent:nest | ||
appFuncOption.traverse(List(1,2,3)) | ||
// val res14: Option[List[Int]] = Some(List(1, 2, 3)) | ||
|
||
appFuncOption.traverse(List(1,2,0)) | ||
//val res15: Option[List[Int]] = None | ||
``` | ||
|
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.
Does just
[Kleisli]
without an explicit link not work?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.
Nope, I got an exception when running the preview. I got this explicit from another doc doing the same, must be for the same reason. Idk why it says the Klesli reference is "ambiguous"
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.
Wierd. Too bad. Maybe 2 sections called "Kleisli" somewhere?