Add Filter API for Free monads. Any if condition in the for comprehension is failing due to the missing filter APIs. There is also a StackOverflow question on the same. https://stackoverflow.com/q/41659747/3711149
Hey guys,
Any update or a workaround available? I want to use Free Monads in Prod and this would be a great feature to have.
You could used FreeT with Either[String, ?] as the inner monad then implement filter on that FreeT with it just giving you a left in the case that the filter fails.
Sorry, I'm on a phone or I would write the code out here.
Thanks. If you can post a gist/snippet, it would be great.
Okay, something like:
type MyFree[M[_], T] = FreeT[Either[String, ?], M, T]
implicit class FilterFree[M[_], T](val on: FreeT[Either[String, ?], M, T]) extends AnyVal {
def filter(fn: T => Boolean): FreeT[Either[String, ?], M, T] =
on.flatMap { t =>
if (fn(t)) FreeT.pure(t)
else FreeT.liftF(Left(s"filter failed on value: $t"))
}
}
Something like that.
We could think about adding a generalized version of @johnynek snippet that accepts any MonadError, might be useful 🤔
Something I came up with on the quick:
def filter[E](f: A => Boolean)(e: => E)(implicit ME: MonadError[M, E]): FreeT[S, M, A] =
on.flatMap { t =>
if (f(t)) FreeT.pure(t)
else FreeT.liftT(ME.raiseError(e))
}
The above variant may be useful, but can't be used with a for comprehension.
I don't think it makes sense to include this unless it's for MonadError over Unit.
I vote to close this, as I don't think it's really worth including.
+1 to close.
On Wed, Oct 4, 2017 at 08:04 Alexander Konovalov notifications@github.com
wrote:
Instead of
for {
(a, b) <- f
} yield ayou can always write
for {
ab <- f
(a, b) = ab
} yield awhich is a bit annoying but not the end of the world ;) I think that is
the use case OP had problems with.—
You are receiving this because you were mentioned.Reply to this email directly, view it on GitHub
https://github.com/typelevel/cats/issues/1851#issuecomment-334240847,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAEJdrm3UscN27se-wV4jS84opz8xmv2ks5so8iygaJpZM4PBxc_
.>
P. Oscar Boykin, Ph.D. | http://twitter.com/posco | http://pobox.com/~boykin
Most helpful comment
Okay, something like:
Something like that.