-
-
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
Support lifting polymorphic functions to FunctionK in Scala 3 #3895
Merged
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e7f04e3
Support for lifting polymorphic functions to FunctionK in Scala 3
bplommer e6493b0
remove unused aliases + import
bplommer f5405a2
Merge branch 'main' into scala3-functionk-lift
armanbilge 0afe43f
Move test to correct directory
armanbilge 88a0fb4
Headers
armanbilge acdf14f
bracketify
armanbilge 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
12 changes: 11 additions & 1 deletion
12
core/src/main/scala-3.x/src/main/scala/cats/arrow/FunctionKMacros.scala
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 |
---|---|---|
@@ -1,4 +1,14 @@ | ||
package cats | ||
package arrow | ||
|
||
private[arrow] class FunctionKMacroMethods | ||
private[arrow] class FunctionKMacroMethods: | ||
/** | ||
* Lifts function `f` of `[X] => F[X] => G[X]` into a `FunctionK[F, G]`. | ||
* | ||
* {{{ | ||
* val headOptionK = FunctionK.lift[List, Option]([X] => (_: List[X]).headOption) | ||
* }}} | ||
*/ | ||
def lift[F[_], G[_]](f: [X] => F[X] => G[X]): FunctionK[F, G] = | ||
new FunctionK[F, G]: | ||
def apply[A](fa: F[A]): G[A] = f(fa) |
16 changes: 16 additions & 0 deletions
16
tests/src/test/scala-3.x/cats/tests/FunctionKLiftSuite.scala
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,16 @@ | ||
package cats.tests | ||
|
||
import cats.arrow.FunctionK | ||
import cats.implicits._ | ||
import org.scalacheck.Prop._ | ||
import cats.laws.discipline.arbitrary._ | ||
|
||
class FunctionKLiftSuite extends CatsSuite { | ||
|
||
test("lift a polymorphic function directly") { | ||
val fHeadOption = FunctionK.lift[List, Option]([X] => (_: List[X]).headOption) | ||
forAll { (a: List[Int]) => | ||
assert(fHeadOption(a) === a.headOption) | ||
} | ||
} | ||
} |
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.
Note that in the future the compiler should support polymorphic sam, so you'll be able to write
val fHeadOption: List ~> Option = [X] => (_: List[X]).headOption
(or maybe even= _.headOption
)