-
-
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
Kleisli#andThen unavailable for functor Validated[X, ?] #2013
Comments
we have the |
Just to add to this, FWIW, here's an example I just ran in the REPL: scala> val x = Kleisli((i: Int) => i.asRight[String])
x: cats.data.Kleisli[[+B]Either[String,B],Int,Int] = Kleisli($$Lambda$2585/1074814992@1f9843e5)
scala> val y = Kleisli((i: Int) => i.toDouble.asRight[String])
y: cats.data.Kleisli[[+B]Either[String,B],Int,Double] = Kleisli($$Lambda$2586/357402099@2a90f681)
scala> val z = x andThen y
z: cats.data.Kleisli[[+B]Either[String,B],Int,Double] = Kleisli(cats.data.Kleisli$$Lambda$2589/2011040303@4e1ff808)
scala> val w = Kleisli((d: Double) => if (d < 5.0) d.toString.asLeft[Double] else d.asRight[String])
w: cats.data.Kleisli[[+B]Either[String,B],Double,Double] = Kleisli($$Lambda$2590/1381774303@1dc0c104)
scala> val v = Kleisli((d: Double) => "Error".asLeft[Double])
v: cats.data.Kleisli[[+B]Either[String,B],Double,Double] = Kleisli($$Lambda$2591/1179268926@2206e39a)
scala> z andThen (v, w).parMapN(_ + _)
res4: cats.data.Kleisli[[+B]Either[String,B],Int,Double] = Kleisli(cats.data.Kleisli$$Lambda$2589/2011040303@3e52d414)
scala> res4.run(2)
res5: Either[String,Double] = Left(Error2.0)
scala> res4.run(6)
res6: Either[String,Double] = Left(Error) |
Composing Kleisli's with
andThen
isn't possible when the functor isValidated[X, ?]
even though validated has anandThen
method.This is because Kleisli's
andThen
is an alias forflatMap
and requires aFlatMap
instance butValidated
doesn't have aFlatMap
instance (rightly so).I don't know if it's reasonable, but there could be an
AndThen
type class in the same manner asFlatMap
. The laws could be the same and for the vast majority of the instances theandThen
method would just alias toflatMap
. A default internalAndThenIsFlatMap
trait could bake this into existingFlatMap
instances. I don't know if this has any broader benefit outside of fixing the issue described above.The text was updated successfully, but these errors were encountered: