Is a Monad Transformation required here?

The old version of the API looked something like this (credit @travisbrown for the offer traverseU here ). Please note that these two APIs are created as an example of toys to illustrate the gist of this SO question. Real code does more interesting things.

def anotherFunc: Throwable \/ List[String] = personList.flatMap { pl =>
  pl.traverseU { p =>
  for {
    s <- Option("fakeString").\/>(new Throwable("not found"))
  } yield s
 }
}

Where

case class Person(name: String)

 private def personList: \/[Throwable, List[Person]] = {
   \/.fromTryCatch {
      List(Person("John"))
  }
}

Now converting this defto Futurewill look like this

def anotherFunc: Future[Throwable \/ List[String]] = Future {
personList.flatMap { pl =>
  pl.traverseU { p =>
    for {
      s <- Option("fakeString").\/>(new Throwable("not found"))


    } yield s
  }
 }
}

Now, as mentioned earlier, this is an example of a toy, but I have one or more generators in this understanding forthat return a clause wrapped in Future, for example.Future[\/[Throwable,String]]

you can imagine something like s1below

   for {
       s <- Option("fakeString").\/>(new Throwable("not found"))
       //s1 <- Future{"returning Future[\/[Throwable,String]" }
   } yield //value out of s1 future must be yielded

Is monad conversion required here?

. out Future monad (s1 ) , anotherFunc? , EitherT ?

. anotherFunc , Future[\/[Throwable,String]]. Await

def anotherFunc: Future[Throwable \/ List[String]] = Future {
personList.flatMap { pl =>
  pl.traverseU { p =>
    for {
      s <- Option("fakeString").\/>(new Throwable("not found"))
      f <- Await.result(futureResult, 1.seconds)
    } yield f
  }
}
}

def futureResult = Future{Option("""another Disjunction is called here - which returns a    Future[\/[Throwable,T]""").\/>(new Throwable("not found"))}
+4

Source: https://habr.com/ru/post/1545707/


All Articles