1

我想product()为所有集合添加一个方法并像我可以使用的那样使用它sum()。我已经可以通过 go 获得列表的产品x.inject { a, b -> a * b },但我希望能够 go x.product()

到目前为止我已经尝试过

Collection.metaClass.product = {-> delegate.inject { a, b -> a * b } }

x = [1,2,3,4]
println(x.product())

但这导致

Caught: groovy.lang.MissingMethodException: No signature of method: java.util.ArrayList.inject() is applicable for argument types: (Util$_run_closure1_closure2) values: [Util$_run_closure1_closure2@161bb7fe]
Possible solutions: inject(java.lang.Object, groovy.lang.Closure), inject(java.lang.Object, groovy.lang.Closure), inspect(), toSet(), collect(), collect()
groovy.lang.MissingMethodException: No signature of method: java.util.ArrayList.inject() is applicable for argument types: (Util$_run_closure1_closure2) values: [Util$_run_closure1_closure2@161bb7fe]
Possible solutions: inject(java.lang.Object, groovy.lang.Closure), inject(java.lang.Object, groovy.lang.Closure), inspect(), toSet(), collect(), collect()
    at Util$_run_closure1.doCall(Util.groovy:1)
    at Util.run(Util.groovy:4)
4

2 回答 2

1

弄清楚了。

无论出于什么完全奇怪的原因,groovysh 都允许x.inject { a, b -> a * b },但是当我在 groovysh 之外实际编译它时,它就爆炸了。一旦更改为x.inject(1) { a, b -> a * b },一切都按预期工作。

于 2013-01-30T23:41:10.397 回答
1

这是一个可以通过在 Groovy 控制台中运行代码来测试的解决方案

// implement method
Collection.metaClass.product = {

  if (!delegate) {
    return null
  }

  delegate.inject {a, b ->  a * b}
}

// test it
assert [1,2,3].product() == 6
assert [].product() == null

稍长但更具可读性(IMO)的解决方案是:

Collection.metaClass.product = {

  if (!delegate) {
    return null
  }

  def result = 1

  delegate.each {
    result *= it
  }
  result
}
于 2013-01-30T09:16:25.850 回答