Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Scala for/yield运行,但不';不完整_Scala - Fatal编程技术网

Scala for/yield运行,但不';不完整

Scala for/yield运行,但不';不完整,scala,Scala,我试图遍历两个可能大小不同的数组,并从中随机选择元素组成一个新数组(用于遗传算法中的交叉)(childGeneCount只是较长数组的长度) 在下面的代码段中,每个gene.toString都会记录日志,但我的代码似乎不会执行最后一个日志。我在做什么蠢事 val genes = for (i <- 0 to childGeneCount) yield { val gene = if (Random.nextBoolean()) { if (i < p1genes.leng

我试图遍历两个可能大小不同的数组,并从中随机选择元素组成一个新数组(用于遗传算法中的交叉)(childGeneCount只是较长数组的长度)

在下面的代码段中,每个gene.toString都会记录日志,但我的代码似乎不会执行最后一个日志。我在做什么蠢事

val genes = for (i <- 0 to childGeneCount) yield {
  val gene = if (Random.nextBoolean()) {
    if (i < p1genes.length) {
      p1genes(i)
    } else {
      p2genes(i)
    }
  } else {
    if (i < p2genes.length) {
      p2genes(i)
    } else {
      p1genes(i)
    }
  }
  Logger.debug(gene.toString)
  gene
}
Logger.debug("crossover finishing - never gets here??")

