Skip to content
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

Added Align instances for Id and Kleisli #4287

Merged
merged 2 commits into from
Oct 12, 2022

Conversation

atnoya
Copy link
Contributor

@atnoya atnoya commented Aug 30, 2022

This PR adds Align instances for Kleisli and Id.

I realized these two were not available while trying some code like (simplified):

type Env = Map[String, String]
type FromEnvPrefixed[A] = Reader[Env, A]

def prefixed(prefix: String): Reader[Env, Map[String, String]] = {
   Reader { (env: Env) => 
      env.filter(_._1.startsWith(prefix)).foldLeft(Map.empty[String, String]) { case (acc, (k, v)) =>
            val tKey = k.replace(prefix, "")
            acc + (tKey, v)
      }
   }
}

val inConfig = prefixed("label").align(prefixed("label2")).value.run(Map("labelprop1" -> "prop1", "label2prop1" -> "prop1", "labelprop2" -> "prop2")

inConfig.toList.traverse {
   case (k, Ior.Both(one, two)) => Right(k -> (one, two)) //Yeah!!
   case (k, _) => Left(s"Missing property for key '$k'") //Missing for one of the prefixes
}

Looking at docs and laws I see no reason why they shouldn't have it. Please let me know otherwise.

@@ -89,6 +89,12 @@ class KleisliSuite extends CatsSuite {
SerializableTests.serializable(CommutativeMonad[Kleisli[Id, Int, *]])
)

{
implicit val K: Align[Kleisli[Id, MiniInt, *]] = Kleisli.catsDataAlignForKleisli[Id, MiniInt]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For some reason this is required for scala 2.12.x, as it fails to find the implicit Align for Kleisli on its own. Is there another way to do this?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it must be an inference issue. /shrug

[error] /workspace/cats/tests/shared/src/test/scala/cats/tests/KleisliSuite.scala:93:51: could not find implicit value for evidence parameter of type cats.Align[[γ$23$]cats.data.Kleisli[[A]A,cats.laws.discipline.MiniInt,γ$23$]]
[error]     checkAll("Kleisli[Id, MiniInt, *]", AlignTests[Kleisli[Id, MiniInt, *]].align[Int, Int, Int, Int])
[error]                                                   ^

@armanbilge armanbilge added this to the 2.9.0 milestone Oct 12, 2022
Copy link
Member

@armanbilge armanbilge left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. So sorry for the slow review cycle.

def FA: Align[F]

override def align[A, B](fa: Kleisli[F, R, A], fb: Kleisli[F, R, B]): Kleisli[F, R, Ior[A, B]] =
Kleisli(r => FA.align(fa.run(r), fb.run(r)))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a stack-safety trap, but it's not obvious to me if there's a better way.

@armanbilge armanbilge changed the title Added Align instances for Id and Kleisli Added Align instances for Id and Kleisli Oct 12, 2022
@atnoya
Copy link
Contributor Author

atnoya commented Oct 12, 2022

LGTM. So sorry for the slow review cycle.

No worries, definitely not urgent, appreciate your dedication to OSS!

Should I go ahead and merge or wait for more reviewers?

Copy link
Contributor

@satorg satorg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me too, thanks!

@satorg satorg merged commit 7ec4ed9 into typelevel:main Oct 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants