Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8062703
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T10:45:38+00:00 2026-06-05T10:45:38+00:00

Being writing a completely async library to access a remote service (using Play2.0), I’m

  • 0

Being writing a completely async library to access a remote service (using Play2.0), I’m using Promise and Validation to create non-blocking call, which has a type presenting fail and valid result at once.

Promise comes from Play2-scala, where Validation comes from scalaz.

So here is the type of examples of such functions

  • f :: A => Promise[Validation[E, B]]
  • g :: B => Promise[Validation[E, C]]

So far, so good, now if I want to compose them, I can simple use the fact that Promise present a flatMap, so I can do it with a for-comprehension

for (
   x <- f(a);
   y <- g(b)
) yield y

Ok, I took a shortcut to my problem here because I didn’t reused the Validation results within the for-comprehension. So if I want to reuse x in g, here is how I could do

for (
   x <- f(a); // x is a Validation
   y <- x.fold(
      fail => Promise.pure(x),
      ok => g(ok)
   )
) yield y

Fair enough, but this kind of boilerplate will go to pollute my code over and over again. The problem here is that I’ve a kind of two-levels Monadic structure like M[N[_]].

At this stage, is there any structure in f° programming that enables working with such structure by skipping easily the secong level:

for (
   x <- f(a); //x is a B
   y <- g(b) 
) yield y

Now, below is how I achieved something similar.

I created kind of Monadic structure that wraps the two level in one, let say ValidationPromised which pimped the Promise type with two methods:

def /~> [EE >: E, B](f: Validation[E, A] => ValidationPromised[EE, B]): ValidationPromised[EE, B] = 
    promised flatMap { valid => 
        f(valid).promised
    }

def /~~>[EE >: E, B](f: A => ValidationPromised[EE, B]): ValidationPromised[EE, B] = 
    promised flatMap { valid => 
        valid.fold (
            bad => Promise.pure(KO(bad)),
            good => f(good).promised
        )
    }

This allows me to do such things

      endPoint.service /~~>                                   //get the service
      (svc =>                                                 //the service
        svc.start /~~> (st =>                                 //get the starting elt
          svc.create(None) /~~>                               //svc creates a new elt
          (newE =>                                            //the created one
            newEntry.link(st, newE) /~~>                      //link start and the new
            (lnk => Promise.pure(OK((st, lnk, newE))))        //returns a triple => hackish 
          ) 
        )
      )

As we can see /~~> is pretty similar to flatMap but skips one level. The problem is the verbosity (that’s why “for-comprehension” exists in Scala and “do” in Haskell).

Another point, I’ve the /~> that stands like a map also but works on the second level (instead of the Valid type — third level)

So my second question is corollary to the former… Am I approching a sustainable solution with this construction ?

sorry to be that long

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T10:45:40+00:00Added an answer on June 5, 2026 at 10:45 am

    The concept you are looking for here is monad transformers. In brief, monad transformers compensate for monads not composing by allowing you to “stack” them.

    You didn’t mention the version of Scalaz you are using, but if you look in the scalaz-seven branch, you’ll find ValidationT. This can be used to wrap any F[Validation[E, A]] into a ValidationT[F, E, A], where in your case F = Promise. If you change f and g to return ValidationT, then you can leave your code as

    for {
      x ← f(a)
      y ← g(b)
    } yield y
    

    This will give you a ValidationT[Promise, E, B] as a result.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing an auditing service for some business-critical operations. The service is being implemented
I'm writing a Ruby C Extension where I'm using math.h . It's being compiled
I am writing a crawler to log all cookies being deployed by a set
I'm writing an application that runs things to a schedule. Idea being that the
I am writing a Page with XML Only. No XSL is being. everything is
I am currently writing a bulk processing algorithm for pitch detection in audio being
I'm writing a .NET library to inject managed DLLs into external processes. My current
Some background I am planning to writing a REST service which helps facilitate collaboration
I'm currently writing a little C# library to simplify implementing little physical simulations/experiments. The
I'm writing a (hopefully) small project that would end up being distributed in binary

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.