2017-03-09 52 views
0

尝试映射元组列表时出现编译错误。我有回Future[List[(String, String)]]的方法,我需要使用这两个集的字符串值,使调用其他方法Map Scala元组列表

def myHelper(): Future[List[(String, String)]] = { ... } 

def myWork() = { 
    myHelper() map { 
    case(firstVal, secondVal) => otherWork(firstVal, secondVal) 
    } 
} 

错误,我得到的是

found: (T1, T2) 
required: List[(String, String)] 

什么建议吗?

编辑 嗯....我不清楚我的问题。 otherWork预计所有结果列表

def otherWork(firstVals: List[String], secondVals: List[Strong]) = { ... } 
+0

我已经更新我的答案。而且,'otherWork'返回类型是什么? –

回答

2

让我们实现myWork功能:

def myWork = myHelper.map { 
    helper => (otherWork _).tupled(helper.unzip) 
} 
+0

更新我的问题 –

2

取决于你想要做什么。在每个元组

呼叫otherWork

def myWork() = { 
    myHelper().map(_.map { 
     case (firstVal, secondVal) => otherWork(firstVal, secondVal) 
    }) 
} 

如果otherWork: (String, String) => T,然后myWork:() => Future[List[T]]

如果otherWork: (String, String) => Future[T]和要运行所有这些,收集结果,那么你可以使用像

def myWork() = { 
    myHelper() flatMap { list => Future.sequence(
     list map { case (firstVal, secondVal) => 
      otherWork(firstVal, secondVal) 
     }) 
    } 
} 

随着问题的澄清,你想unzip

def myWork() = { 
    myHelper() map { list => 
     list.unzip match { 
      case (firstVals, secondVals) => otherWork(firstVals, secondVals) 
     } 
    } 
} 

Federico Pellegatta的回答有一个较短的写作形式。

+0

更新我的问题 –