-
-
Notifications
You must be signed in to change notification settings - Fork 75
Add option to use underscore symbol _ instead of * to define anonymous type lambdas
#188
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
Merged
larsrh
merged 5 commits into
typelevel:main
from
7mind:feature/Yunderscore-placeholders
May 15, 2021
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
adbd336
Add option to use underscore symbol `_` instead of `*` to define anon…
neko-kai 02ab0b5
Do not accidentally filter out non-MemberDef trees on Scala <=2.10
neko-kai 1556638
Remove 2.10 support from the build
neko-kai 16dedbd
Remove cruft induced by 2.10 support
neko-kai 7e78552
Regenerate workflows (drop 2.10)
neko-kai 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
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,10 @@ | ||
| package d_m | ||
|
|
||
| import scala.tools.nsc.plugins.Plugin | ||
|
|
||
| trait PluginOptionsCompat { | ||
| def pluginOptions(plugin: Plugin) = plugin.options | ||
| } | ||
|
|
||
| //compatibility stub | ||
| trait PluginCompat |
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,16 @@ | ||
| package d_m | ||
|
|
||
| import scala.tools.nsc.plugins.Plugin | ||
|
|
||
| trait PluginOptionsCompat { | ||
| def pluginOptions(plugin: Plugin) = plugin.asInstanceOf[PluginCompat].options | ||
| } | ||
|
|
||
| trait PluginCompat extends Plugin { | ||
| var options: List[String] = _ | ||
| override def processOptions(options: List[String], error: String => Unit): Unit = { | ||
| this.options = options | ||
| init(options, error) | ||
| } | ||
| def init(options: List[String], error: String => Unit): Boolean | ||
| } |
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
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,13 @@ | ||
| package underscores | ||
|
|
||
| trait Functor[M[_]] { | ||
| def fmap[A, B](fa: M[A])(f: A => B): M[B] | ||
| } | ||
|
|
||
| class EitherRightFunctor[L] extends Functor[Either[L, _]] { | ||
| def fmap[A, B](fa: Either[L, A])(f: A => B): Either[L, B] = | ||
| fa match { | ||
| case Right(a) => Right(f(a)) | ||
| case Left(l) => Left(l) | ||
| } | ||
| } |
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,23 @@ | ||
| package underscores | ||
|
|
||
| trait ~~>[A[_[_]], B[_[_]]] { | ||
| def apply[X[_]](a: A[X]): B[X] | ||
| } | ||
|
|
||
| trait Bifunctor[F[_[_[_]], _[_[_]]]] { | ||
| def bimap[A[_[_]], B[_[_]], C[_[_]], D[_[_]]](fab: F[A, B])(f: A ~~> C, g: B ~~> D): F[C, D] | ||
| } | ||
|
|
||
| final case class Coproduct[A[_[_]], B[_[_]], X[_]](run: Either[A[X], B[X]]) | ||
|
|
||
| object Coproduct { | ||
| def coproductBifunctor[X[_]]: Bifunctor[Coproduct[_[_[_]], _[_[_]], X]] = | ||
| new Bifunctor[Coproduct[_[_[_]], _[_[_]], X]] { | ||
| def bimap[A[_[_]], B[_[_]], C[_[_]], D[_[_]]](abx: Coproduct[A, B, X])(f: A ~~> C, g: B ~~> D): Coproduct[C, D, X] = | ||
| abx.run match { | ||
| case Left(ax) => Coproduct(Left(f(ax))) | ||
| case Right(bx) => Coproduct(Right(g(bx))) | ||
| } | ||
| } | ||
| def test[X[_]]: Bifunctor[({ type L[F[_[_]], G[_[_]]] = Coproduct[F, G, X] })#L] = coproductBifunctor[X] | ||
| } |
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,13 @@ | ||
| package underscores | ||
|
|
||
| // // From https://github.com/non/kind-projector/issues/20 | ||
| // import scala.language.higherKinds | ||
|
|
||
| object KindProjectorWarnings { | ||
| trait Foo[F[_], A] | ||
| trait Bar[A, B] | ||
|
|
||
| def f[G[_]]: Unit = () | ||
|
|
||
| f[Foo[Bar[Int, _], _]] // shadowing warning | ||
| } |
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,67 @@ | ||
| package underscores | ||
|
|
||
| trait ~>[-F[_], +G[_]] { | ||
| def apply[A](x: F[A]): G[A] | ||
| } | ||
| trait ~>>[-F[_], +G[_]] { | ||
| def dingo[B](x: F[B]): G[B] | ||
| } | ||
| final case class Const[A, B](getConst: A) | ||
|
|
||
| class PolyLambdas { | ||
| type ToSelf[F[_]] = F ~> F | ||
|
|
||
| val kf1 = Lambda[Option ~> Vector](_.iterator.toVector) | ||
|
|
||
| val kf2 = λ[Vector ~> Option] { | ||
| case Vector(x) => Some(x) | ||
| case _ => None | ||
| } | ||
|
|
||
| val kf3 = λ[ToSelf[Vector]](_.reverse) | ||
|
|
||
| val kf4 = λ[Option ~>> Option].dingo(_ flatMap (_ => None)) | ||
|
|
||
| val kf5 = λ[Map[_, Int] ~> Map[_, Long]](_.map { case (k, v) => (k, v.toLong) }.toMap) | ||
|
|
||
| val kf6 = λ[ToSelf[Map[_, Int]]](_.map { case (k, v) => (k, v * 2) }.toMap) | ||
|
|
||
| implicit class FGOps[F[_], A](x: F[A]) { | ||
| def ntMap[G[_]](kf: F ~> G): G[A] = kf(x) | ||
| } | ||
|
|
||
| // Scala won't infer the unary type constructor alias from a | ||
| // tuple. I'm not sure how it even could, so we'll let it slide. | ||
| type PairWithInt[A] = (A, Int) | ||
| def mkPair[A](x: A, y: Int): PairWithInt[A] = x -> y | ||
| val pairMap = λ[ToSelf[PairWithInt]] { case (k, v) => (k, v * 2) } | ||
| val tupleTakeFirst = λ[λ[A => (A, Int)] ~> List](x => List(x._1)) | ||
|
|
||
| // All these formulations should be equivalent. | ||
| def const1[A] = λ[ToSelf[Const[A, _]]](x => x) | ||
| def const2[A] : ToSelf[Const[A, _]] = λ[Const[A, _] ~> Const[A, _]](x => x) | ||
| def const3[A] : Const[A, _] ~> Const[A, _] = λ[ToSelf[Const[A, _]]](x => x) | ||
| def const4[A] = λ[Const[A, _] ~> Const[A, _]](x => x) | ||
| def const5[A] : ToSelf[Const[A, _]] = λ[ToSelf[λ[B => Const[A, B]]]](x => x) | ||
| def const6[A] : Const[A, _] ~> Const[A, _] = λ[ToSelf[λ[B => Const[A, B]]]](x => x) | ||
|
|
||
| @org.junit.Test | ||
| def polylambda(): Unit = { | ||
| assert(kf1(None) == Vector()) | ||
| assert(kf1(Some("a")) == Vector("a")) | ||
| assert(kf1(Some(5d)) == Vector(5d)) | ||
| assert(kf2(Vector(5)) == Some(5)) | ||
| assert(kf3(Vector(1, 2)) == Vector(2, 1)) | ||
| assert(kf4.dingo(Some(5)) == None) | ||
| assert(kf5(Map("a" -> 5)) == Map("a" -> 5)) | ||
| assert(kf6(Map("a" -> 5)) == Map("a" -> 10)) | ||
|
|
||
| assert((mkPair("a", 1) ntMap pairMap) == ("a" -> 2)) | ||
| assert((mkPair(Some(true), 1) ntMap pairMap) == (Some(true) -> 2)) | ||
|
|
||
| assert(mkPair('a', 1).ntMap(tupleTakeFirst) == List('a')) | ||
| // flatten works, whereas it would be a static error in the | ||
| // line above. That's pretty poly! | ||
| assert(mkPair(Some(true), 1).ntMap(tupleTakeFirst).flatten == List(true)) | ||
| } | ||
| } |
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.
Do we need the Scala 2.10 support still? Otherwise I'm inclined to drop this.
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.
@larsrh Do you want me to drop 2.10 build as part of this PR? The
caseis required to compile on 2.10 but is not required otherwise.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.
That would be great!
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.
@larsrh Done in last 2 commits