-
-
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 UnorderedFoldable and UnorderedTraverse #1981
Merged
Merged
Changes from all commits
Commits
Show all changes
41 commits
Select commit
Hold shift + click to select a range
0ea4ac0
Add CommutativeApply and Applicative and an instance for Validated[Co…
0cd4eb9
Make Future a CommutativeApplicative
f9a239a
Revert "Make Future a CommutativeApplicative"
965f9c2
Add unordered traversal for Sets
d3a803a
Test commutativeApply instances + Nested + Tuple2K instances
1ab4f47
Add unordered traversal for Map
2164b51
Fix priority for Nested instances
7f1f6c7
Fix priority for Tuple2K instances
75a656a
Merge branch 'master' into add-commutative-apply
69c6374
Merge branch 'master' into add-commutative-apply
fa8a731
Move methods to typeclass
2aeddd7
Deduplicate Applicative instance
aa49de9
Try out new typeclasses
d308cf7
Add UnorderedFoldable and UnorderedTraverse and move traversal functi…
148c113
Merge branch 'master' into add-commutative-apply
0ffbc55
Remove Set from Foldable docs
23cb589
Add extra ref law
e0069a8
Merge branch 'master' into add-commutative-apply
376a5c6
Revert "Add UnorderedFoldable and UnorderedTraverse and move traversa…
d673655
Revert "Remove Set from Foldable docs"
8666c64
Add Unordered type classes
74b0228
Make UnorderedFoldable and Traverse super classes
dfa8bd3
Make unorderedFoldMap the main method
903694d
define isEmpty in terms of exists
821424d
Merge branch 'master' into add-unordered-classes
a1cdf2d
Merge branch 'master' into add-unordered-classes
cdc6586
Merge branch 'add-unordered-classes' of https://github.com/LukaJCB/ca…
3e705d4
Merge branch 'master' into add-unordered-classes
5f4d41f
Add mention to unorderedFoldable in foldable scaladoc
cd2e1f5
Merge branch 'master' into add-unordered-classes
kailuowang 5d9f0f7
Add size to UnorderedFoldable
f70fe6f
Merge branch 'add-unordered-classes' of https://github.com/LukaJCB/ca…
04722f0
Made exists and forall sufficiently lazy
9568933
Add Mima exceptions
9666a0c
Merge branch 'master' into add-unordered-classes
23a6ca1
Merge branch 'master' into add-unordered-classes
d2037e6
Merge branch 'master' into add-unordered-classes
kailuowang 11b397c
Fix merging mistake
89cab23
Remove toSet
e2984b4
Merge branch 'add-unordered-classes' of https://github.com/LukaJCB/ca…
ea38cd6
Remove unused imports
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
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,71 @@ | ||
package cats | ||
|
||
import cats.kernel.CommutativeMonoid | ||
import simulacrum.typeclass | ||
import cats.instances.long._ | ||
/** | ||
* `UnorderedFoldable` is like a `Foldable` for unordered containers. | ||
*/ | ||
@typeclass trait UnorderedFoldable[F[_]] { | ||
|
||
def unorderedFoldMap[A, B: CommutativeMonoid](fa: F[A])(f: A => B): B | ||
|
||
def unorderedFold[A: CommutativeMonoid](fa: F[A]): A = | ||
unorderedFoldMap(fa)(identity) | ||
|
||
|
||
/** | ||
* Returns true if there are no elements. Otherwise false. | ||
*/ | ||
def isEmpty[A](fa: F[A]): Boolean = | ||
exists(fa)(Function.const(true)) | ||
|
||
def nonEmpty[A](fa: F[A]): Boolean = | ||
!isEmpty(fa) | ||
|
||
/** | ||
* Check whether at least one element satisfies the predicate. | ||
* | ||
* If there are no elements, the result is `false`. | ||
*/ | ||
def exists[A](fa: F[A])(p: A => Boolean): Boolean = | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be implemented with unorderedFoldMap |
||
unorderedFoldMap(fa)(a => Eval.later(p(a)))(UnorderedFoldable.commutativeMonoidEval(UnorderedFoldable.orMonoid)) | ||
.value | ||
|
||
/** | ||
* Check whether all elements satisfy the predicate. | ||
* | ||
* If there are no elements, the result is `true`. | ||
*/ | ||
def forall[A](fa: F[A])(p: A => Boolean): Boolean = | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be implemented with foldMap using and monoid. |
||
unorderedFoldMap(fa)(a => Eval.later(p(a)))(UnorderedFoldable.commutativeMonoidEval(UnorderedFoldable.andMonoid)) | ||
.value | ||
|
||
/** | ||
* The size of this UnorderedFoldable. | ||
* | ||
* This is overriden in structures that have more efficient size implementations | ||
* (e.g. Vector, Set, Map). | ||
* | ||
* Note: will not terminate for infinite-sized collections. | ||
*/ | ||
def size[A](fa: F[A]): Long = unorderedFoldMap(fa)(_ => 1L) | ||
} | ||
|
||
object UnorderedFoldable { | ||
private val orMonoid: CommutativeMonoid[Boolean] = new CommutativeMonoid[Boolean] { | ||
val empty: Boolean = false | ||
|
||
def combine(x: Boolean, y: Boolean): Boolean = x || y | ||
} | ||
|
||
private val andMonoid: CommutativeMonoid[Boolean] = new CommutativeMonoid[Boolean] { | ||
val empty: Boolean = true | ||
|
||
def combine(x: Boolean, y: Boolean): Boolean = x && y | ||
} | ||
|
||
private def commutativeMonoidEval[A: CommutativeMonoid]: CommutativeMonoid[Eval[A]] = | ||
new EvalMonoid[A] with CommutativeMonoid[Eval[A]] { val algebra = Monoid[A] } | ||
|
||
} |
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,13 @@ | ||
package cats | ||
|
||
import simulacrum.typeclass | ||
|
||
/** | ||
* `UnorderedTraverse` is like a `Traverse` for unordered containers. | ||
*/ | ||
@typeclass trait UnorderedTraverse[F[_]] extends UnorderedFoldable[F] { | ||
def unorderedTraverse[G[_]: CommutativeApplicative, A, B](sa: F[A])(f: A => G[B]): G[F[B]] | ||
|
||
def unorderedSequence[G[_]: CommutativeApplicative, A](fga: F[G[A]]): G[F[A]] = | ||
unorderedTraverse(fga)(identity) | ||
} |
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
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 can be implemented with foldMap (map to true, do the commutative “or” monoid