Scala:在后续的 flatMap 中使用映射函数元组
Scala: Use map function tuples in a subsequent flatMap
我想在后续的flatMap中使用一个map函数的元组
像这样:
val list = orders.map(ord => (ord.prod.tasks, ord.quantity))
.zipWithIndex flatMap {
case (variable, index) =>
createSchedules(variable._1.size * variable._2, ord, null)
}
有什么方法可以使用它,或者您认为我必须改变我考虑解决方案的方式吗?
I want to use a tuple of a map function in a subsequent flatMap.
这是在后续 flatMap 中使用元组的工作示例
val input = List("a", "b", "c")
val list = input.map(i => (i + "A", i + "B")).zipWithIndex flatMap{ case (variable, index)=> variable._1 + variable._2}
Issue with original code
val list = orders.map(ord => (ord.prod.tasks, ord.quantity)).zipWithIndex flatMap{case (variable, index)=>createSchedules(variable._1.size * variable._2, ord, null)}
一个问题是 ord 超出了 'createSchedules' 调用的范围。
ord 将仅在初始 'orders.map' 范围内可见。
首先,从您传递给 createSchedules
的参数来看,该函数似乎可以简化为(我暂时忽略 null
参数) :
def createSchedules(order: Order): List[Schedule] = {
val num = order.prod.tasks.size * order.quantity
// do stuff
}
此外,开头的map
是不必要的。 list
可以简化为:
val list = orders.zipWithIndex
.flatMap {
case (order, index) =>
createSchedules(order)
}
虽然不清楚您需要 index
做什么。
我想在后续的flatMap中使用一个map函数的元组
像这样:
val list = orders.map(ord => (ord.prod.tasks, ord.quantity))
.zipWithIndex flatMap {
case (variable, index) =>
createSchedules(variable._1.size * variable._2, ord, null)
}
有什么方法可以使用它,或者您认为我必须改变我考虑解决方案的方式吗?
I want to use a tuple of a map function in a subsequent flatMap.
这是在后续 flatMap 中使用元组的工作示例
val input = List("a", "b", "c")
val list = input.map(i => (i + "A", i + "B")).zipWithIndex flatMap{ case (variable, index)=> variable._1 + variable._2}
Issue with original code
val list = orders.map(ord => (ord.prod.tasks, ord.quantity)).zipWithIndex flatMap{case (variable, index)=>createSchedules(variable._1.size * variable._2, ord, null)}
一个问题是 ord 超出了 'createSchedules' 调用的范围。
ord 将仅在初始 'orders.map' 范围内可见。
首先,从您传递给 createSchedules
的参数来看,该函数似乎可以简化为(我暂时忽略 null
参数) :
def createSchedules(order: Order): List[Schedule] = {
val num = order.prod.tasks.size * order.quantity
// do stuff
}
此外,开头的map
是不必要的。 list
可以简化为:
val list = orders.zipWithIndex
.flatMap {
case (order, index) =>
createSchedules(order)
}
虽然不清楚您需要 index
做什么。