-
-
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 Align typeclass #3076
Merged
Add Align typeclass #3076
Changes from 7 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
2523014
Add Align typeclass
LukaJCB 909a33a
Scalafmt
LukaJCB c5084bf
Add Const and Validated instances and derivation from SemigroupK and …
LukaJCB fb6b0c6
Merge branch 'master' into align
LukaJCB 953a99f
Merge branch 'master' into align
LukaJCB 006a23c
Add MiMaException tests
LukaJCB 48eaa14
scalafmt
LukaJCB 7a772b4
Merge branch 'master' into align
LukaJCB e249b96
Use lazy iterator for align
LukaJCB 6e7d2b4
Add doctests and Either tests
LukaJCB 5be6a86
Address feedback
LukaJCB 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ TAGS | |
.idea/* | ||
.idea_modules | ||
.DS_Store | ||
.vscode | ||
.sbtrc | ||
*.sublime-project | ||
*.sublime-workspace | ||
|
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,6 @@ | ||
package cats.compat | ||
|
||
private[cats] object Vector { | ||
def zipWith[A, B, C](fa: Vector[A], fb: Vector[B])(f: (A, B) => C): Vector[C] = | ||
(fa, fb).zipped.map(f) | ||
} |
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,6 @@ | ||
package cats.compat | ||
|
||
private[cats] object Vector { | ||
def zipWith[A, B, C](fa: Vector[A], fb: Vector[B])(f: (A, B) => C): Vector[C] = | ||
fa.lazyZip(fb).map(f) | ||
} |
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,53 @@ | ||
package cats | ||
|
||
import simulacrum.typeclass | ||
|
||
import cats.data.Ior | ||
|
||
/** | ||
* `Align` supports zipping together structures with different shapes, | ||
* holding the results from either or both structures in an `Ior`. | ||
* | ||
* Must obey the laws in cats.laws.AlignLaws | ||
*/ | ||
@typeclass trait Align[F[_]] { | ||
|
||
def functor: Functor[F] | ||
LukaJCB marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/** | ||
* Pairs elements of two structures along the union of their shapes, using `Ior` to hold the results. | ||
* | ||
* Align[List].align(List(1, 2), List(10, 11, 12)) = List(Ior.Both(1, 10), Ior.Both(2, 11), Ior.Right(12)) | ||
*/ | ||
def align[A, B](fa: F[A], fb: F[B]): F[Ior[A, B]] | ||
|
||
/** | ||
* Combines elements similarly to `align`, using the provided function to compute the results. | ||
*/ | ||
def alignWith[A, B, C](fa: F[A], fb: F[B])(f: Ior[A, B] => C): F[C] = | ||
functor.map(align(fa, fb))(f) | ||
|
||
/** | ||
* Align two structures with the same element, combining results according to their semigroup instances. | ||
*/ | ||
def alignCombine[A: Semigroup](fa1: F[A], fa2: F[A]): F[A] = | ||
alignWith(fa1, fa2)(_.merge) | ||
|
||
/** | ||
* Same as `align`, but forgets from the type that one of the two elements must be present. | ||
*/ | ||
def padZip[A, B](fa: F[A], fb: F[B]): F[(Option[A], Option[B])] = | ||
alignWith(fa, fb)(_.pad) | ||
|
||
/** | ||
* Same as `alignWith`, but forgets from the type that one of the two elements must be present. | ||
*/ | ||
def padZipWith[A, B, C](fa: F[A], fb: F[B])(f: (Option[A], Option[B]) => C): F[C] = | ||
alignWith(fa, fb)(ior => Function.tupled(f)(ior.pad)) | ||
LukaJCB marked this conversation as resolved.
Show resolved
Hide resolved
LukaJCB marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
object Align { | ||
def semigroup[F[_]: Align, A: Semigroup]: Semigroup[F[A]] = new Semigroup[F[A]] { | ||
LukaJCB marked this conversation as resolved.
Show resolved
Hide resolved
|
||
def combine(x: F[A], y: F[A]): F[A] = Align[F].alignCombine(x, y) | ||
} | ||
} |
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
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.
Are we sure this doesn't break BC? If we are sure maybe add a test in
binCompatTest
? If not can we just add a separate method to provide the instance forAlign
?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.
I added some tests there, I think that should suffice, WDYT?
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.
I think that shall do. thanks!
On a slightly related note, since we have been updating the build, I am a bit nervous that if something breaks
binCompatTest
, we wouldn't know. I'll create an issue.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 just makes me vaguely uncomfortable. It's only used twice and it seems like it'd be almost equivalently noisy just to write out the
new AbstractNonEmptyInstances[F, NEF] with Align[NEF]
in those two places.I know we don't promise bincompat for people using Java, etc., but breaking it for the sake of saving a few lines doesn't feel ideal.