Scala 如何在DBIOaction SLICK中展平序列?

Scala 如何在DBIOaction SLICK中展平序列?,scala,slick,flatten,Scala,Slick,Flatten,嘿,伙计们,我是新来的滑头,我怎么能把这个序列展平呢?这样就可以返回注释过的代码 def insertIfNotExists(mapCountryStates: Map[String, Iterable[StateUtil]]): Future[Seq[Seq[StateTable]]] /*: Future[Seq[StateTable]]*/ = { val interaction = DBIO.sequence(mapCountryStates.toSeq.map { ca

嘿,伙计们,我是新来的滑头,我怎么能把这个序列展平呢?这样就可以返回注释过的代码

    def insertIfNotExists(mapCountryStates: Map[String, Iterable[StateUtil]]): Future[Seq[Seq[StateTable]]] /*: Future[Seq[StateTable]]*/ = {
    val interaction = DBIO.sequence(mapCountryStates.toSeq.map { case (alpha2Country, statesUtil) =>
      val codes = statesUtil.map(_.alpha3Code)
      for {
        countryId <- Countries.filter(_.alpha2Code === alpha2Country).map(_.id).result.head
        existing <- States.filter(s => (s.alpha3Code inSet codes) && s.countryId === countryId).result
        stateTables = statesUtil.map(x => StateTable(0L, x.name, x.alpha3Code, countryId))
        statesInserted <- StatesInsertQuery ++= stateTables.filter(s => !existing.exists(x => x.alpha3Code == s.alpha3Code && x.countryId == s.countryId))
      } yield existing ++ statesInserted
    })

    db.run(interaction.transactionally)
  }
def insertIfNotExists(mapccountrystates:Map[String,Iterable[StateUtil]]):Future[Seq[Seq[StateTable]]/*:Future[Seq[StateTable]]]*/={
val interaction=DBIO.sequence(mapconrystates.toSeq.map{case(alpha2Country,statesUtil)=>
val代码=statesUtil.map(uu.alpha3Code)
为了{
countryId状态表(0L,x.name,x.alpha3Code,countryId))
statesInserted!existing.exists(x=>x.alpha3Code==s.alpha3Code&&x.countryId==s.countryId))
}生成已插入的现有++状态
})
db.run(交互.事务性)
}
如果我写在这里:

val交互作用=DBIO.序列(…)。展平

或在此:

db.run(交互。扁平化。事务性)


[error]无法证明Seq[Seq[StateRepository.this.StateTableMapping#TableElementType]一个可行的解决方案应该是在
未来的
完成后展平序列:

def insertIfNotExists(mapCountryStates: Map[String, Iterable[StateUtil]]): Future[Seq[StateTable]] = {
  val interaction = DBIO.sequence(mapCountryStates.toSeq.map { case (alpha2Country, statesUtil) =>
    val codes = statesUtil.map(_.alpha3Code)
    for {
      countryId <- Countries.filter(_.alpha2Code === alpha2Country).map(_.id).result.head
      existing <- States.filter(s => (s.alpha3Code inSet codes) && s.countryId === countryId).result
      stateTables = statesUtil.map(x => StateTable(0L, x.name, x.alpha3Code, countryId))
      statesInserted <- StatesInsertQuery ++= stateTables.filter(s => !existing.exists(x => x.alpha3Code == s.alpha3Code && x.countryId == s.countryId))
    } yield existing ++ statesInserted
  })

  db.run(interaction.transactionally).map(_.flatten)
}
def insertIfNotExists(mapccountrystates:Map[String,Iterable[StateUtil]]):Future[Seq[StateTable]={
val interaction=DBIO.sequence(mapconrystates.toSeq.map{case(alpha2Country,statesUtil)=>
val代码=statesUtil.map(uu.alpha3Code)
为了{
countryId状态表(0L,x.name,x.alpha3Code,countryId))
statesInserted!existing.exists(x=>x.alpha3Code==s.alpha3Code&&x.countryId==s.countryId))
}生成已插入的现有++状态
})
db.run(interaction.transactionaly).map(u.flant)
}

一个可行的解决方案应该是,一旦
未来的
完成,就将序列展平:

def insertIfNotExists(mapCountryStates: Map[String, Iterable[StateUtil]]): Future[Seq[StateTable]] = {
  val interaction = DBIO.sequence(mapCountryStates.toSeq.map { case (alpha2Country, statesUtil) =>
    val codes = statesUtil.map(_.alpha3Code)
    for {
      countryId <- Countries.filter(_.alpha2Code === alpha2Country).map(_.id).result.head
      existing <- States.filter(s => (s.alpha3Code inSet codes) && s.countryId === countryId).result
      stateTables = statesUtil.map(x => StateTable(0L, x.name, x.alpha3Code, countryId))
      statesInserted <- StatesInsertQuery ++= stateTables.filter(s => !existing.exists(x => x.alpha3Code == s.alpha3Code && x.countryId == s.countryId))
    } yield existing ++ statesInserted
  })

  db.run(interaction.transactionally).map(_.flatten)
}
def insertIfNotExists(mapccountrystates:Map[String,Iterable[StateUtil]]):Future[Seq[StateTable]={
val interaction=DBIO.sequence(mapconrystates.toSeq.map{case(alpha2Country,statesUtil)=>
val代码=statesUtil.map(uu.alpha3Code)
为了{
countryId状态表(0L,x.name,x.alpha3Code,countryId))
statesInserted!existing.exists(x=>x.alpha3Code==s.alpha3Code&&x.countryId==s.countryId))
}生成已插入的现有++状态
})
db.run(interaction.transactionaly).map(u.flant)
}

看起来您可能在使用
DBIO.fold
。这提供了一种方法,可以采取许多操作,并将它们减少到单个值。在这种情况下,您的单个值是
Seq[StateTable]
中的
Seq[StateTable]

这可能看起来是什么样子的草图

def insertIfNotExists(...): DBIO[Seq[StateTable]] = {
  val interaction: Seq[DBIO[Seq[StateTable]]] = ...
  val startingPoint: Seq[StateTable] = Seq.empty
  DBIO.fold(interaction, startingPoint) {
    (total, list) => total ++ list
  }
}
看起来这些类型将使用折叠排列。希望对你的情况有用


有一些关于折叠的详细信息。

看起来您可能在
DBIO.fold之后。这提供了一种方法,可以采取许多操作,并将它们减少到单个值。在这种情况下,您的单个值是
Seq[StateTable]
中的
Seq[StateTable]

这可能看起来是什么样子的草图

def insertIfNotExists(...): DBIO[Seq[StateTable]] = {
  val interaction: Seq[DBIO[Seq[StateTable]]] = ...
  val startingPoint: Seq[StateTable] = Seq.empty
  DBIO.fold(interaction, startingPoint) {
    (total, list) => total ++ list
  }
}
看起来这些类型将使用折叠排列。希望对你的情况有用


还有一些关于折叠的更多信息。

该解决方案是我在返回控制器之前所做的,但我想在dbioaction中展平,无论如何,非常感谢,因为该解决方案是我在返回控制器之前所做的,但无论如何,我想在dbioaction中展平,非常感谢你,regardsoops,就像我以前没有意识到的那样,折叠它正是我想要的,谢谢@Richard Dallaway,现在如果我关闭我的解决方案…哦,就像我以前没有意识到的那样,折叠它正是我想要的,谢谢@Richard Dallaway,现在如果我关闭我的解决方案。。。。