Scala merSorted.length,cIndex+cIncrease) } } 选项列表 } val供应商=列表( (a,aa,1),, (a,aa,2),, c(“a”,“ab”,1), c(“b”,“ba”,1), c(“b”,“bb”,1), c(“b

Scala merSorted.length,cIndex+cIncrease) } } 选项列表 } val供应商=列表( (a,aa,1),, (a,aa,2),, c(“a”,“ab”,1), c(“b”,“ba”,1), c(“b”,“bb”,1), c(“b,scala,Scala,merSorted.length,cIndex+cIncrease) } } 选项列表 } val供应商=列表( (a,aa,1),, (a,aa,2),, c(“a”,“ab”,1), c(“b”,“ba”,1), c(“b”,“bb”,1), c(“b”,“bb”,4) ) val消费者=列表( (a,aa,4),, (a,aa,2),, c(“a”,“ab”,3), c(“b”,“ba”,3), c(“b”,“bb”,3), c(“b”,“bb”,3) ) def子组计算={计算(供应商、

merSorted.length,cIndex+cIncrease) } } 选项列表 } val供应商=列表( (a,aa,1),, (a,aa,2),, c(“a”,“ab”,1), c(“b”,“ba”,1), c(“b”,“bb”,1), c(“b”,“bb”,4) ) val消费者=列表( (a,aa,4),, (a,aa,2),, c(“a”,“ab”,3), c(“b”,“ba”,3), c(“b”,“bb”,3), c(“b”,“bb”,3) ) def子组计算={计算(供应商、消费者、子组)} //逻辑选择子组中得分足够好的对 //删除匹配的供应商和客户将发生在这里 //-只显示找到的对: println(“找到的选项:“+subculations.length”) 子群计算。foreach(o =>println(“[”+o.consumer.group+”、“+o.consumer.subGroup+”]-[“+o.supplier.group+”、“+o.supplier.subGroup+”) +“]有分数“+o.选项,o.分数) ) def groupCalculations={calculations(供应商、消费者、团队)} //逻辑选择组中得分足够好的配对 //删除匹配的供应商和客户将发生在这里 //-只显示找到的对(注意,由于使用更通用的属性,因此有更多的组合): println(“找到的选项:”+groupCalculations.length) 分组计算。foreach(o =>println(“[”+o.consumer.group+”、“+o.consumer.subGroup+”]-[“+o.supplier.group+”、“+o.supplier.subGroup+”) +“]有分数“+o.选项,o.分数) )
@[Ivan Kurchenko],感谢您的建议。虽然
Option
感觉很有希望,但它需要类型规范
Option[data type]
,我看不出如何改变它。有什么建议吗?另外
def groupCalculation=calculations(uu.group)
返回语法错误
无法解析符号组
(第二次声明也是如此)。你的例子编译正确吗?@Dan my bad-修复了它。@[Ivan Kurchenko]我现在理解了逻辑-它非常强大,谢谢!最后一个后续问题:此技术名称是否在Scala中(即将参数声明为
f(f:c=>String)
?(请注意,我将接受响应,然后编辑它,以便其他需要它的人可以使用完整的功能代码)@Dan这里没什么特别的,你们只是提取了一部分不常见的逻辑到函数中——通常它被称为高阶函数,但你们可以看到这个想法非常简单。
case class c (
             group : String,
             subGroup : String,
             someValues : Double
             )

case class option (
                    supplier : c,
                    consumer : c,
                    score : Double
                  )


val supplier = List(
  c("a", "aa", 1),
  c("a", "aa", 2),
  c("a", "ab", 1),
  c("b", "ba", 1),
  c("b", "bb", 1),
  c("b", "bb", 4)
)


val consumer = List(
  c("a", "aa", 4),
  c("a", "aa", 2),
  c("a", "ab", 3),
  c("b", "ba", 3),
  c("b", "bb", 3),
  c("b", "bb", 3)
)

// finding different score options
val supplierSorted = supplier.sortBy(x => x.subGroup) //how to write the sort dynamically?
val consumerSorted = consumer.sortBy(x => x.subGroup) //how to write the sort dynamically?
val options = scala.collection.mutable.ListBuffer[option]()

var sIndex: Int = 0
var cIndex: Int = 0

while(sIndex < supplierSorted.length){
  val s = supplierSorted(sIndex)
  var cIncrease = 0
  var c = consumerSorted(cIndex)
  while (s.subGroup == c.subGroup && cIndex + cIncrease < supplierSorted.length){ //how to write the first condition dynamically?
    options += option(s, c, math.abs(s.someValues - c.someValues))
    cIncrease += 1
    if(cIndex + cIncrease < supplierSorted.length) c = supplierSorted(sIndex + cIncrease)
  }
  sIndex += 1
  if(sIndex < supplierSorted.length && supplierSorted(sIndex).subGroup != s.subGroup) { //how to write the condition dynamically?
    cIndex = math.min(consumerSorted.length, cIndex + cIncrease)
  }

}
def groupCalculations = calculations(_.group)
def subGroupCalculations = calculations(_.subGroup)

def calculations(f: c => String) = {
  // finding different score options
  val supplierSorted = supplier.sortBy(f) //how to write the sort dynamically?
  val consumerSorted = consumer.sortBy(f) //how to write the sort dynamically?
  val options = scala.collection.mutable.ListBuffer[option]()

  while (sIndex < supplierSorted.length) {
    val s = supplierSorted(sIndex)
    var cIncrease = 0
    var c = consumerSorted(cIndex)
    //condition dynamic via function - f
    while (f(s) == f(c) && cIndex + cIncrease < supplierSorted.length) { 
      options += option(s, c, math.abs(s.someValues - c.someValues))
      cIncrease += 1
      if (cIndex + cIncrease < supplierSorted.length)
        c = supplierSorted(sIndex + cIncrease)
    }
    sIndex += 1
    //condition dynamic via function - f
    if (sIndex < supplierSorted.length && f(supplierSorted(sIndex)) != f(s.subGroup)) {
      cIndex = math.min(consumerSorted.length, cIndex + cIncrease)
    }

  }
}

case class c (
                   group : String,
                   subGroup : String,
                   someValues : Double
                 )

    case class option (
                        supplier : c,
                        consumer : c,
                        option : String,
                        score : Double
                      )


    def calculations(supplier : List[c], consumer : List[c], f: c => String) : List[option] = {
      // finding different score options
      val options = scala.collection.mutable.ListBuffer[option]()
      val supplierSorted = supplier.sortBy(f) //sort changes based on parameter f (either subGroup or group)
      val consumerSorted = consumer.sortBy(f)
      var sIndex: Int = 0
      var cIndex: Int = 0

      while (sIndex < supplierSorted.length) {
        val s = supplierSorted(sIndex)
        var cIncrease = 0
        var c = consumerSorted(cIndex)
        //condition dynamic via function - f
        while (f(s) == f(c) && cIndex + cIncrease < supplierSorted.length) {
          options += option(s, c, f.toString(), math.abs(s.someValues - c.someValues))
          cIncrease += 1
          if (cIndex + cIncrease < supplierSorted.length)
            c = supplierSorted(cIndex + cIncrease)
        }
        sIndex += 1
        //condition dynamic via function - f
        if (sIndex < supplierSorted.length && f(supplierSorted(sIndex)) != f(s)) {
          cIndex = math.min(consumerSorted.length, cIndex + cIncrease)
        }

      }
      options.toList
    }

    val supplier = List(
      c("a", "aa", 1),
      c("a", "aa", 2),
      c("a", "ab", 1),
      c("b", "ba", 1),
      c("b", "bb", 1),
      c("b", "bb", 4)
    )
    val consumer = List(
      c("a", "aa", 4),
      c("a", "aa", 2),
      c("a", "ab", 3),
      c("b", "ba", 3),
      c("b", "bb", 3),
      c("b", "bb", 3)
    )
    def subGroupCalculations = {calculations(supplier, consumer, _.subGroup)}
    //logic selecting pairs with good enough score found in subgroup
    //and removing matched suppliers and customers would happen here
    // - just showing found pairs instead:
    println("Options found: " + subGroupCalculations.length)
    subGroupCalculations.foreach(o
    => println("[" + o.consumer.group + "," + o.consumer.subGroup + "] - [" + o.supplier.group + "," + o.supplier.subGroup
        + "] has score " + o.option, o.score)
    )

    def groupCalculations = {calculations(supplier, consumer, _.group)}
    //logic selecting pairs with good enough score found in group
    //and removing matched suppliers and customers would happen here
    // - just showing found pairs instead (notice, there are more combination due to using more generic attribute):
    println("Options found: " + groupCalculations.length)
    groupCalculations.foreach(o
    => println("[" + o.consumer.group + "," + o.consumer.subGroup + "] - [" + o.supplier.group + "," + o.supplier.subGroup
        + "] has score " + o.option, o.score)
    )