-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add UnorderedFoldable and UnorderedTraverse (#1981)
* Add CommutativeApply and Applicative and an instance for Validated[CommutativeSemigroup, ?] * Make Future a CommutativeApplicative * Revert "Make Future a CommutativeApplicative" This reverts commit 0cd4eb9. * Add unordered traversal for Sets * Test commutativeApply instances + Nested + Tuple2K instances * Add unordered traversal for Map * Fix priority for Nested instances * Fix priority for Tuple2K instances * Move methods to typeclass * Deduplicate Applicative instance * Try out new typeclasses * Add UnorderedFoldable and UnorderedTraverse and move traversal functions there * Remove Set from Foldable docs * Add extra ref law * Revert "Add UnorderedFoldable and UnorderedTraverse and move traversal functions there" This reverts commit d308cf7. * Revert "Remove Set from Foldable docs" This reverts commit 0ffbc55. * Add Unordered type classes * Make UnorderedFoldable and Traverse super classes * Make unorderedFoldMap the main method * define isEmpty in terms of exists * Add mention to unorderedFoldable in foldable scaladoc * Add size to UnorderedFoldable * Made exists and forall sufficiently lazy * Add Mima exceptions * Fix merging mistake * Remove toSet * Remove unused imports
- Loading branch information
1 parent
8cff547
commit d4a3cf5
Showing
27 changed files
with
451 additions
and
102 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
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 = | ||
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 = | ||
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.