-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Add Syntax For Enumerable #4347
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
isomarcte
wants to merge
2
commits into
typelevel:main
Choose a base branch
from
isomarcte:enumerable-syntax
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.
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 |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
* Copyright (c) 2015 Typelevel | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
* this software and associated documentation files (the "Software"), to deal in | ||
* the Software without restriction, including without limitation the rights to | ||
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | ||
* the Software, and to permit persons to whom the Software is furnished to do so, | ||
* subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
*/ | ||
|
||
package cats | ||
package syntax | ||
|
||
import cats.kernel._ | ||
|
||
trait PartialNextSyntax { | ||
implicit final def catsSyntaxPartialNext[A](a: A): PartialNextOps[A] = | ||
new PartialNextOps(a) | ||
} | ||
|
||
final class PartialNextOps[A](private val lhs: A) extends AnyVal { | ||
def partialNext(implicit A: PartialNext[A]): Option[A] = | ||
A.partialNext(lhs) | ||
} | ||
|
||
trait NextSyntax extends PartialNextSyntax { | ||
implicit final def catsSyntaxNext[A](a: A): NextOps[A] = | ||
new NextOps(a) | ||
} | ||
|
||
final class NextOps[A](private val lhs: A) extends AnyVal { | ||
def next(implicit A: Next[A]): A = | ||
A.next(lhs) | ||
} | ||
|
||
trait PartialPreviousSyntax { | ||
implicit final def catsSyntaxPartialPrevious[A](a: A): PartialPreviousOps[A] = | ||
new PartialPreviousOps(a) | ||
} | ||
|
||
final class PartialPreviousOps[A](private val lhs: A) extends AnyVal { | ||
def partialPrevious(implicit A: PartialPrevious[A]): Option[A] = | ||
A.partialPrevious(lhs) | ||
} | ||
|
||
trait PreviousSyntax extends PartialPreviousSyntax { | ||
implicit final def catsSyntaxPrevious[A](a: A): PreviousOps[A] = | ||
new PreviousOps(a) | ||
} | ||
|
||
final class PreviousOps[A](private val lhs: A) extends AnyVal { | ||
def previous(implicit A: Previous[A]): A = | ||
A.previous(lhs) | ||
} | ||
|
||
trait BoundedEnumerableSyntax extends NextSyntax with PreviousSyntax { | ||
implicit final def catsSyntaxForBoundedEnumerable[A](a: A): BoundedEnumerableOps[A] = | ||
new BoundedEnumerableOps(a) | ||
} | ||
|
||
final class BoundedEnumerableOps[A](private val lhs: A) extends AnyVal { | ||
def cycleNext(implicit A0: PartialNext[A], A1: LowerBounded[A]): A = | ||
A0.partialNext(lhs).getOrElse(A1.minBound) | ||
|
||
def cyclePrevious(implicit A0: PartialPrevious[A], A1: UpperBounded[A]): A = | ||
A0.partialPrevious(lhs).getOrElse(A1.maxBound) | ||
} |
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
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.
I am a bit concerned that the syntax class implements its own logic.
There's
BoundedEnumerable
typelclass already that implementscycleNext
andcyclePrevious
.Usually the syntax should only do
shouldn't it?
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.
...on the other hand, I think I see what you meant:
BoundedEnumerable
is too restrictive for that.And I think this is because the entire hierarchy is sort of not that well-developed yet. Perhaps, there should be two more typeclasses added:
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.
Actually, there's a couple classes like that:
PartialPreviousUpperBounded
andPartialNextLowerBounded
, but those do not seem to be well-developed either:cats/kernel/src/main/scala-2.13+/cats/kernel/EnumerableCompat.scala
Lines 28 to 42 in bdb1ee0
I wonder, why does it inherit to
PartialNext
if it does not use anything from the latter?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.
@satorg Yeah, there are some issues here.
So, first off, RE the syntax classes having logic. I don't want to demand a constraint of
BoundedEnumerable
because someone might define aPartialNext
andLowerBounded
as separate definitions and then we won't be able to solve for the instance, e.g. likeDecoder
Encoder
in circe orGet
Put
in Doobie.Secondly, I think there is a more complete and less restrictive encoding for what
PartialPreviousUpperBounded
andPartialNextLowerBouneded
are trying to do. I'm prototyping it out here: isomarcte@9debe1c#diff-805a1fb053663f9d27cfcb8586bc2310cbe9a649366549b8d0106a366774e2e3R45Keep in mind, that is a rough early draft of what I'm calling
Countable
(should probably just be calledEnumerable
). It can be defined for both finite and infinite types, unlikePartialPreviousUpperBounded
.I'm trying to work out how the whole hierarchy should look.
The
cycleNext
andcyclePrevious
functions are interesting. Technically, we could define these for unbounded (infinite) types as well, they would just be aliases fornext
andprevious
. That is to say, the cycle over an infinite domain, is just a cycle which never wraps. This might be nice if someone wanted to endlessly enumerate over a domain, not caring if they were cycling or just going on forever. 🤷Countable
(orEnumerable
) would be really helpful for defining anInterval
type. The stdlib Range and cats-collection Range is sort of trying to be both a convenient way to enumerate a series of elements and also a true mathematical interval, with the stdlib version leaning a bit more into the former and cats-collection's version the latter. I think that is why we have some ergonomic issues with those types. However if we defineCountable
separately then we can solve the "I want to enumerate over some set of values problem" and having that as a constraint also makes writingInterval
a lot nicer. Specifically, saying that anInterval
is something that maps to the natural numbers opens up a whole lot of optimizations and ergonomic benefits.Sorry, I got a little off topic there. All that
Countable
stuff would be another PR once I've sorted out how I think it might all fit together with the existing classes. For now, I just wanted to get the syntax in place for what is already defined, with minimal constraints.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 appreciate you deep involvements into the topic. Actually, I think you already have a suggestion on how the concern could be solved, don't you?
cats/kernel/src/main/scala/cats/kernel/Enumerable.scala
Lines 256 to 257 in be74ffc