-
-
Notifications
You must be signed in to change notification settings - Fork 424
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
152 additions
and
88 deletions.
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,24 @@ | ||
`Alternative<F>` allows for propagation of 'failure' and 'choice' (in some appropriate sense, depending on the type), | ||
as well as provision of a unit/identity value (`Empty`). | ||
|
||
`Alternative` is a `Choice` and `MonoidK`, which means it has a `Choose` method, a `Combine` method (which defaults to | ||
calling the `Choose` method), and an `Empty` method. That creates a semantic meaning for `Choose`, which is about | ||
choice propagation rather than the broader meaning of `SemigroupK.Combine`. It also allows for `Choose` and `Combine` | ||
to have separate implementations depending on the type. | ||
|
||
The way to think about `Choose` and the inherited `SemigroupK.Combine` methods is: | ||
* `Choose` is the failure/choice propagation operator: `|` | ||
* `Combine` is the concatenation/combination/addition operator: `+` | ||
|
||
Any type that supports the `Alternative` trait should also implement the `|` operator, to enable easy choice/failure | ||
propagation. If there is a different implementation of `Combine` (rather than accepting the default), then the type | ||
should also implement the `+` operator. | ||
|
||
`AlternativeLaw` can help you test your implementation: | ||
|
||
choose(Pure(a), Pure(b)) = Pure(a) | ||
choose(Empty(), Pure(b)) = Pure(b) | ||
choose(Pure(a), Empty()) = Pure(a) | ||
choose(Empty(), Empty()) = Empty() | ||
|
||
It also tests the `Applicative` and `Functor` laws. |
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,23 @@ | ||
`Choice<F>` allows for propagation of 'failure' and 'choice' (in some appropriate sense, depending on the type). | ||
|
||
`Choice` is a `SemigroupK`, but has a `Choose` method, rather than relying on the `SemigroupK.Combine` method, (which | ||
now has a default implementation of invoking `Choose`). That creates a new semantic meaning for `Choose`, which is | ||
about choice propagation rather than the broader meaning of `Combine`. It also allows for `Choose` and `Combine` to | ||
have separate implementations depending on the type. | ||
|
||
The way to think about `Choose` and the inherited `SemigroupK.Combine` methods is: | ||
* `Choose` is the failure/choice propagation operator: `|` | ||
* `Combine` is the concatenation/combination/addition operator: `+` | ||
|
||
Any type that supports the `Choice` trait should also implement the `|` operator, to enable easy choice/failure | ||
propagation. If there is a different implementation of `Combine` (rather than accepting the default), then the type | ||
should also implement the `+` operator. | ||
|
||
`ChoiceLaw` can help you test your implementation: | ||
|
||
choose(Pure(a), Pure(b)) = Pure(a) | ||
choose(Fail, Pure(b)) = Pure(b) | ||
choose(Pure(a), Fail) = Pure(a) | ||
choose(Fail [1], Fail [2]) = Fail [2] | ||
|
||
It also tests the `Applicative` and `Functor` laws. |
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