-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
docs: improve Func and AppFunc documentation #4890
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
Open
mmustafasenoglu
wants to merge
2
commits into
typelevel:main
Choose a base branch
from
mmustafasenoglu:docs/func-documentation
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.
+316
−5
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -8,6 +8,7 @@ laika.navigationOrder = [ | |
| eval.md | ||
| freeapplicative.md | ||
| freemonad.md | ||
| func.md | ||
| functionk.md | ||
| id.md | ||
| ior.md | ||
|
|
||
This file contains hidden or 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,159 @@ | ||
| # Func | ||
|
|
||
| API Documentation: @:api(cats.data.Func) | ||
|
|
||
| `Func` is a function `A => F[B]`, where `F` is a functor. It is similar to `Kleisli`, but with weaker requirements: while `Kleisli` requires `FlatMap` (or `Monad`) for sequential composition, `Func` only requires `Functor` and supports parallel (applicative) composition via `AppFunc`. | ||
|
|
||
| ## Kleisli vs Func | ||
|
|
||
| The key difference is in how they compose: | ||
|
|
||
| - **Kleisli** is for ''sequential'' composition: `A => F[B]` followed by `B => F[C]` requires the output of the first to feed into the second, and needs `FlatMap` to chain them. | ||
| - **Func** is for ''parallel'' composition: given `A => F[B]` and `A => F[C]`, we can combine them into `A => F[(B, C)]` using only `Applicative#product`. | ||
|
|
||
| This is useful when you have independent effects that don't depend on each other's results. | ||
|
|
||
| ```scala mdoc:silent | ||
| import cats.data._ | ||
| import cats.syntax.all._ | ||
|
|
||
| val parseInt: Func[Option, String, Int] = | ||
| Func.func(s => scala.util.Try(s.toInt).toOption) | ||
|
|
||
| val parseDouble: Func[Option, String, Double] = | ||
| Func.func(s => scala.util.Try(s.toDouble).toOption) | ||
|
|
||
| // Compose in parallel: parse as Int and Double independently | ||
| val combined: Func[Option, String, (Int, Double)] = | ||
| parseInt.product(parseDouble) | ||
|
|
||
| combined.run("42") // Some((42, 42.0)) | ||
| ``` | ||
|
|
||
| ## Func | ||
|
|
||
| At its core, `Func[F[_], A, B]` wraps a function `A => F[B]`. Depending on the properties of `F[_]`, we can do different things: | ||
|
|
||
| ```scala mdoc:silent | ||
| val f: Func[Option, Int, Int] = Func.func(i => Some(i + 1)) | ||
|
|
||
| // map only requires Functor | ||
| f.map(_ * 10).run(5) // Some(60) | ||
| ``` | ||
|
|
||
| ### Methods | ||
|
|
||
| ``` | ||
| Method | Constraint on `F[_]` | ||
| --------- | ------------------- | ||
| map | Functor | ||
| mapK | (none - uses natural transformation) | ||
| product | Applicative (via AppFunc) | ||
| ``` | ||
|
|
||
| ## AppFunc | ||
|
|
||
| `AppFunc[F[_], A, B]` is the more powerful version of `Func`, requiring an `Applicative` instance for `F`. It supports parallel composition via `product`, `compose`, `andThen`, and `traverse`. | ||
|
|
||
| ```scala mdoc:silent | ||
| val validateName: AppFunc[Either[String, *], Config, String] = | ||
| Func.appFunc(c => if (c.name.nonEmpty) Right(c.name) else Left("Name required")) | ||
|
|
||
| val validateAge: AppFunc[Either[String, *], Config, Int] = | ||
| Func.appFunc(c => if (c.age > 0) Right(c.age) else Left("Invalid age")) | ||
|
|
||
| // Both validators run independently | ||
| val combined = validateName.product(validateAge) | ||
| ``` | ||
|
|
||
| Note: the example above uses kind-projector syntax (`Either[String, *]`). | ||
|
|
||
| ### Composition | ||
|
|
||
| `AppFunc` supports several composition patterns: | ||
|
|
||
| #### product | ||
|
|
||
| Combine two `AppFunc`s that share the same input type, producing a tuple of results: | ||
|
|
||
| ```scala mdoc:silent | ||
| val f: AppFunc[Option, Int, Int] = Func.appFunc(i => Some(i + 1)) | ||
| val g: AppFunc[Option, Int, String] = Func.appFunc(i => Some(i.toString)) | ||
|
|
||
| f.product(g).run(42) // Some((43, "42")) | ||
| ``` | ||
|
|
||
| #### compose and andThen | ||
|
|
||
| Compose two `AppFunc`s where the output of one feeds into the other: | ||
|
|
||
| ```scala mdoc:silent | ||
| val parse: AppFunc[Option, String, Int] = | ||
| Func.appFunc(s => scala.util.Try(s.toInt).toOption) | ||
|
|
||
| val double: AppFunc[Option, Int, Int] = | ||
| Func.appFunc(i => Some(i * 2)) | ||
|
|
||
| // andThen: parse first, then double | ||
| val parseThenDouble: AppFunc[Nested[Option, Option, *], String, Int] = | ||
| parse.andThen(double) | ||
|
|
||
| parseThenDouble.run("21") // Nested(Some(Some(42))) | ||
| ``` | ||
|
|
||
| #### traverse | ||
|
|
||
| Apply an `AppFunc` to each element of a traversable structure: | ||
|
|
||
| ```scala mdoc:silent | ||
| val parse: AppFunc[Option, String, Int] = | ||
| Func.appFunc(s => scala.util.Try(s.toInt).toOption) | ||
|
|
||
| parse.traverse(List("1", "2", "3")) // Some(List(1, 2, 3)) | ||
| parse.traverse(List("1", "x", "3")) // None | ||
| ``` | ||
|
|
||
| ### Methods | ||
|
|
||
| ``` | ||
| Method | Constraint on `F[_]` | ||
| --------- | ------------------- | ||
| map | (none - Applicative already implies Functor) | ||
| product | Applicative | ||
| compose | Applicative | ||
| andThen | Applicative | ||
| traverse | Applicative | ||
| ``` | ||
|
|
||
| ## Type class instances | ||
|
|
||
| `Func[F, C, *]` has the following type class instances depending on what `F[_]` has: | ||
|
|
||
| ``` | ||
| Type class | Constraint on `F[_]` | ||
| -------------- | ------------------- | ||
| Functor | Functor | ||
| Apply | Apply | ||
| Applicative | Applicative | ||
| ``` | ||
|
|
||
| `Func[F, *, C]` (contravariant in the input type) has: | ||
|
|
||
| ``` | ||
| Type class | Constraint on `F[_]` | ||
| -------------- | ------------------- | ||
| Contravariant | Contravariant | ||
| ``` | ||
|
|
||
| ## When to use Func vs Kleisli | ||
|
|
||
| - Use **Kleisli** when you need sequential composition: the output of one function feeds into the next, and you need `flatMap`. | ||
| - Use **Func/AppFunc** when you have independent effects that can run in parallel: you only need `Applicative`, not `Monad`. | ||
|
|
||
| In practice, `Kleisli` is more commonly used because most real-world effects are sequential. However, `Func`/`AppFunc` can be more efficient for parallel validation, parallel data fetching, or any scenario where independent effects can be composed without dependencies. | ||
|
|
||
| ## Further reading | ||
|
|
||
| - [The Essence of the Iterator Pattern](https://www.cs.ox.ac.uk/jeremy.gibbons/publications/iterator.pdf) - the paper that inspired `Func` | ||
| - @:api(cats.data.Kleisli) - the sequential counterpart | ||
| - @:api(cats.data.Nested) - used in `compose`/`andThen` return types |
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.
This doesn't really parse and double the input, it parses it into both
IntandDoublesimultaneously.