Too many nested Types? I have the following Scala code:

  def getLists(token: String, username: String):Future[Try[Seq[Group]]] = {
    val result:Future[Try[JsValue]] = getAPIResult(token, "https://myapi.com/screen_name=" + username)
    result.map( x =&gt; convertFromJsValueToObject(x))
  }

  def getSubscriptions(token: String, username: String):Future[Try[Seq[Group]]] = {...} //same as above

  private def getAPIResult(token: String, apiCall: String):Future[Try[JsValue]] = {
    WS.url(apiCall)
    .withHeaders("Authorization" -&gt; ("Bearer " + token))
    .get().map(response =&gt;
       response.status match {
       case 200 =&gt; Success(Json.parse(response.body))
       case _ =&gt; Failure(new RuntimeException("Web service call failed: " + response.body))
    })
  }

  private def convertFromJsValueToObject(json: Try[JsValue]): Try[Seq[Group]] = {
    json match {
      case Success(v) =&gt; {
        Success(v.as[Seq[Group]])
      }
      case Failure(t) =&gt; Failure(t)
      }
  }


and I have a third function like this:

def getAllListsAndSubscriptions(token: String, username: String):Future[Try[Seq[Group]]] = {...}


Which is supposed to concatenate the two Seq collections of Group objects together and return them as per the type signature. I'm really struggling to figure out how to do this though, as the types are so deeply nested.

Have I written my code badly or is there another way to do this?
You should "lift" your functions instead.

For example, instead of defining:

def getSomething(input: V): Future[FinalType] = {
   val intermediate: IntermediaryType = complicatedFunctionReturningFuture(input)
   mappingFunction(intermediate)
}


use

complicatedFunctionReturningFuture(input).map(mappingFunction)


directly in your code.

You can think of map as a method that is lifting a function IntermediaryType =&gt; FinalType to a function Future[Intermediary] =&gt; Future[FinalType].

If you have a Future[Try[Type]], you can do a "double lifting" (I'm claiming the trademark on that term!):

complicatedFunctionReturningFutureTry(input).map(_.map(mappingFunction))


which lifts the same function IntermediaryType =&gt; FinalType to a function Future[Try[Intermediary]] =&gt; Future[Try[FinalType]].