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;








1















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?










share|improve this question




























    1















    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?










    share|improve this question
























      1












      1








      1








      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?










      share|improve this question














      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 23 at 13:56









      AlexandrAlexandr

      5,02784581




      5,02784581






















          1 Answer
          1






          active

          oldest

          votes


















          2














          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.






          share|improve this answer

























            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
            );



            );













            draft saved

            draft discarded


















            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









            2














            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.






            share|improve this answer





























              2














              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.






              share|improve this answer



























                2












                2








                2







                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.






                share|improve this answer















                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.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Mar 23 at 15:00

























                answered Mar 23 at 14:45









                Yuval ItzchakovYuval Itzchakov

                118k26179248




                118k26179248





























                    draft saved

                    draft discarded
















































                    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.




                    draft saved


                    draft discarded














                    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





















































                    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







                    Popular posts from this blog

                    SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

                    용인 삼성생명 블루밍스 목차 통계 역대 감독 선수단 응원단 경기장 같이 보기 외부 링크 둘러보기 메뉴samsungblueminx.comeh선수 명단용인 삼성생명 블루밍스용인 삼성생명 블루밍스ehsamsungblueminx.comeheheheh

                    155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해