val genes=for(i非常肯定harry0000的答案是正确的:我一直在用“to”like“until”,而且我非常习惯于异常被大声抛出,以至于我没想到去看那里


我最终从for/yield切换到了
List.tablate(childGeneCount){I=>{
,这修复了这个错误,可能是出于同样的原因。

非常确定harry0000的答案是正确的:我使用的是“to”like“until”,并且非常习惯于异常被大声抛出,以至于我不想看那里


最后,我从for/yield切换到
List.tablate(childGeneCount){I=>{
,修复错误的原因可能与此相同。

既然您要求可能的样式改进,这里有两个建议的实现。第一个实现不太惯用,但性能更高。第二个更漂亮,但做得更多

  def crossover[E : ClassTag](a: Array[E], b: Array[E]): Array[E] = {
    val (larger, smaller) = if(a.length > b.length) (a, b) else (b, a)
    val result = Array.ofDim[E](larger.length)
    for(i <- smaller.indices)
      result(i) = if(Random.nextBoolean()) larger(i) else smaller(i)
    for(i <- smaller.length until larger.length)
      result(i) = larger(i)
    result
  }


  def crossoverIdiomatic[E : ClassTag](a: Array[E], b: Array[E]): Array[E] = {
    val randomPart = (a zip b).map { case (x,y) => if(Random.nextBoolean()) x else y }
    val (larger, smaller) = if(a.length > b.length) (a, b) else (b, a)
    randomPart ++ larger.drop(smaller.length)
  }

  val a = Array("1", "2", "3", "4", "5", "6")
  val b = Array("one", "two", "three", "four")

  // e.g. output: [one,2,three,4,5,6]
  println(crossover(a, b).mkString("[", ",", "]"))
  println(crossoverIdiomatic(a, b).mkString("[", ",", "]"))
def crossover[E:ClassTag](a:Array[E],b:Array[E]):Array[E]={
val(较大,较小)=如果(a.长度>b.长度)(a,b)其他(b,a)
val结果=数组.ofDim[E](较大的.length)
对于(i b.长度)(a,b)其他(b,a)
randomPart++较大的.drop(较小的.length)
}
val a=数组(“1”、“2”、“3”、“4”、“5”、“6”)
val b=数组(“一”、“二”、“三”、“四”)
//例如输出:[一,二,三,四,五,六]
println(交叉(a,b).mkString(“[”,“,”,“]))
println(crossoverIdiomatic(a,b).mkString(“[”,“,”,“]))

请注意,
E:ClassTag
只是为了让编译器乐于使用
Array[E]
,如果你的工作只需要
Int
,你可以放弃所有花哨的泛型。

既然你要求可能的风格改进,这里有两个建议的实现。第一个不太惯用,但性能更高。第二个更漂亮,但做得更多

  def crossover[E : ClassTag](a: Array[E], b: Array[E]): Array[E] = {
    val (larger, smaller) = if(a.length > b.length) (a, b) else (b, a)
    val result = Array.ofDim[E](larger.length)
    for(i <- smaller.indices)
      result(i) = if(Random.nextBoolean()) larger(i) else smaller(i)
    for(i <- smaller.length until larger.length)
      result(i) = larger(i)
    result
  }


  def crossoverIdiomatic[E : ClassTag](a: Array[E], b: Array[E]): Array[E] = {
    val randomPart = (a zip b).map { case (x,y) => if(Random.nextBoolean()) x else y }
    val (larger, smaller) = if(a.length > b.length) (a, b) else (b, a)
    randomPart ++ larger.drop(smaller.length)
  }

  val a = Array("1", "2", "3", "4", "5", "6")
  val b = Array("one", "two", "three", "four")

  // e.g. output: [one,2,three,4,5,6]
  println(crossover(a, b).mkString("[", ",", "]"))
  println(crossoverIdiomatic(a, b).mkString("[", ",", "]"))
def crossover[E:ClassTag](a:Array[E],b:Array[E]):Array[E]={
val(较大,较小)=如果(a.长度>b.长度)(a,b)其他(b,a)
val结果=数组.ofDim[E](较大的.length)
对于(i b.长度)(a,b)其他(b,a)
randomPart++较大的.drop(较小的.length)
}
val a=数组(“1”、“2”、“3”、“4”、“5”、“6”)
val b=数组(“一”、“二”、“三”、“四”)
//例如输出:[一,二,三,四,五,六]
println(交叉(a,b).mkString(“[”,“,”,“]))
println(crossoverIdiomatic(a,b).mkString(“[”,“,”,“]))

请注意,
E:ClassTag
只是为了让编译器乐于使用
Array[E]
,如果您的工作只需要
Int
,您可以删除所有花哨的泛型。

您是对的,问题在于“to”应该是“until”。我对您的代码做了一些修改,使其更像scala

  val p1genes = "AGTCTC"
  val p2genes = "ATG"

  val genePair = p1genes.zipAll(p2genes, None, None)
  val matchedGene = for (pair <- genePair) yield {
    pair match {
      case (p1Gene, None) => p1Gene
      case (None, p2Gene) => p2Gene
      case (p1Gene, p2Gene) => if (Random.nextBoolean()) p1Gene else p2Gene
    }
  }
  println(matchedGene)
val p1genes=“AGTCTC”
val p2=“ATG”
val genePair=p1genes.zipAll(p2genes,None,None)
val matchedGene=for(对p1基因
病例(无,p2基因)=>p2基因
case(p1Gene,p2Gene)=>if(Random.nextBoolean())p1Gene else p2Gene
}
}
println(matchedGene)
这个过程是:

  • 首先把两个dna序列压缩成一个
  • 用None填充较短的序列
  • 现在循环压缩序列并填充新序列

  • 你是对的,问题是“to”应该是“until”。我对你的代码做了一些修改,使它更像scala

      val p1genes = "AGTCTC"
      val p2genes = "ATG"
    
      val genePair = p1genes.zipAll(p2genes, None, None)
      val matchedGene = for (pair <- genePair) yield {
        pair match {
          case (p1Gene, None) => p1Gene
          case (None, p2Gene) => p2Gene
          case (p1Gene, p2Gene) => if (Random.nextBoolean()) p1Gene else p2Gene
        }
      }
      println(matchedGene)
    
    val p1genes=“AGTCTC”
    val p2=“ATG”
    val genePair=p1genes.zipAll(p2genes,None,None)
    val matchedGene=for(对p1基因
    病例(无,p2基因)=>p2基因
    case(p1Gene,p2Gene)=>if(Random.nextBoolean())p1Gene else p2Gene
    }
    }
    println(matchedGene)
    
    这个过程是:

  • 首先把两个dna序列压缩成一个
  • 用None填充较短的序列
  • 现在循环压缩序列并填充新序列

  • 重新修改了Tawkir的答案,使用更干净的
    None
    处理:

    val p1genes = "AGTCTC"
    val p2genes = "ATG"
    
    val genePair = p1genes.map(Some.apply).zipAll(p2genes.map(Some.apply), None, None)
    val matchedGene = genePair.map {
      case (Some(p1Gene), None) => p1Gene
      case (None, Some(p2Gene)) => p2Gene
      case (Some(p1Gene), Some(p2Gene)) => if (Random.nextBoolean()) p1Gene else p2Gene
    }
    println(matchedGene)
    
    如果要避免使用
    Some
    包装序列,另一种解决方案是使用已知未出现在序列中的字符作为“无”标记:


    重新修改了Tawkir的答案,使用更干净的
    None
    处理:

    val p1genes = "AGTCTC"
    val p2genes = "ATG"
    
    val genePair = p1genes.map(Some.apply).zipAll(p2genes.map(Some.apply), None, None)
    val matchedGene = genePair.map {
      case (Some(p1Gene), None) => p1Gene
      case (None, Some(p2Gene)) => p2Gene
      case (Some(p1Gene), Some(p2Gene)) => if (Random.nextBoolean()) p1Gene else p2Gene
    }
    println(matchedGene)
    
    如果要避免使用
    Some
    包装序列,另一种解决方案是使用已知未出现在序列中的字符作为“无”标记:


    如果
    childGeneCount
    是较长数组的长度,则应使用
    0直到childGeneCount
    而不是
    0到childGeneCount
    数组(长度)
    引发IndexOutOfBoundsException。如果
    childGeneCount
    是较长数组的长度,则应使用
    0直到childGeneCount
    而不是
    0到childGeneCount
    数组(长度)
    引发IndexOutOfBoundsException。通过在一个序列中将字母与
    None
    s组合,结果是一种
    IndexedSeq[Any]
    类型。这很好,我不知道
    zipAll
    !它没有考虑的一件事是,您可以准确地知道
    None
    何时会出现(最初较小序列的整个末端)。另一个问题是,您将
    Char
    s与
    Option
    混合使用,因此您需要在
    p1genes
    p2genes
    上使用
    genes.map(Some.apply)
    来保留您的类型。曾经可以使用某些特定字符(如“-”)已知不会出现在DNA中而不是
    None
    标记中,或者首先将整个序列转换为某些序列(完整显示在我的答案中)。谢谢,不知道
    .map(Some.apply)
    。通过将字母与
    None
    s组合在一个序列中,结果是典型的