Compose ReaderT with Either through for-comprehension and applicative pureApplicatives compose, monads don'tEither, Options and for comprehensionsWhat's the best way to get a Scala Object field by inputting Object name in StringParser combinator optional partsFor Comprehension Example to Understand Composing MonadsApplicative <*> inside ReaderT StateT IOHow to compose functions operating with ReaderT and Either?For comprehensions Future and EitherCompose optional queries for for-comprehension in doobie?Scala - How to Combine EitherT with Either in For Comprehension
How might a landlocked lake become a complete ecosystem?
Where to find every-day healthy food near Heathrow Airport?
Do Life Drain attacks from wights stack?
Motorola 6845 and bitwise graphics
Is 95% of what you read in the financial press “either wrong or irrelevant?”
Were any toxic metals used in the International Space Station?
How can we allow remote players to effectively interact with a physical tabletop battle-map?
Why does lemon juice reduce the "fish" odor of sea food — specifically fish?
Could there be a material that inverts the colours seen through it?
What information do scammers need to withdraw money from an account?
Why doesn't Iron Man's action affect this person in Endgame?
Adding labels and comments to a matrix
Is this possible when it comes to the relations of P, NP, NP-Hard and NP-Complete?
Substring join or additional table, which is faster?
Are there any established rules for splitting books into parts, chapters, sections etc?
How do I adjust encounters to challenge my lycanthrope players without negating their cool new abilities?
complicated arrows in flowcharts
Can a tourist shoot a gun for recreational purpose in the USA?
Is 12 minutes connection in Bristol Temple Meads long enough?
Why does the headset man not get on the tractor?
Alexa-rank complaining about insecure generator meta-tag
What was Euron's motivation for this fight in "The Bells"?
Can you pick an advanced rogue talent with the extra rogue talent feat?
Fixed width with p doesn't work
Compose ReaderT with Either through for-comprehension and applicative pure
Applicatives compose, monads don'tEither, Options and for comprehensionsWhat's the best way to get a Scala Object field by inputting Object name in StringParser combinator optional partsFor Comprehension Example to Understand Composing MonadsApplicative <*> inside ReaderT StateT IOHow to compose functions operating with ReaderT and Either?For comprehensions Future and EitherCompose optional queries for for-comprehension in doobie?Scala - How to Combine EitherT with Either in For Comprehension
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
Here are functions that return ReaderT and Either as a return type:
import cats.data.ReaderT
type FailFast[A] = Either[List[String], A]
def getValue(name: String):ReaderT[FailFast, Map[String, String], String] =
ReaderT((map) => map.get(name)
.toRight(List(s"$name field not specified")))
def nonNullable(name: String)(data: String): FailFast[String] =
Right(data).ensure(List(s"$name cannot be nullable"))(_ != null)
def nonBlank(name: String)(data: String): FailFast[String] =
Right(data).ensure(List(s"$name cannot be blank"))(_.nonEmpty)
Here is a composition of these functions that works fine:
def readNotEmptyValue(name: String): ReaderT[FailFast, Map[String, String], String] =
for
value <- getValue(name)
_ <- ReaderT((_:Map[String, String]) => nonNullable(name)(value))
res <- ReaderT((_:Map[String, String]) => nonBlank(name)(value))
yield res
I want to get rid of this ReaderT.apply invocation, and write something via applicative pure:
type Q[A] = ReaderT[FailFast, Map[String, String], A]
import cats.syntax.applicative._
import cats.instances.either._
def readNotEmptyValue(name: String): ReaderT[FailFast, Map[String, String], String] =
for
value <- getValue(name)
_ <- nonBlank(name)(value).pure[Q]
res <- nonBlank(name)(value).pure[Q]
yield res.right.get
Unfortunately the last solution does not work with negative cases. I can for sure use match to check, weather it is Right or Left.
But is there a way to compose it with pure and minimize manual effort. How to do it correctly?
scala monads composition monad-transformers scala-cats
add a comment |
Here are functions that return ReaderT and Either as a return type:
import cats.data.ReaderT
type FailFast[A] = Either[List[String], A]
def getValue(name: String):ReaderT[FailFast, Map[String, String], String] =
ReaderT((map) => map.get(name)
.toRight(List(s"$name field not specified")))
def nonNullable(name: String)(data: String): FailFast[String] =
Right(data).ensure(List(s"$name cannot be nullable"))(_ != null)
def nonBlank(name: String)(data: String): FailFast[String] =
Right(data).ensure(List(s"$name cannot be blank"))(_.nonEmpty)
Here is a composition of these functions that works fine:
def readNotEmptyValue(name: String): ReaderT[FailFast, Map[String, String], String] =
for
value <- getValue(name)
_ <- ReaderT((_:Map[String, String]) => nonNullable(name)(value))
res <- ReaderT((_:Map[String, String]) => nonBlank(name)(value))
yield res
I want to get rid of this ReaderT.apply invocation, and write something via applicative pure:
type Q[A] = ReaderT[FailFast, Map[String, String], A]
import cats.syntax.applicative._
import cats.instances.either._
def readNotEmptyValue(name: String): ReaderT[FailFast, Map[String, String], String] =
for
value <- getValue(name)
_ <- nonBlank(name)(value).pure[Q]
res <- nonBlank(name)(value).pure[Q]
yield res.right.get
Unfortunately the last solution does not work with negative cases. I can for sure use match to check, weather it is Right or Left.
But is there a way to compose it with pure and minimize manual effort. How to do it correctly?
scala monads composition monad-transformers scala-cats
add a comment |
Here are functions that return ReaderT and Either as a return type:
import cats.data.ReaderT
type FailFast[A] = Either[List[String], A]
def getValue(name: String):ReaderT[FailFast, Map[String, String], String] =
ReaderT((map) => map.get(name)
.toRight(List(s"$name field not specified")))
def nonNullable(name: String)(data: String): FailFast[String] =
Right(data).ensure(List(s"$name cannot be nullable"))(_ != null)
def nonBlank(name: String)(data: String): FailFast[String] =
Right(data).ensure(List(s"$name cannot be blank"))(_.nonEmpty)
Here is a composition of these functions that works fine:
def readNotEmptyValue(name: String): ReaderT[FailFast, Map[String, String], String] =
for
value <- getValue(name)
_ <- ReaderT((_:Map[String, String]) => nonNullable(name)(value))
res <- ReaderT((_:Map[String, String]) => nonBlank(name)(value))
yield res
I want to get rid of this ReaderT.apply invocation, and write something via applicative pure:
type Q[A] = ReaderT[FailFast, Map[String, String], A]
import cats.syntax.applicative._
import cats.instances.either._
def readNotEmptyValue(name: String): ReaderT[FailFast, Map[String, String], String] =
for
value <- getValue(name)
_ <- nonBlank(name)(value).pure[Q]
res <- nonBlank(name)(value).pure[Q]
yield res.right.get
Unfortunately the last solution does not work with negative cases. I can for sure use match to check, weather it is Right or Left.
But is there a way to compose it with pure and minimize manual effort. How to do it correctly?
scala monads composition monad-transformers scala-cats
Here are functions that return ReaderT and Either as a return type:
import cats.data.ReaderT
type FailFast[A] = Either[List[String], A]
def getValue(name: String):ReaderT[FailFast, Map[String, String], String] =
ReaderT((map) => map.get(name)
.toRight(List(s"$name field not specified")))
def nonNullable(name: String)(data: String): FailFast[String] =
Right(data).ensure(List(s"$name cannot be nullable"))(_ != null)
def nonBlank(name: String)(data: String): FailFast[String] =
Right(data).ensure(List(s"$name cannot be blank"))(_.nonEmpty)
Here is a composition of these functions that works fine:
def readNotEmptyValue(name: String): ReaderT[FailFast, Map[String, String], String] =
for
value <- getValue(name)
_ <- ReaderT((_:Map[String, String]) => nonNullable(name)(value))
res <- ReaderT((_:Map[String, String]) => nonBlank(name)(value))
yield res
I want to get rid of this ReaderT.apply invocation, and write something via applicative pure:
type Q[A] = ReaderT[FailFast, Map[String, String], A]
import cats.syntax.applicative._
import cats.instances.either._
def readNotEmptyValue(name: String): ReaderT[FailFast, Map[String, String], String] =
for
value <- getValue(name)
_ <- nonBlank(name)(value).pure[Q]
res <- nonBlank(name)(value).pure[Q]
yield res.right.get
Unfortunately the last solution does not work with negative cases. I can for sure use match to check, weather it is Right or Left.
But is there a way to compose it with pure and minimize manual effort. How to do it correctly?
scala monads composition monad-transformers scala-cats
scala monads composition monad-transformers scala-cats
asked Mar 23 at 13:56
AlexandrAlexandr
5,02784581
5,02784581
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Instead of Applicative.pure, you can use EitherOps.liftTo to remove the verboseness of ReaderT.apply:
def readNotEmptyValue(name: String): ReaderT[FailFast, Map[String, String], String] =
for
value <- getValue(name)
_ <- nonBlank(name)(value).liftTo[Q]
res <- nonBlank(name)(value).liftTo[Q]
yield res
Otherwise, you're still dealing with an instance of FailFast[String] and not String, and there's no way around it if you want to attempt and extract the value out of FailFast.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55314431%2fcompose-readert-with-either-through-for-comprehension-and-applicative-pure%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Instead of Applicative.pure, you can use EitherOps.liftTo to remove the verboseness of ReaderT.apply:
def readNotEmptyValue(name: String): ReaderT[FailFast, Map[String, String], String] =
for
value <- getValue(name)
_ <- nonBlank(name)(value).liftTo[Q]
res <- nonBlank(name)(value).liftTo[Q]
yield res
Otherwise, you're still dealing with an instance of FailFast[String] and not String, and there's no way around it if you want to attempt and extract the value out of FailFast.
add a comment |
Instead of Applicative.pure, you can use EitherOps.liftTo to remove the verboseness of ReaderT.apply:
def readNotEmptyValue(name: String): ReaderT[FailFast, Map[String, String], String] =
for
value <- getValue(name)
_ <- nonBlank(name)(value).liftTo[Q]
res <- nonBlank(name)(value).liftTo[Q]
yield res
Otherwise, you're still dealing with an instance of FailFast[String] and not String, and there's no way around it if you want to attempt and extract the value out of FailFast.
add a comment |
Instead of Applicative.pure, you can use EitherOps.liftTo to remove the verboseness of ReaderT.apply:
def readNotEmptyValue(name: String): ReaderT[FailFast, Map[String, String], String] =
for
value <- getValue(name)
_ <- nonBlank(name)(value).liftTo[Q]
res <- nonBlank(name)(value).liftTo[Q]
yield res
Otherwise, you're still dealing with an instance of FailFast[String] and not String, and there's no way around it if you want to attempt and extract the value out of FailFast.
Instead of Applicative.pure, you can use EitherOps.liftTo to remove the verboseness of ReaderT.apply:
def readNotEmptyValue(name: String): ReaderT[FailFast, Map[String, String], String] =
for
value <- getValue(name)
_ <- nonBlank(name)(value).liftTo[Q]
res <- nonBlank(name)(value).liftTo[Q]
yield res
Otherwise, you're still dealing with an instance of FailFast[String] and not String, and there's no way around it if you want to attempt and extract the value out of FailFast.
edited Mar 23 at 15:00
answered Mar 23 at 14:45
Yuval ItzchakovYuval Itzchakov
118k26179248
118k26179248
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55314431%2fcompose-readert-with-either-through-for-comprehension-and-applicative-pure%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown